first read a maze file maze.in (the first line is the file column below is the file content: 1 means the wall 0 means that the road can go)
How to read?
//reading a two-dimensional arrayFunc readmaze (filenamestring) [][]int { //opening the file by default is already present in this file so there is no other error handlingFile, err: =OS. Open (filename)ifErr! =Nil {panic (err)}//you need to read the rows and columns first and read them using fscanf . varRow, colintFMT. FSCANF (file,"%d%d", &row, &Col)//Create two-dimensional slice note: Here is the incoming parameter row row because the two-dimensional array is actually: there is a row so many one-dimensional array composition here is the use of slicesMaze: = make ([]int, Row) forI: =Range Maze {Maze[i]= Make ([]int, col)//Create with Make forJ: =Range Maze[i] {fmt. FSCANF (file,"%d", &maze[i][j])} } returnMaze}func Main () {maze:= Readmaze ("mazepractice/maze.in") for_,row: =Range Maze { for_,val: =range Row {fmt. Printf ("%3d", Val)} Fmt. Println ()}}
Interpretation of Maze algorithm
Package Mainimport ("FMT" "OS") func readmaze (filenamestring) [][]int{file, err:=OS. Open (filename)ifErr! =Nil {panic (err)}varRow, colintFMT. FSCANF (file,"%d%d", &row, &col) Maze:= Make ([]int, Row) forI: =Range Maze {Maze[i]= Make ([]int, col) forJ: =Range Maze[i] {fmt. FSCANF (file,"%d", &maze[i][j])} } returnMaze}//define the structure of a point because this is an array it is best not to use X, y definition (to avoid confusion with coordinates)Type Pointstruct{i, Jint}//Define 4 Directions: Top left and bottom rightvarDirs = [4]point{{-1,0}, {0, -1}, {1,0}, {0,1}}//The maze needs to go from the top left to the right, so the coordinates change accordingly. Note: Here is the use of returning a new point instead of using the pointer (of course, the pointer can)func (P point) at Add (R point), point {returnPOINT{P.I + r.i, P.J +R.J}}//Judging if the point to go is not reasonable there is no cross-border the return value is a point and a status valueFunc (P point) at (Grid []int) (int,BOOL) { ifP.I <0|| P.I >=len (grid) {return 0,false } ifP.J <0|| P.J >=Len (grid[p.i]) {return 0,false } returnGRID[P.I][P.J],true}//the key algorithm to walk the maze pass through 3 parameters: two-dimensional slicing, start point, end point return: A two-dimensional sliceFunc Walk (Maze []int, start, end point) []int { //This is actually creating a steps that walks through the path. Based on the size of the file maze.in we read, createSteps: = make ([]int, Len (Maze)) forI: =range Steps {steps[i]= Make ([]int, Len (Maze[i]))} //Create a queue start point Yes (0,0)Q: =[]point{start}//queue is not empty forLen (Q) >0{cur:= q[0]//cur Team start point (0,0)Q = q[1:]//Remove the first point of the queue//Find the end of the exit ifCur = =End { Break } //no, look. for_, dir: =Range Dirs {//go down these roads through the left and right .Next: =Cur.add (dir)//Judging the route is not allowed to walk: 0 can walk 1 not to go, of course, cross-border is not possibleVal, OK: =next.at (Maze)if!ok | | val = =1 { Continue } //Judging whether these points have passed: just don't go backwardsval, ok =next.at (steps)if!ok | | Val! =0 { Continue } //not 0, maybe back to the beginning. ifNext = =Start {Continue } //Current number of stepsCursteps, _: =cur.at (steps) STEPS[NEXT.I][NEXT.J]= Cursteps +1 //Put this in the back of the queue.Q =Append (Q, Next)}} returnSteps}func Main () {maze:= Readmaze ("mazepractice/maze.in") Steps:= Walk (Maze, point{0,0}, Point{len (Maze)-1, Len (maze[0]) -1}) //Print the actual walk of this array for_, Row: =Range Steps { for_, Val: =range Row {fmt. Printf ("%3d", Val)} Fmt. Println ()} lessstep:= Steps[len (Maze)-1][len (maze[0])-1] FMT. Printf ("This maze needs at least%d steps to complete.", Lessstep)//todo:construct path from steps}
The left is the read file, and the right side is the output:
The left is the read file, and the right side is the output:
Go Maze algorithm