hdoj-acm1010 (JAVA) odd-and-even pruning method maze search

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.