Reprint declaration: Original translation from: http://www.cnblogs.com/xiezie/p/5568822.html
The first encounter maze search, give me the feeling is very pleasantly surprised: understand this words, feel oneself also mastered a skill ~
Personal feelings, role-playing games, at least can be found in the Maze search shadow.
Parity pruning this algorithm feels very open vision ~ So to describe this specific problem is too vivid ~
In a word, it's very interesting.
Careful people will find that whenever the design to move, we have to think about up and down, which also lets us see the idea of such algorithms.
In the Learning Maze search, I found: This search algorithm is the first to analyze the situation when the results are found, and then the path for the barrier, after the upper and lower left and right, and then back to the channel. (Is this a form of recursive notation?) Caishuxueqian, dare not to be raved)
In addition, the problem of Maze search class not only involves backtracking, but also makes people curious about the algorithm of Maze generation ~
Next, we should study the Maze generation algorithm and Maze search algorithm mentioned in depth, breadth search !
Have to draw on the results of others:
Here is a simple theory of pruning, one can understand:
Think of Map as
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
Need odd steps from 0->1
Need even steps from 0->0
Here is the Java implementation code:
ImportJava.util.*;ImportJava.io.*; Public classmain{ Public Static voidMain (string[] arg) {Scanner Scan=NewScanner (NewBufferedinputstream (system.in)); intn,m,t; while((N=scan.nextint ())!=0&& (M=scan.nextint ())!=0&& (T=scan.nextint ())!=0) {OK=false; intx = 0,y = 0; intTargetx = 0,targety = 0; Char[] Map =New Char[N][m]; for(inti = 0; I! = N; i + +) {String row=Scan.next (); Char[] C =Row.tochararray (); for(intj = 0; J! = m; J + +) {Map[i][j]=C[j]; if(c[j]== '. ')){ Continue; } if(c[j]== ' X '){ Continue; } if(c[j]== ' S ') {x=i; Y=J; Continue; } if(c[j]== ' D ') {Targetx=i; Targety=J; } } } //Pruning if((targetx+targety)%2== (x+y)%2) {//same if(t%2==1) {T=-1; } }Else{ if(t%2==0) {//DissimilarT=-1; }} findpath (Map,x,y,t,targetx,targety); if(T==-1) {System.out.println ("NO"); Continue; } if(OK) {System.out.println ("YES"); }Else{System.out.println ("NO"); }} scan.close (); } Static BooleanOK =false; Static voidFindpath (Char[] Map,intIintJintTintTargetx,inttargety) { if(ok| | T==-1){ return; } if(t==0&& (map[i][j]== ' D ' | | i==targetx&&j==targety)) {OK=true; return; } Map[i][j]= ' X '; //on if(j-1!=-1&& map[i][j-1]!= ' X ') {Findpath (map,i,j-1,t-1, targetx,targety); } //under if(j + 1!=map[0].length&&map[i][j + 1]!= ' X ') {Findpath (map,i,j+1,t-1, targetx,targety); } //left if(i-1!=-1&&map[i-1][j]!= ' X ') {Findpath (map,i-1,j,t-1, targetx,targety); } //Right if(i + 1!=map.length&&map[i+1][j]!= ' X ') {Findpath (map,i+1,j,t-1, targetx,targety); } Map[i][j]= '. '; }}
hdoj-acm1010 (JAVA) odd-and-even pruning method maze search