ATC A - 深さ優先探索
提出
WA
code: python
h, w = list(map(int, input().split()))
area = []
for i in range(h):
row = input().split()
area.append(*row)
for x in range(h):
for y in range(w):
for i in range(4):
dist = [s0 + moveXi, s1 + moveYi] 解答
code: python
import sys
sys.setrecursionlimit(10**7)
h, w = list(map(int, input().split()))
searched = [False * w for _ in range(h)] for x in range(h):
for y in range(w):
sx, sy = x, y
gx, gy = x, y
def areaSearch(x, y):
# 右, 下、左、上
for i in range(4):
if (nextX >= 0 and nextX < h and nextY >= 0 and nextY < w):
areaSearch(nextX, nextY)
areaSearch(sx, sy)
print("Yes")
else:
print("No")
テーマ
メモ
再帰関数の呼び出し制限
code: python
import sys
sys.setrecursionlimit(10**7)
提出
checkedの位置、nextX, nextY
code: python
h, w = map(int, input().split())
for y in range(h):
for x in range(w):
checked = [0 * w for _ in range(h)] def dfs(nowy, nowx):
for y in goy:
for x in gox:
nextY = nowy + y
nextX = nowx + x
if (nextY < 0 or nextY > h - 1 or nextX < 0 or nextX > w - 1):
pass
pass
else:
dfs(nextY, nextX)
print("Yes")
else:
print("No")
提出
AC
code: python
h, w = map(int, input().split())
for i in range(h):
for j in range(w):
s = (i, j)
g = (i, j)
visited = [False * w for _ in range(h)] while stack:
now_i, now_j = stack.pop(0)
for k in range(4):
next_i, next_j = now_i + xk, now_j + yk stack.append((next_i, next_j))
print("Yes") if visited[g0][g1] else print("No")