15
0 7 7 7
*5#++B+B+++++$3
55#+++++++###$$
###$++++++#+*#+
++$@$+++$$$3+#+
+++$$+++$+4###+
A++++###$@+$++A
+++++#++$#$$+++
A++++#+5+#+++++
+++$$#$++#++++A
+++$+@$###+++++
+###4+$+++$$+++
+#+3$$$+++$##++
+#*+#++++++#$$+
$####+++++++$##
3$+++B++B++++#5
输出13
0 7 7 7
*5#++B+B+++++$3
55#+++++++###$$
###$++++++#+*#+
++$@$+++$$$3+#+
+++$$+++$+4###+
A++++###$@+$++A
+++++#++$#$$+++
A++++#+5+#+++++
+++$$#$++#++++A
+++$+@$###+++++
+###4+$+++$$+++
+#+3$$$+++$##++
+#*+#++++++#$$+
$####+++++++$##
3$+++B++B++++#5
输出13
给 了4个数 a b c d, 我以为是求(a, b) 到 (c, d) 怪不得不是9
结果tm的 是求 (b, a) 到 (d, c)
from collections import deque def main(): n = int(input()) a, b, c, d = map(int, input().split()) # 给4个数, 结果求 (b, a) 到(d, c) x1 = b y1 = a x2 = d y2 = c maze = [] for _ in range(n): L = list(input()) transform(L) maze.append(L) mark = [[-1] * n for _ in range(n)] mark[x1][y1] = 0 bfs(maze, mark, n, x1, y1, x2, y2) def transform(L): for i in range(len(L)): if L[i] == '#'&nbs***bsp;L[i] == '@': L[i] = 1 else: L[i] = 0 def bfs(maze, mark, n,a,b, c, d): queue = deque([[a, b]]) dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] while queue: a, b = queue.popleft() for i in range(4): x = a + dx[i] y = b + dy[i] if 0 <= x < n and 0 <= y < n and mark[x][y] == -1 and maze[x][y] == 0: queue.append([x, y]) mark[x][y] = mark[a][b] + 1 print(mark[c][d]) main()
全部评论
(3) 回帖