POJ-3083 children of the Candy Corn (BFS+DFS)

Source: Internet
Author: User

Description

The Cornfield Maze is a popular Halloween treat. Visitors is shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, a nd other terrors on their quest to find the exit.

One popular maze-walking strategy guarantees that the visitor would eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there ' no guarantee which strategy (left or right) would be better, and the path taken are seldom the most effic Ient. (It also doesn ' t work on mazes with exits that is not on the edge; those types of mazes is not represented in this probl EM.)

As the proprieter of a cornfield that's about to being converted into a maze and you ' d like to have a computer program that can Determine the left and right-hand paths along with the shortest path so, can figure out which layout have the best Chance of confounding visitors.

Input

Input to this problem would begin with a line containing a single integer n indicating the number of mazes. Each maze would consist of one line with a width, w, and Height, h (3 <= W, H <=-Max), followed by H lines of W Charac Ters each of that represent the maze layout. Walls is represented by a hash marks (' # '), empty space by periods ('. '), the start by a ' and the exit by an ' E '.

Exactly one's and one ' E ' would be present in the maze, and they would always be located along one of the maze edges and N Ever in a corner. The maze is fully enclosed by walls (' # '), with the only openings being the ' S ' and ' E '. The ' S ' and ' E ' would also is separated by at least one wall (' # ').

Assume that the maze exit was always reachable from the start point.

Output

For each maze in the input, output to a single line the number of necessarily unique squares that a person would vis It (including ' S ' and ' E ') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another are only allowed in the horizontal or vertical direction; Movement along the diagonals is not allowed.

Sample Input

28 8#########......##.####.##.####.##.####.##.####.##...#. # #S #e### #9 5##########.#.#.# #S .... e#.#.#.#.##########

Sample Output

37 5 517) 17 9


The main topic: from the beginning to the end, the length of three roads. The first one is to always take the left priority, and four directions are traversed clockwise, until the length of the end point, the second is always on the right priority, and four directions in the counterclockwise direction, until the length of the end; The third one is the shortest way.
Problem Analysis: Obviously, the first two roads are DFS out, the third is the BFS out.

The code is as follows:
1# include<iostream>2# include<cstdio>3# include<queue>4# include<cstring>5# include<algorithm>6 using namespacestd;7 structnode8 {9     intx,y,t;TenNodeint_x,int_y,int_t): X (_x), Y (_y), T (_t) {} One }; A Charp[ -][ -]; - BOOLFlag; - intw,h,ans[2],mark[ -][ -]; the intd[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; - BOOLOkintXinty) - { -     if(x>=0&&x0&&y<W) +         return true; -     return false; + } A voidDfsintXintYintPosintTintStep) at { -     if(flag) -         return ; -     if(p[x][y]=='E'){ -flag=true; -ans[(t==1)]=Step; in         return ; -     } to     intNX,NY,NP; +      for(intI=1, pp= (pos+t+4)%4; i<=4; ++i,pp= (pp-t+4)%4){ -nx=x+d[pp][0],ny=y+d[pp][1]; the         if(OK (nx,ny) &&p[nx][ny]!='#'){ *DFS (nx,ny,pp,t,step+1); $             if(flag)Panax Notoginseng                 return ; -         } the     } + } A voidBFsintSxintSy) the { +Queue<node>Q; -memset (Mark,0,sizeof(Mark)); $mark[sx][sy]=1; $Q.push (Node (Sx,sy,1)); -      while(!q.empty ()) -     { theNode u=Q.front (); - Q.pop ();Wuyi         if(p[u.x][u.y]=='E'){ theprintf"%d\n", u.t); -             return ; Wu         } -          for(intI=0;i<4;++i) { About             intnx=u.x+d[i][0],ny=u.y+d[i][1]; $             if(OK (nx,ny) &&!mark[nx][ny]&&p[nx][ny]!='#'){ -mark[nx][ny]=1; -Q.push (Node (nx,ny,u.t+1)); -             } A         } +     } the } - intMain () $ { the     intT,sx,sy,pos; thescanf"%d",&T); the      while(t--) the     { -scanf"%d%d",&w,&h); in          for(intI=0; ii) { thescanf"%s", P[i]); the              for(intj=0; j<w;++j) About                 if(p[i][j]=='S') thesx=i,sy=J; the         } the         if(sx==0) +pos=2; -         if(sx==h-1) thepos=0;Bayi         if(sy==0) thepos=1; the         if(sy==w-1) -pos=3; -ans[0]=ans[2]=0; theflag=false; theDFS (sx,sy,pos,-1,1); theflag=false; theDFS (Sx,sy,pos,1,1); -printf"%d%d", ans[0],ans[1]); the BFS (sx,sy); the     } the     return 0;94}
View Code

After feeling: This problem is both disgusting and water!!!

POJ-3083 children of the Candy Corn (BFS+DFS)

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.