【无标题】

row, col = map(int, input().split())
flag = [[0 for _ in range(col)] for _ in range(row)]
lis = []
for _ in range(row):
    lis.append(input())
status_dir = {"bing": [[-1, 0], [1, 0], [0, -1], [0, 1]],
           "ma": [[-2, 1], [2, 1], [1, -2], [1, 2], [-2, -1], [2, -1], [-1, 2], [-1, -2]]}

change_status = {"bing": "ma", "ma": "bing"}
min_step = 100000000000


def dfs(status, i, j, current_step, flag, path):
    if not 0 <= i < row or not 0 <= j < col or flag[i][j] == 2:
        return
    if lis[i][j] == "X":
        return
    if i == row-1 and j == col-1:
        global min_step
        min_step = min(current_step, min_step)
    if current_step > min_step:
        return
    if lis[i][j] == "S":
        for k in range(2):
            if k == 0 and flag[i][j] == 0:
                path.append([i, j])
                ori = status
                status = change_status[status]
                current_step += 1
                flag[i][j] = 1
                dfs(status, i, j, current_step, flag, path)
                flag[i][j] = 0
                current_step -= 1
                status = ori
                path.pop(0)
            if k == 1:
                continue
        for r, c in status_dir[status]:
            i += r
            j += c
            path.append([i, j])
            current_step += 1
            flag[i][j] = 2
            dfs(status, i, j, current_step, flag, path)
            path.pop()
            flag[i][j] = 0
            i -= r
            j -= c
            current_step -= 1
    else:
        for r, c in status_dir[status]:
            i += r
            j += c
            path.append([i, j])
            current_step += 1
            flag[i][j] = 2
            dfs(status, i, j, current_step, flag, path)
            flag[i][j] = 0
            i -= r
            j -= c
            current_step -= 1
            path.pop()


dfs("bing", 0, 0, 0, flag, [[0, 0]])
print(min_step)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值