题目描述
解题思路
题目的意思就是跟着脚下字母走,如果能从边界走出去就是出了迷宫,如果在里面死循环就是走不出
1.走出迷宫终止条件
当前坐标根据脚下字母移动后X或Y大于10,或者是X或Y小于0。这时候能走出迷宫
2.走不出迷宫的终止条件
如果上一步是和这一步要走的方向是相反的则陷入死循环,走不出迷宫。例如上一步是D当前这一步是U
代码
time#在遍历中记录有多少人走出迷宫
move#移动之后当前脚下字母
lastmove#上一步脚下的字母
move_x#移动之后当前的x坐标
move_y#移动之后当前的y坐标
a=[list(map(str,input())) for _ in range(10)]
time=0
for x in range(10):
for y in range(10):
if a[x][y]=='U':
if x-1<0:
time+=1
continue
move=a[x-1][y]
move_x=x-1
move_y=y
if a[x][y] == 'D':
if x+1>9:
time+=1
continue
move = a[x+1][y]
move_x=x+1
move_y=y
if a[x][y]=='R':
if y+1>9:
time+=1
continue
move=a[x][y+1]
move_x=x
move_y=y+1
if a[x][y]=='L':
if y-1<0:
time+=1
continue
move=a[x][y-1]
move_x=x
move_y=y-1
lastmove=a[x][y]
while True:
if lastmove=='R' and move=='L':
break
if lastmove=='U' and move=='D':
break
if lastmove == 'L' and move == 'R':
break
if lastmove == 'D' and move == 'U':
break
if move == 'U':
if move_x - 1 < 0:
time += 1
break
lastmove = move
move = a[move_x - 1][move_y]
move_x=move_x-1
move_y=move_y
if move == 'D':
if move_x + 1 > 9:
time += 1
break
lastmove = move
move = a[move_x + 1][move_y]
move_x=move_x+1
move_y=move_y
if move == 'R':
if move_y + 1 > 9:
time += 1
break
lastmove = move
move = a[move_x][move_y + 1]
move_x=move_x
move_y=move_y+1
if move == 'L':
if move_y- 1 < 0:
time += 1
break
lastmove = move
move = a[move_x][move_y - 1]
move_x=move_x
move_y=move_y-1
continue
print(time)