桃花惜天


私信TA

用户名:dotcpp0601297

访问量:1085

签 名:

永远相信美好的事情即将发生!

等  级
排  名 2018
经  验 2408
参赛次数 0
文章发表 17
年  龄 0
在职情况 学生
学  校 长江大学
专  业

  自我简介:

TA的其他文章


解题思路:

注意事项:

参考代码:


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 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区