huangjian


私信TA

用户名:dotcpp0626587

访问量:541

签 名:

等  级
排  名 9902
经  验 1067
参赛次数 1
文章发表 15
年  龄 0
在职情况 教师
学  校
专  业

  自我简介:

import pprint
r,c=map(int,input().split())
b=[]
for x in range(c):
    b.append(list(input()))
_=input()
d=[(1,0),(0,1),(-1,0),(0,-1)]

node=[]
vis=set()

for x in range(c):
    for y in range(r):
        if b[x][y]=='@':
            node.append((x,y))
            vis.add((x,y))
            break
    else:
        continue
    break
                 
step=1
def bfs():
    global step
    while node:
        size=len(node)
        for i in range(size):
            x=node[0][0]
            y=node[0][1]
            node.remove((x,y))
            for m,n in d:
               
                if 0<=x+m<c and 0<=y+n<len(b[x+m]) and (x+m,y+n) not in vis and b[x+m][y+n]!="#":
                    if y+n>=c:
                        continue
                    step+=1
                    node.append((x+m,y+n))
                    vis.add((x+m,y+n))
     
bfs()
print(step)

最后一个样例:

16 16
....#.......##..
....##......##..
....#.......#...
...##.......#...
....@.........##
..............##
##....##....##..
#...........##..
#...............
.#..#.##........
##..............
.........#..####
..................
........#######.
.#..#.##........
.#..#.##
0 0


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区