POJ 3083Children of the Candy Corn

Posted -凡-尘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3083Children of the Candy Corn相关的知识,希望对你有一定的参考价值。

 

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9
/*
这题感开始我才用dfs做,但回溯不清楚,看了网上是用bfs,给我了想法
右:0
下:1
左:2
上:3
比如:计算向左转的时,此时方向是2,就应该这样循环1,2,3,0。
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int t,n,m,sx,sy,tx,ty,dir,ans,ansleft,ansright;
char mp[45][45];
 struct node2
    {
        int x,y;
        node2(int a,int b){x=a; y=b;}
    };
struct node
{
    int x,y,d;
    node(int a,int b,int c){x=a; y=b; d=c;}
};
int dr[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//ср:0,об:1ё╛вС:2ё╛ио:3
void righthand(int k)
{
    ansright=1;
    queue<node> Q;
    Q.push(node(sx,sy,k));
    while(1)
    {
        node p=Q.front();
        Q.pop();
        if (p.x==tx && p.y==ty) return;
        p.d=(p.d+1)%4;
        for(int i=0;i<4;i++)
        {
            int xx=p.x+dr[(p.d-i+4)%4][0];
            int yy=p.y+dr[(p.d-i+4)%4][1];
            if(xx>=0 && xx<n && yy>=0 && yy<m && mp[xx][yy]!=#)
            {
                Q.push(node(xx,yy,(p.d-i+4)%4));
                ansright++;
                break;
            }
        }
    }
}

void lefthand(int k)
{
    ansleft=1;
    queue<node> Q;
    Q.push(node(sx,sy,k));
    while(1)
    {
        node p=Q.front();
        Q.pop();
        if (p.x==tx && p.y==ty) return;
        p.d=(p.d-1+4)%4;
        for(int i=0;i<4;i++)
        {
            int xx=p.x+dr[(p.d+i)%4][0];
            int yy=p.y+dr[(p.d+i)%4][1];
            if(xx>=0 && xx<n && yy>=0 && yy<m && mp[xx][yy]!=#)
            {
                Q.push(node(xx,yy,(p.d+i)%4));
                ansleft++;
                break;
            }
        }
    }
}

void bfs()
{
    queue<node2> Q;
    int vis[45][45];
    for(int i=0;i<n;i++)
      for(int j=0;j<m;j++)
        vis[i][j]=0;
         vis[sx][sy]=1;
    Q.push(node2(sx,sy));
    while(!Q.empty())
    {
        node2 p=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=p.x+dr[i][0];
            int yy=p.y+dr[i][1];
            if (xx<0 || yy<0 || xx>=n || yy>=m || mp[xx][yy]==#) continue;
            if (vis[xx][yy]>0) continue;
             vis[xx][yy]=vis[p.x][p.y]+1;
             Q.push(node2(xx,yy));
             if (xx==tx && yy==ty) {ans=vis[xx][yy];return;}
        }
    }
    ans=vis[tx][ty];
    return;
}

int main()
{
    scanf("%d",&t);
    for(;t>0;t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",&mp[i]);
            for(int j=0;j<m;j++)
                if (mp[i][j]==S) sx=i,sy=j;
                 else if (mp[i][j]==E) tx=i,ty=j;
        }
        if (sx==0) dir=1;
         else if (sx==n-1) dir=3;
           else if (sy==0) dir=0;
             else if (sy==m-1) dir=2;
        bfs();
        //printf("%d\n",ans);
        righthand(dir);
        //printf("%d\n",ansright);
        lefthand(dir);
        //printf("%d\n",ansleft);

        printf("%d %d %d\n",ansleft,ansright,ans);
    }
    return 0;
}

 

以上是关于POJ 3083Children of the Candy Corn的主要内容,如果未能解决你的问题,请参考以下文章

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

The Children of Redux

DFS plus BFS之“Children of the Candy Corn”

POJ 3498 March of the Penguins(网络流+枚举)

POJ 2553 The Bottom of a Graph

POJ2699The Maximum Number of Strong Kings