POJ 3083 Children of the Candy Corn(基础题)

来源:互联网 发布:淘宝客服美女头像 编辑:程序博客网 时间:2024/06/07 08:22

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9555 Accepted: 4143

Description

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

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

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

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.

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

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is 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
思路:根据题目要求走就行了,注意相对方向
#include<cstring>#include<queue>#include<cstdio>#include<map>#include <vector>#include<string>#include <algorithm>#include <iostream>using namespace std;const int maxn=40+10;char  mapp[maxn][maxn];int vis[maxn*maxn];int c,r;int sx,sy,ex,ey;int ans1,ans2,ans3;struct point{int x,y;char direct;};/******************************/void goahead(point &q){switch(q.direct){case'S':q.x+=1;ans1++;break;case'N':q.x-=1;ans1++;break;case'E':q.y+=1;ans1++;break;case'W':q.y-=1;ans1++;break;}}bool turnright(point q){if(q.direct=='S')q.direct='W';else if(q.direct=='N')q.direct='E';else if(q.direct=='E')q.direct='S';else q.direct='N';switch(q.direct){case'S':q.x+=1;break;case'N':q.x-=1;break;case'E':q.y+=1;break;case'W':q.y-=1;break;}if(mapp[q.x][q.y]=='#')return false;elsereturn true;}void turnleft(point &q){if(q.direct=='S')q.direct='E';else if(q.direct=='N')q.direct='W';else if(q.direct=='E')q.direct='N';else q.direct='S';}bool ahead(point q){switch(q.direct){case'S':q.x+=1;break;case'N':q.x-=1;break;case'E':q.y+=1;break;case'W':q.y-=1;break;}if(mapp[q.x][q.y]=='#')return false;else return true;}void dfsright(point u){if(u.x==ex&&u.y==ey)return ;else if(turnright(u)){if(u.direct=='S')   u.direct='W';else if(u.direct=='N')u.direct='E';else if(u.direct=='E')u.direct='S';else u.direct='N';goahead(u);dfsright(u);}else if(ahead(u)){goahead(u);dfsright(u);}else {turnleft(u);dfsright(u);}}/******************************/bool left(point q){if(q.direct=='S')q.direct='E';else if(q.direct=='N')q.direct='W';else if(q.direct=='E')q.direct='N';else q.direct='S';switch(q.direct){case'S':q.x+=1;break;case'N':q.x-=1;break;case'E':q.y+=1;break;case'W':q.y-=1;break;}if(mapp[q.x][q.y]=='#')return false;elsereturn true;}void right(point &q){if(q.direct=='S')q.direct='W';else if(q.direct=='N')q.direct='E';else if(q.direct=='E')q.direct='S';else q.direct='N';}void dfsleft(point u){if(u.x==ex&&u.y==ey)return ;else if(left(u)){if(u.direct=='S')   u.direct='E';else if(u.direct=='N')u.direct='W';else if(u.direct=='E')u.direct='N';else u.direct='S';goahead(u);dfsleft(u);}else if(ahead(u)){goahead(u);dfsleft(u);}else {right(u);dfsleft(u);}}/******************************/queue<int>q;int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};int dis[maxn*maxn];void bfs(int cur){q.push(cur);vis[cur]=1;while(!q.empty()){int temp=q.front();q.pop();int x=temp/c;int y=temp%c;for(int i=0;i<4;i++){int newx=x+d[i][0];int newy=y+d[i][1];int newcur=newx*c+newy;if(newx>=0&&newx<r&&newy>=0&&newy<c&&!vis[newcur]&&mapp[newx][newy]!='#'){vis[newcur]=1;q.push(newcur);dis[newcur]=dis[temp]+1;}}}}int main(){int T;scanf("%d",&T);while(T--){scanf("%d %d",&c,&r);point ss;for(int i=0;i<r;i++){for(int j=0;j<c;j++){cin>>mapp[i][j];if(mapp[i][j]=='S'){char ch;sx=i;sy=j;if(sx==0) ch='S';else if(sy==0)ch='E';else if(sy==c-1)ch='W';else if(sx==r-1)ch='N';ss.x=i;ss.y=j;ss.direct=ch;}if(mapp[i][j]=='E'){ex=i;ey=j;}}}ans1=1;dfsright(ss);int c1=ans1;ans1=1;dfsleft(ss);int c2=ans1;memset(vis,0,sizeof(vis));    dis[sx*c+sy]=1;bfs(sx*c+sy);int c3=dis[ex*c+ey];cout<<c2<<" "<<c1<<" "<<c3<<endl;}return 0;}

0 0