问题给定一个M×N的迷宫图,求一条从指定入口到出口的最短路径。假设迷宫图如图所示(M=8,N=8)对于图中的每个方块,空白表示通道,阴影表示墙壁。获取到的路径必须是简单路径,即获取到的路径上不能重复出现同一个通道块。为了算法的方便,在迷宫周围加了一堵墙。对应的迷宫数组为:vargameMap=[M+2][N+2]int{{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,0,1},{1,0,1,1,1,0,0,0,0,1},{1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,0,1},{1,0,1,1,1,0,1,1,0,1},{1,1,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1},}用go语言实现方案:packagemainimport("fmt")const(M=8N=8)//块类型typeBoxstruct{iint//块行号jint//块列号preint//前一个块在队列中的位置}//orderQueuetypeQueuestruct{data[]Boxfrontintrearint}var(gameMap=[M+2][N+2]int{{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,0,1},{1,0,1,1,1,0,0,0,0,1},{1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,0,1},{1,0,1,1,1,0,1,1,0,1},{1,1,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1},})funcgameSearch(xStart,yStart,xEnd,yEndint)bool{vari,j,diintfind:=falsevarqueue队列queue.data=[]Box{}queue.front=-1queue.rear=-1queue.rear++queue.data=append(queue.data,Box{})queue.data[queue.rear].i=xStartqueue.data[queue.rear].j=yStart//(xStart,yStart)进队queue.data[queue.rear].pre=-1gameMap[xStart][yStart]=-1forqueue.front!=queue.rear&&!find{queue.front++i=queue.data[queue.front].ij=queue.data[queue.front].jifi==xEnd&&j==yEnd{find=true打印路径(&queue,queue.front)returntrue}//顺时针fordi=0;迪<4;di++{switchdi{case0:i=queue.data[queue.front].i-1j=queue.data[queue.front].j案例1:i=queue.data[queue.front].ij=queue.data[queue.front].j+1案例2:i=queue.data[queue.front].i+1j=queue.data[queue.front].j案例3:i=queue.data[queue.front].ij=queue.data[queue.front].j-1}ifgameMap[i][j]==0{queue.rear++queue.data=append(queue.data,Box{})queue.data[queue.rear].i=iqueue.data[queue.rear].j=jqueue.data[queue.rear].pre=queue.frontgameMap[i][j]=-1}}}returnfalse}funcprintPath(queue*Queue,frontint){vark,j,ns=front复制代码,0,0varmaxSize=len(queue.data)fmt.Println("\n")对于k!=0{j=kk=queue.data[k].prequeue.data[j].pre=-1}k=0fmt.Println("迷宫路径如下:\n")fork
