728x90
https://www.acmicpc.net/problem/18428
import sys
input = sys.stdin.readline
def backtracking(cnt):
global flag
if cnt == 3:
if bfs():
flag = True
return
else:
for i in range(N):
for j in range(N):
if graph[i][j] == "X":
graph[i][j] = "O"
backtracking(cnt + 1)
graph[i][j] = "X"
def bfs():
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
for t in teacher:
for k in range(4):
nx, ny = t
while 0 <= nx < N and 0 <= ny < N:
if graph[nx][ny] == "O":
break
if graph[nx][ny] == "S":
return False
nx += dx[k]
ny += dy[k]
return True
N = int(input())
graph = []
teacher = []
flag = False
for i in range(N):
M = list(map(str, input().rstrip().split(" ")))
graph.append(M)
for j in range(N):
if graph[i][j] == "T":
teacher.append((i, j))
backtracking(0)
if flag:
print("YES")
else:
print("NO")
728x90
'Study > Coding Test' 카테고리의 다른 글
[백준] 1940 - 연속합 Python (0) | 2023.06.29 |
---|---|
[백준] 11660 - 구간 합 구하기 5 Python (0) | 2023.06.28 |
[백준] 11053 - 가장 긴 증가하는 부분 수열 Python (0) | 2023.06.27 |
[백준] 5430 - AC Python (0) | 2023.06.05 |
[백준] 11727 - 2×n 타일링 2 Python (0) | 2023.06.05 |