解题思路:
注意事项:
参考代码:
def get(st,mmp,n,m): for i in range(n): for j in range(m): if mmp[i][j] == st: return [i, j, 0] def bfs(mmp): x = [1, -1, 0, 0] y = [0, 0, 1, -1] li = get('@', mmp, a, b) end = get('*', mmp, a, b) queue = [] ans = 0 queue.append(li) while queue: mmp[li[0]][li[1]] = '#' xx, yy, ans = queue.pop(0) if xx == end[0] and yy == end[1]: return ans for i in range(4): lx = xx + x[i] ly = yy + y[i] if lx >= 0 and lx < a and ly >= 0 and ly < b and mmp[lx][ly] != '#': queue.append([lx, ly, ans + 1]) mmp[lx][ly] = '#' return -1 while True: a,b=map(int,input().split()) if a ==0 and b==0: break mmp = [ list(input()) for _ in range(a)] print(bfs(mmp))
0.0分
1 人评分
C语言训练-舍罕王的失算 (C语言代码)浏览:1033 |
大神老白 (C语言代码)浏览:713 |
简单的a+b (C语言代码)浏览:712 |
【亲和数】 (C语言代码)浏览:492 |
哥德巴赫曾猜测 (C语言代码)浏览:995 |
字符串的输入输出处理 (C语言代码)浏览:926 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:614 |
A+B for Input-Output Practice (V) (C语言代码)浏览:463 |
星期判断机 (C语言代码)浏览:859 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:637 |