POJ 3083 Children of the Candy Corn

来源:互联网 发布:在线视频gif制作软件 编辑:程序博客网 时间:2024/06/06 09:26

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'为终点,'#'为墙,'.'为路,有三种走法,分别求每种走法的步数。

1.先转向左,如果左边不能走就依次向右转,看是否可走。

2.先转向右,如果右边不能走就依次向左转,看是否可走。

3.求从起点到终点的最小步数。

思路:设置一个所朝的方向,1代表北,2代表东,3代表南,4代表西。(地图中是上北下南左西右东),然后用dfs分别搜索第1和第2种走法,然后用bfs搜索最短的走法

代码比较长,但第一种和第二种的走法差不多。


#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>using namespace std;int place[45][45];int dr[4][2]={{0,1}, {0,-1}, {1,0}, {-1,0}};bool vis[45][45];int w, h;bool flag;struct node{    int x, y, step;};int ansl, ansr, ans;   //记录三种走法的步数int si, sj, ei, ej;void dfsl(int x, int y, int step, int to)  //第一种走法{    if(flag)        return;    if(x==ei&&y==ej)    {        flag=true;        ansl=step;        return;    }    if(to==1)    {        if(place[x][y-1])            dfsl(x, y-1, step+1, 4);        if(place[x-1][y])            dfsl(x-1, y, step+1, 1);        if(place[x][y+1])            dfsl(x, y+1, step+1, 2);        if(place[x+1][y])            dfsl(x+1, y, step+1, 3);    }    if(to==2)    {        if(place[x-1][y])            dfsl(x-1, y, step+1, 1);        if(place[x][y+1])            dfsl(x, y+1, step+1, 2);        if(place[x+1][y])            dfsl(x+1, y, step+1, 3);        if(place[x][y-1])            dfsl(x, y-1, step+1, 4);    }    if(to==3)    {        if(place[x][y+1])            dfsl(x, y+1, step+1, 2);        if(place[x+1][y])            dfsl(x+1, y, step+1, 3);        if(place[x][y-1])            dfsl(x, y-1, step+1, 4);        if(place[x-1][y])            dfsl(x-1, y, step+1, 1);    }    if(to==4)    {        if(place[x+1][y])            dfsl(x+1, y, step+1, 3);        if(place[x][y-1])            dfsl(x, y-1, step+1, 4);        if(place[x-1][y])            dfsl(x-1, y, step+1, 1);        if(place[x][y+1])            dfsl(x, y+1, step+1, 2);    }}void dfsr(int x, int y, int step, int to)    //第二种走法{    if(flag)        return;    if(x==ei&&y==ej)    {        flag=true;        ansr=step;        return;    }    if(to==1)    {        if(place[x][y+1])            dfsr(x, y+1, step+1, 2);        if(place[x-1][y])            dfsr(x-1, y, step+1, 1);        if(place[x][y-1])            dfsr(x, y-1, step+1, 4);        if(place[x+1][y])            dfsr(x+1, y, step+1, 3);    }    if(to==2)    {        if(place[x+1][y])            dfsr(x+1, y, step+1, 3);        if(place[x][y+1])            dfsr(x, y+1, step+1, 2);        if(place[x-1][y])            dfsr(x-1, y, step+1, 1);        if(place[x][y-1])            dfsr(x, y-1, step+1, 4);    }    if(to==3)    {        if(place[x][y-1])            dfsr(x, y-1, step+1, 4);        if(place[x+1][y])            dfsr(x+1, y, step+1, 3);        if(place[x][y+1])            dfsr(x, y+1, step+1, 2);        if(place[x-1][y])            dfsr(x-1, y, step+1, 1);    }    if(to==4)    {        if(place[x-1][y])            dfsr(x-1, y, step+1, 1);        if(place[x][y-1])            dfsr(x, y-1, step+1, 4);        if(place[x+1][y])            dfsr(x+1, y, step+1, 3);        if(place[x][y+1])            dfsr(x, y+1, step+1, 2);    }}void bfs()            //第三种走法{    memset(vis, false, sizeof(vis));    queue<node> q;    node a, next;    a.x=si;    a.y=sj;    a.step=1;    q.push(a);    vis[si][sj]=true;    while(!q.empty())    {        a=q.front();        q.pop();        if(a.x==ei&&a.y==ej)        {            ans=a.step;            return;        }        for(int i=0; i<4; i++)        {            next.x=a.x+dr[i][0];            next.y=a.y+dr[i][1];            if(vis[next.x][next.y]||!place[next.x][next.y])                continue;            next.step=a.step+1;            q.push(next);            vis[next.x][next.y]=true;        }    }}int main(){    int t;    scanf("%d", &t);    getchar();    while(t--)    {        ansl=ansr=ans=0;        memset(place, 0, sizeof(place));        scanf("%d%d", &w, &h);        getchar();        char x;        for(int i=1; i<=h; i++)        {            for(int j=1; j<=w; j++)            {                scanf("%c", &x);                if(x=='S')                {                    si=i;                    sj=j;                    place[i][j]=1;                }                if(x=='E')                {                    ei=i;                    ej=j;                    place[i][j]=1;                }                if(x=='.')                    place[i][j]=1;            }            getchar();        }        flag=false;        dfsl(si, sj, 1, 1);        flag=false;        dfsr(si, sj, 1, 1);        bfs();        printf("%d %d %d\n", ansl, ansr, ans);    }    return 0;}



原创粉丝点击