N,M = [int(i) for i in input().split()]
map = []
for i in range(M):
map.append([int(i) for i in input().split()])
dp = [[[0,0,0,0] for i in range(N)] for i in range(M)]
def find_road(index):
if index[0] >=M or index[0]<0 or index[1] >= N or index[1] <0 or map[index[0]][index[1]] == 0:
return -1
elif index[0] == M-1 and index[1] == N-1:
return 1
for via, value in enumerate(dp[index[0]][index[1]]):
if value == 0 :
if via == 0:
dp[index[0]][index[1]][0] = -1
dp[index[0]][index[1]][0] = find_road((index[0], index[1] + 1))
if dp[index[0]][index[1]][0] == 1:
return 1
elif via == 1:
dp[index[0]][index[1]][1] = -1
dp[index[0]][index[1]][1] = find_road((index[0] + 1, index[1]))
if dp[index[0]][index[1]][1] == 1:
return 1
elif via == 2:
dp[index[0]][index[1]][2] = -1
dp[index[0]][index[1]][2] = find_road((index[0], index[1] - 1))
if dp[index[0]][index[1]][2] == 1:
return 1
else:
dp[index[0]][index[1]][3] = -1
dp[index[0]][index[1]][3] = find_road((index[0] - 1, index[1]))
if dp[index[0]][index[1]][3] == 1:
return 1
return max(dp[index[0]][index[1]])
res = find_road((0,0))
if res == 1:
print('Yes')
else:
print('No')
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
3 KB |
Output is correct |
2 |
Correct |
30 ms |
3 KB |
Output is correct |
3 |
Correct |
37 ms |
3 KB |
Output is correct |
4 |
Runtime error |
79 ms |
4 KB |
Execution failed because the return code was nonzero |
5 |
Halted |
0 ms |
0 KB |
- |