Packagesort; Public classHuisu { Public Static voidMain (String args[]) {intdata[][]={{1,1,1,1,1,1,1,1,1},//Maze of 10 rows and 9 columns{0,0,0,0,0,0,0,0,1}, {1,0,1,0,0,1,1,0,1}, {1,0,0,1,0,1,0,0,1}, {1,1,0,1,0,0,1,0,1}, {1,0,0,0,1,0,1,0,1}, {1,0,1,1,0,0,1,0,1}, {1,0,0,0,1,1,0,1,1}, {1,0,1,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1}}; intfx[][]={{0,1},{1,0},{0,-1},{-1,0}};//represents the top left four directions intx=1,y=0,p=0,i=0;//x is the coordinate on the x-axis, y is the coordinate on the y-axis, and p is the number of steps intxx[]=New int[90];//record the x-coordinate of each step intyy[]=New int[90];//record the y-coordinate of each step intss[]=New int[90];//record each point walked a few directions, if equal to 4, indicating that all directions were gone for(intj=0;j<90;j++) {//Initialize three arraysXx[j]=0; YY[J]=0; SS[J]=0; } Xx[p]=x;//X=1 indicates that the entry starts from the second rowyy[p]=y; while(x>=0 && x<=9 && y>=0 && y<=8) {//loop within the range of the graphi=i+1;//I means four directions, plus 1 is the next direction. if(i<=4) {//If the four directions are not finished, the pointer is to the right; when i=2, the pointer is down; When i=3, the pointer goes to the left; i=4, the pointer is upX=XX[P]+FX[I-1][0];//coordinate movement in the x directionY=YY[P]+FX[I-1][1];//coordinate movement in y direction if(X==1 && y==0) {i++; X=XX[P]+FX[I-1][0];//coordinate movement in the x directionY=YY[P]+FX[I-1][1];//coordinate movement in y direction } if(x>=0 && x<=9 && y>=0 && y<=8) {//if the boundary is not exceeded in the X and y directions if(data[x][y]==0) {//If the next point doesn't have a barrier pointp++;//Step Count IncreaseSs[p]=i;//record the direction that has gone in four directionsXx[p]=x;//record X coordinateYy[p]=y;//record y-coordinatedata[x][y]=2;//2 means that the point has passed//System.out.println ("direction" +ss[p]+ "up", coordinates for ("+xx[p]+", "+yy[p]+"), "+p+" step after the array result; ");//for (int t=0;t<10;t++) {//for (int m=0;m<9;m++)//System.out.print (data[t][m]+ "");//System.out.println ();// }I= 0;//the point can be reached, set I to 0, and re-search the 4 directions of the point } } }Else{//Otherwise, the four directions of the point have already been completed and cannot continue to go down, then returnI=SS[P];//after returning, take out the original directionp--;//after the return, the number of steps should be reduced by 1//System.out.println ("Return to the +p+" step, the original direction is: "+i+", coordinates are: ("+xx[p]+", "+yy[p]+") "); } } if(p<=0) System.out.println ("Can't pass!" ");//P=0 said back to the entrance, no path to go Else{//Otherwise, an existing path is indicated, the path is outputSystem.out.print ("Running Route: entrance: (1,0)--"); for(intk=1;k<=p;k++) System.out.print ("(" +xx[k]+ "," +yy[k]+ ")--"); System.out.println (Export "); } }}
Backtracking algorithm for solving maze problem