PackageCorejava; Public classFourtwo {Static intM//(M,n) is a few rows or columns Static intN; Static intP//(P,Q) as the end point Static intQ; Static intmin=9999; Static int[][]a=New int[51] [51];//Store Map Static int[][]b=New int[51] [51];//Storage Path StaticString []record=Newstring[9999]; Public Static voidDfsintXintYintStep) { int[][]next={{0,1},{1,0},{0,-1},{-1,0}}; if(x==p&y==q) {System.out.println ("Step" +step); for(inti=0;i<step;i++) {System.out.print (record[i]); } System.out.println (" "); if(step<min) {min=step; } return; } for(intk=0;k<4;k++) { intTx=x+next[k][0]; intTy=y+next[k][1]; if(tx<0| | tx>=m| | ty<0| | ty>=N) {Continue; } if(a[tx][ty]==0&b[tx][ty]==0) {Record[step]= "(" +tx+ "," +ty+ ")"; B[tx][ty]=1; DFS (Tx,ty,step+1);//if this line of code changes to step++;d FS (tx,ty,step), then the value of step in the for loop is changed and there is a problemB[tx][ty]=0; } } return; } Public Static voidMain (String []args) {m= 5; N= 4; intStartx=0; intStarty=0; P=3; Q=2; B[startx][starty]=1; record[0]= "(" +0+ "," +0+ ")"; a[0][2]=1; a[2][2]=1; a[3][1]=1; a[4][3]=1; DFS (Startx,starty,0); System.out.println ("Min" +min); }}
Here it feels too troublesome to get the input and set the fixed value directly. At the same time Dfs (TX,TY,STEP+1); Here it bothers me, began not to consider the step in the cycle of use, after step=step+1 changed the loop step value, query for a long time to know the cause of the error.
Aha, the fourth chapter of the second section of the rescue of small Ha Java implementation