poj 3083 Children of the Candy Corn

来源:互联网 发布:网络鲜花实体店 编辑:程序博客网 时间:2024/04/30 03:32
题目链接:点我,点我~

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
题目大意:

给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走

先输出左转优先时,从SE的步数

再输出右转优先时,从SE的步数

最后输出SE的最短步数

基本思路:先用dfs找出先左转或先右转的路径,在用bfs求最短路径.

注:在dfs时 方向的顺序为 先左(右)转 在直走 在右(左)转 在后退

bfs就相对好理解了

<span style="font-size:18px;">#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<queue>using namespace std;int mp[44][44];int vis[44][44];int f;int ansl,ansr,ans;int ex,ey;struct node{    int xx,yy;    int c;}q,p;queue<struct node>que;void dfsl(int x,int y,int step,int d){    if(f)return;    if(x==ex&&y==ey)    {        f=1;        ansl=step;        return;    }     if(d == 1){//先左转再直走再右转再下走        if(mp[x+1][y])    dfsl(x+1,y,step+1,4);        if(mp[x][y-1])    dfsl(x,y-1,step+1,1);        if(mp[x-1][y])    dfsl(x-1,y,step+1,2);        if(mp[x][y+1])    dfsl(x,y+1,step+1,3);    }    else if(d == 2){        if(mp[x][y-1])    dfsl(x,y-1,step+1,1);        if(mp[x-1][y])    dfsl(x-1,y,step+1,2);        if(mp[x][y+1])    dfsl(x,y+1,step+1,3);        if(mp[x+1][y])    dfsl(x+1,y,step+1,4);    }    else if(d == 3){        if(mp[x-1][y])    dfsl(x-1,y,step+1,2);        if(mp[x][y+1])    dfsl(x,y+1,step+1,3);        if(mp[x+1][y])    dfsl(x+1,y,step+1,4);        if(mp[x][y-1])    dfsl(x,y-1,step+1,1);    }    else{        if(mp[x][y+1])    dfsl(x,y+1,step+1,3);        if(mp[x+1][y])    dfsl(x+1,y,step+1,4);        if(mp[x][y-1])    dfsl(x,y-1,step+1,1);        if(mp[x-1][y])    dfsl(x-1,y,step+1,2);    }}void Dfsr(int x,int y,int step,int face){    if(f)return;    if(x==ex&&y==ey)    {        f=1;        ansr=step;        return;    }    if(face == 1){//先右转再直走再左转再下走        if(mp[x-1][y])     Dfsr(x-1,y,step+1,2);        if(mp[x][y-1])     Dfsr(x,y-1,step+1,1);        if(mp[x+1][y])     Dfsr(x+1,y,step+1,4);        if(mp[x][y+1])     Dfsr(x,y+1,step+1,3);    }    else if(face == 2){        if(mp[x][y+1])     Dfsr(x,y+1,step+1,3);        if(mp[x-1][y])     Dfsr(x-1,y,step+1,2);        if(mp[x][y-1])     Dfsr(x,y-1,step+1,1);        if(mp[x+1][y])     Dfsr(x+1,y,step+1,4);    }    else if(face == 3){        if(mp[x+1][y])     Dfsr(x+1,y,step+1,4);        if(mp[x][y+1])     Dfsr(x,y+1,step+1,3);        if(mp[x-1][y])     Dfsr(x-1,y,step+1,2);        if(mp[x][y-1])     Dfsr(x,y-1,step+1,1);    }    else{        if(mp[x][y-1])     Dfsr(x,y-1,step+1,1);        if(mp[x+1][y])     Dfsr(x+1,y,step+1,4);        if(mp[x][y+1])     Dfsr(x,y+1,step+1,3);        if(mp[x-1][y])     Dfsr(x-1,y,step+1,2);    }}int  bfs(int x,int y){    while(!que.empty())que.pop();    q.xx=x;    q.yy=y;    q.c=1;    que.push(q);    vis[x][y]=1;    while(!que.empty())    {        p=que.front();        que.pop();        if(p.xx==ex&&p.yy==ey)return p.c;        if(mp[p.xx-1][p.yy]&&vis[p.xx-1][p.yy]==0)        {            vis[p.xx-1][p.yy]=1;            q.xx=p.xx-1;            q.yy=p.yy;            q.c=p.c+1;            que.push(q);        }         if(mp[p.xx+1][p.yy]&&vis[p.xx+1][p.yy]==0)        {            vis[p.xx+1][p.yy]=1;            q.xx=p.xx+1;            q.yy=p.yy;            q.c=p.c+1;            que.push(q);        }         if(mp[p.xx][p.yy-1]&&vis[p.xx][p.yy-1]==0)        {            vis[p.xx][p.yy-1]=1;            q.xx=p.xx;            q.yy=p.yy-1;            q.c=p.c+1;            que.push(q);        }         if(mp[p.xx][p.yy+1]&&vis[p.xx][p.yy+1]==0)        {            vis[p.xx][p.yy+1]=1;            q.xx=p.xx;            q.yy=p.yy+1;            q.c=p.c+1;            que.push(q);        }    }    return 0;}int main(){    int t;    int w,h;    cin>>t;    while(t--)    {        cin>>w>>h;        memset(mp,0,sizeof(mp));        memset(vis,0,sizeof(vis));        char ch;        int sx,sy;        for(int i=1;i<=h;i++)        {            for(int j=1;j<=w;j++)            {                cin>>ch;                if(ch=='S')                {                    sx=i;                    sy=j;                    mp[i][j]=1;                }                if(ch=='E')                {                    ex=i;                    ey=j;                    mp[i][j]=1;                }                if(ch=='.')mp[i][j]=1;            }        }        int face;        if(sx-1>0&&mp[sx-1][sy]==1)face=2;        else if(sy+1<=w&&mp[sx][sy+1]==1)face=3;        else if(sx+1<=h&&mp[sx+1][sy]==1)face=4;        else if(sy-1>0&&mp[sx][sy-1]==1)face=1;        f=0;        dfsl(sx,sy,1,face);        f=0;        Dfsr(sx,sy,1,face);        ans=bfs(sx,sy);        cout<<ansl<<" "<<ansr<<" "<<ans<<endl;    }    return 0;}</span>


0 0
原创粉丝点击