-
Notifications
You must be signed in to change notification settings - Fork 0
/
파이프 옮기기 1.py
73 lines (65 loc) · 1.78 KB
/
파이프 옮기기 1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
시작 시간: 2022-09-04 PM 04:24
소요 시간: 2시간
풀이 방법:
- DFS. 모든 경우를 탐색해야하므로 DFS, BFS 효율은 같다
- 시간 초과..
"""
HORIZONTAL = '가로'
VERTICAL = '세로'
ACROSS = '대각선'
INFO = {
HORIZONTAL: { HORIZONTAL: [[0, 1]], ACROSS: [[1, 1], [0, 1], [1, 0]] },
VERTICAL: { VERTICAL: [[1, 0]], ACROSS: [[1, 1], [0, 1], [1, 0]] },
ACROSS: { HORIZONTAL: [[0, 1]], VERTICAL: [[1, 0]], ACROSS: [[1, 1], [0, 1], [1, 0]] }
}
def input_data():
global N, MAP
N = int(input())
MAP = []
for _ in range(N):
line = input()
row = []
for char in line:
if char != " ":
row.append(char)
MAP.append(row)
def input_file(path):
global N, MAP
fs = open(path, 'r')
N = int(fs.readline().strip())
MAP = []
for _ in range(N):
line = list(fs.readline().strip())
row = []
for char in line:
if char != " ":
row.append(char)
MAP.append(row)
def is_movable(x, y, empty_diffs):
global N, MAP
for dx, dy in empty_diffs:
nx, ny = x+dx, y+dy
if not (0 <= nx < N and 0 <= ny < N):
return False
if MAP[nx][ny] != '0':
return False
return True
global N, MAP
input_data()
stack = [(0, 1, HORIZONTAL)]
answer = 0
while stack:
x, y, direction = stack.pop()
if x == N-1 and y == N-1:
answer+=1
continue
empties = INFO[direction]
directions = empties.keys()
for next_direction in directions:
diffs = empties[next_direction]
nx, ny = x+diffs[0][0], y+diffs[0][1]
result = is_movable(x, y, diffs)
if is_movable(x, y, diffs):
stack.append((nx, ny, next_direction))
print(answer)