Poj3083--dfs+bfs Comprehensive Questions

Source: Internet
Author: User

POJ 3083 dfs+bfs+ Analog Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10564 Accepted: 4539

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

Test instructions: From the beginning of the maze to the end of the E, the number of steps along the left side of the wall, the number of steps along the right wall, the shortest path of the number of steps;
Idea: the shortest-circuit direct BFS, along the Wall Walk dfs+ simulation, each time the recursive record direction and change the direction of the next moment to calculate, in the direction of clockwise (or counterclockwise) walk, pay attention to the surplus;
Difficulty: Simulation ... For 3 hours ...
BFS also forgot to change vis to the result of the mle .... The simulation also reversed the left and right direction ...
The only consolation is that 0 seconds have gone by ...
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstdlib>#include<cstring>#include<algorithm>#include<map>#include<vector>#include<queue>using namespacestd;Const intmaxn= +;intn,m;CharCH[MAXN][MAXN];BOOLVIS[MAXN][MAXN];intSI,SJ;intEi,ej;intstepl,stepr,steps;structnode{intx, y;};intdx[]={0,1,0,-1};intdy[]={-1,0,1,0};BOOLFlag;voidBFsintSiintSJ) {Queue<node>Q;    Q.push ({SI,SJ}); intDIST[MAXN][MAXN]; memset (Dist,0,sizeof(Dist)); DIST[SI][SJ]=1; VIS[SI][SJ]=1;  while(!Q.empty ()) {Node now=Q.front ();        Q.pop ();  for(intI=0;i<4; i++){            intnx=now.x+dx[i],ny=now.y+Dy[i]; if(vis[nx][ny]| | ch[nx][ny]=='#')Continue; Vis[nx][ny]=1; Dist[nx][ny]=dist[now.x][now.y]+1; if(ch[nx][ny]=='E') {StepS=Dist[nx][ny]; return;        } q.push ({nx,ny}); }    }}voidDfsintXintYCharEndintDirintStepBOOLtag) {    if(flag)return; if(ch[x][y]==end) {        if(tag) stepl=step; ElseStepr=step; Flag=1;return; }     for(intI=0;i<4; i++){        intnx=x+dx[(dir+i)%4],ny=y+dy[(dir+i)%4]; intNdir= (dir+i+3)%4; if(ch[nx][ny]=='#')Continue; DFS (Nx,ny,end,ndir,step+1, tag); }}intMain () {intT;cin>>T;  while(t--) {cin>>m>>N; Stepl=stepr=steps=0; memset (CH,'#',sizeof(CH));  for(intI=1; i<=n;i++){             for(intj=1; j<=m;j++) {cin>>Ch[i][j]; if(ch[i][j]=='S') {Si=i;sj=J; }                if(ch[i][j]=='E') {ei=i;ej=J; } }} flag=0; DFS (SI,SJ,'E',0,1,1); Flag=0; DFS (Ei,ej,'S',0,1,0); memset (Vis,0,sizeof(VIS));        BFS (SI,SJ); cout<<stepR<<" "<<stepL<<" "<<stepS<<Endl; }    return 0;}
poj3083

Poj3083--dfs+bfs Comprehensive Questions

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.