题目描述
blablabla
样例
blablabla
算法1
blablabla
时间复杂度
参考文献
Python3 代码
import collections
from sys import stdin
n, m = [int(x) for x in stdin.readline().strip().split(' ')]
done = [[False for _ in range(m)] for _ in range(n)]
migong = []
for i in range(n):
migong.append( [int(x) for x in stdin.readline().strip().split(' ')] )
def bfs(migong):
res = 0
que = collections.deque()
que.append((0,0,0))
done[0][0] = True
dirt = [(1,0),(0,1),(-1,0),(0,-1)]
while que:
#size = len(que)
#for _ in range(size):
x,y,dis = que.popleft()
if x == n - 1 and y == m - 1:
print(dis)
break
else:
for i,j in dirt:
if 0 <= x+i < n and 0 <= y+j < m and migong[x+i][y+j] == 0 and not done[x+i][y+j]:
que.append((x+i,y+j,dis+1))
done[x+i][y+j] = True
if __name__ == "__main__":
bfs(migong)
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla