POJ 3083 Children of the Candy Corn (DFS+BFS)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3083

The main topic: give you a maze, S is the starting point, E is the end point, #是墙,. Is the road, S, e at the boundary of the maze, and has a unique solution; The number of steps to turn left S through E, the number of steps to the first right to E, and the shortest number of steps from S to E.

Exercises

1, the crux of the problem lies in the left turn priority and right turn priority, the next direction depends on the direction of the current position, with Dfs constantly in priority direction to traverse through the maze; I define

Former (0)
Left (1) Current position direction (dir) Right (3)
After (2)

Take the left-hand priority for example, to facilitate the direction of the maze: left, front, right, after;

Left: (dir+1)%4

former; (dir+0)%4

Right: (dir+3)%4

Post: (dir+2)%4

Turn right first as long as change the direction of traverse;

2, for S to e the shortest-circuiting with BFS traversal can be;

* See the code comment in detail

#include <iostream>#include<queue>#include<cstring>#include<cstdio>using namespacestd;structpoint{intX,y,step;};Charmap[ -][ -];queue<point>Q;intn,m;intLstep,rstep;intgo[4][2]={-1,0,0,-1,1,0,0,1};//Four directions, left, front, right, back storageintIs_way (intXintY//to determine if this point can go{    if(x<0|| X>=n | | y<0|| Y>=m | | map[x][y]=='#')        return 0; return 1;}voidLDFs (intXintYintDir//left turn priority, X, Y record current position, dir record current direction{lstep++;//Number of steps//cout<<x<< ' <<y<< ' <<fx<<endl; //GetChar ();    if(map[x][y]=='E')return;//Find the end of the exit    if(Is_way (x+go[(dir+1)%4][0],y+go[(dir+1)%4][1]) )//leftLDFs (x+go[(dir+1)%4][0], y+go[(dir+1)%4][1], (dir+1)%4 ); Else if(Is_way (x+go[(dir)%4][0],y+go[(dir)%4][1]) )//Go aheadLDFs (x+go[(dir)%4][0], y+go[(dir)%4][1], (dir)%4 ); Else if(Is_way (x+go[(dir+3)%4][0],y+go[(dir+3)%4][1]) )//Turn RightLDFs (x+go[(dir+3)%4][0], y+go[(dir+3)%4][1], (dir+3)%4 ); ElseLDFs (x+go[(dir+2)%4][0], y+go[(dir+2)%4][1], (dir+2)%4);//back}voidRdfsintXintYintDir//Turn Right First{rstep++; if(map[x][y]=='E')return; if(Is_way (x+go[(dir+3)%4][0],y+go[(dir+3)%4][1]) )//Turn RightRdfs (x+go[(dir+3)%4][0], y+go[(dir+3)%4][1], (dir+3)%4 ); Else if(Is_way (x+go[(dir)%4][0],y+go[(dir)%4][1]) )//Go aheadRdfs (x+go[(dir)%4][0], y+go[(dir)%4][1], (dir)%4 ); Else if(Is_way (x+go[(dir+1)%4][0],y+go[(dir+1)%4][1]) )//leftRdfs (x+go[(dir+1)%4][0], y+go[(dir+1)%4][1], (dir+1)%4 ); ElseRdfs (x+go[(dir+2)%4][0], y+go[(dir+2)%4][1], (dir+2)%4);//back}intBFS ()//for S to e shortest distance, BFS template can be{     while(!Q.empty ()) {Point P=Q.front ();        Q.pop ();  for(intI=0;i<4; i++)        {            intx=p.x+go[i][0]; inty=p.y+go[i][1]; if(map[x][y]=='E')returnp.step+1; if(Is_way (x, y)) {point temp; Temp.x=x; TEMP.Y=y; Temp.step=p.step+1;                Q.push (temp); Map[x][y]='#'; }        }    }    return 0;}intMain () {intT; CIN>>T;  while(t--)    {         while(!q.empty ()) Q.pop (); intx, y; CIN>>m>>N;  for(intI=0; i<n;i++)             for(intj=0; j<m;j++) {cin>>Map[i][j]; if(map[i][j]=='S') {x=i; Y=J; }} lstep=0; Rstep=0; LDFs (x, Y,0); Rdfs (x, Y,0);        Point P; P.x=x; P.Y=y; P.step=1;        Q.push (P); Map[x][y]='#'; cout<<lstep<<' '<<rstep<<' '&LT;&LT;BFS () <<Endl; }    return 0;}

Post: (dir+2)%4

POJ 3083 Children of the Candy Corn (DFS+BFS)

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.