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)
【无标题】
最新推荐文章于 2024-11-12 21:59:57 发布