acwing 1011 献给阿尔吉侬的花束

# acwing 1011 献给阿尔吉侬的花束 https://www.acwing.com/problem/content/description/1103/
# https://www.acwing.com/solution/content/9093/
# 这题虽然还好,但是debug了有一会儿,刚开始是没用函数写的,一直没找出来
# 还是用函数写简单些,以后尽量用函数写吧
from collections import deque

N = 200
t = int(input())
a = [[0] * N for _ in range(N)]
dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]


def dfs(start):
    global r, c
    q = deque()
    bo = [[0] * N for _ in range(N)]
    dis = [[0] * N for _ in range(N)]
    q.append(start)
    bo[start[0]][start[1]] = 1
    while q:
        tmp = q.popleft()
        for i in dxy:
            x, y = tmp[0] + i[0], tmp[1] + i[1]
            if x >= 0 and y >= 0 and x < r and y < c and a[x][y] != '#' and bo[x][y] == 0:
                q.append((x, y))
                dis[x][y] = dis[tmp[0]][tmp[1]] + 1
                bo[x][y] = 1
            if (x, y) == end:
                ans = dis[x][y]
                return ans
    return 0


for _ in range(t):
    r, c = map(int, input().split())
    for i in range(r):
        j = 0
        for tmp in input():
            a[i][j] = tmp
            if tmp == 'S':
                start = (i, j)
            if tmp == 'E':
                end = (i, j)
            j += 1
    ans = dfs(start)
    if ans != 0:
        print(ans)
    else:
        print('oop!')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值