【问题描述】
下图给出了一个迷宫的平面图,其中标记为 1的为障碍,标记为 0的为可以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按 DRRURRDDDR的顺序通过迷宫,一共 10步。其中 D、U、L、R分别表示向下、向上、向左、向右走。
对于下面这个更复杂的迷宫( 30行 50列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。请注意在字典序中 D<L<R<U。(如果你把以下文字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 maze.txt,内容与下面的文本相同)
迷宫略
ditu=[
'1111111111111111111111111111111111111111111111111111',
'1010101010010110010010101100101101001000010001010101',
'1000010001000001010100100001000000010011001101001011',
'1011110110100100010000011010010111000110000000100001',
'1010000000010101000110100001010000010101010110010111',
'1000111110000001010000100101000101000001011000000001',
'1110010001101010000101011000110100110101010111101111',
'1000110110101010010010010100000010001010011100000001',
'1101000001010001001101010101111100110000100001110101',
'1001110000010101000011000100000010001010011000010011',
'1110001101000011100100010010101010101010100011010001',
'1000100001001000001010010101011101000101010100001011',
'1111001001010010010000100000101010101001001000101001',
'1000000100000001010110011110100011000001010101000111',
'1101010100111000010000110000101100111101101000010001',
'1101010101000011010101001010000101000001110111010011',
'1100000001011000100001011001011010010111000000001001',
'1101010010000000101001000010001000001000111101010011',
'1001010010101011010010101000110101011011100001101011',
'1110010100001000011000000101001010000010001110000101',
'1000010001100001101011010000001001010010010000111011',
'1101001010001010000000011101100101101011010101000011',
'1001010000100001101010100001000100010010001000101011',
'1101000010001100100010000101010010101010111110100101',
'1000001001010000001100101001010010000010000000000101',
'1110100000010011101110010010000111010010110111010001',
'1000001101000100010001000000010000111010000001100111',
'1101010001010001000100011111000101010010100000010001',
'1100000101001010010101100000001001010100010111010001',
'1001111000010000100000001101110000000010000000010111',
'1100000011001110101110100010001101110101011011110001',
'1111111111111111111111111111111111111111111111111111'
]
start=(1,1)
end=(30,50)
queue=[[start]]
zouguo=[start]
def bfs(queue):
if end in queue[-1]:
return queue
nows=[]
for now in queue[-1]:
hang,lie=now
if ditu[hang+1][lie]=='0' and ((hang+1,lie) not in zouguo):
nows.append((hang+1,lie))
zouguo.append((hang+1,lie))
if ditu[hang][lie-1]=='0' and ((hang,lie-1) not in zouguo):
nows.append((hang,lie-1))
zouguo.append((hang,lie-1))
if ditu[hang][lie+1]=='0' and ((hang,lie+1) not in zouguo):
nows.append((hang,lie+1))
zouguo.append((hang,lie+1))
if ditu[hang-1][lie]=='0' and ((hang-1,lie) not in zouguo):
nows.append((hang-1,lie))
zouguo.append((hang-1,lie))
queue.append(nows)
return bfs(queue)
queue=bfs(queue)
zouguo2=[start]
stack=[start]
while stack[-1] != end:
hang,lie=stack[-1]
keneng=queue[len(stack)]
if (hang+1,lie) in keneng and (hang+1,lie) not in zouguo2:
stack.append((hang+1,lie))
zouguo2.append((hang+1,lie))
continue
elif (hang,lie-1) in keneng and (hang,lie-1) not in zouguo2:
stack.append((hang,lie-1))
zouguo2.append((hang,lie-1))
continue
elif (hang,lie+1) in keneng and (hang,lie+1) not in zouguo2:
stack.append((hang,lie+1))
zouguo2.append((hang,lie+1))
continue
elif (hang-1,lie) in keneng and (hang-1,lie) not in zouguo2:
stack.append((hang-1,lie))
zouguo2.append((hang-1,lie))
continue
else:stack.pop()
txt=''
for i in range(len(stack)-1):
if stack[i+1][0]>stack[i][0]:
txt+='D'
elif stack[i+1][0]<stack[i][0]:
txt+='U'
if stack[i+1][1]>stack[i][1]:
txt+='R'
if stack[i+1][1]<stack[i][1]:
txt+='L'
print(txt)