문제
- SWEA 4875 미로 D2
- python
- 문제링크
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
나의 코드
"""
Approach: DFS
"""
T = int(input())
def isCondition(row, col):
return 0 <= row < N and 0 <= col < N and (row,col) not in visited and (grid[row][col] == 0 or grid[row][col] == 3)
def dfs(row, col):
global res
if grid[row][col] == 3:
res = 1
return
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
for dir in range(4):
nx = row + dx[dir]
ny = col + dy[dir]
if isCondition(nx, ny):
visited.append((nx, ny))
dfs(nx, ny)
visited.remove((nx, ny))
for tc in range(T):
N = int(input())
grid = [list(map(int, input())) for _ in range(N)]
start_x = 0
start_y = 0
for i in range(N):
for j in range(N):
if grid[i][j] == 2:
start_x, start_y = i, j
break
res = 0
visited = []
dfs(start_x, start_y)
print("#{} {}".format(tc+1, res))
'개발공부 > algorithm' 카테고리의 다른 글
[백준][python] 1904.01타일 - 동적계획법 (0) | 2021.03.07 |
---|---|
[백준][python] 1003.피보나치 함수 - 동적계획법 (0) | 2021.03.07 |
[SWEA][python] 5102.노드의 거리 - BFS (0) | 2021.03.04 |
[SWEA] [python] 1231.중위순회 - tree (0) | 2021.03.03 |
[SWEA] [python] 5174, 5176, 5177, 5178 - Tree (0) | 2021.03.03 |