Poj 3083 Children of the Candy Corn (DFS+BFS)

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11198 Accepted: 4824

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

没啥好说的就是直接上代码。。。但我弄了大半天- -!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
char Map[100][100];
bool vis[100][100];
int cx[]={0,1,0,-1};
int cy[]={1,0,-1,0};
int n,m;
bool bj;
struct node
{
    int a,b;
    int ans;
}q[100];
void dfs(int s1,int s2,int e1,int e2,int d,int &an)
{
    an++;
    if(s1==e1&&s2==e2)//找到便标记下来以免,会返回的时候继续查找
    {
        bj=true;
        return ;
    }
    for(int i=d+3;i<d+7;i++)
    {
        int p=i%4;//注意在沿着一个方向查找的时候可以这么写for(i=d+3;i<d+7;i++)  p=i%4;并且和cx,cy顺序无关
        int x=s1+cx[p];
        int y=s2+cy[p];
        if(x>=1&&y>=1&&x<=m&&y<=n&&Map[x][y]!='#')!!!!!!!一定注意x,y分别小于什么,想想for循环外控行
        {
            dfs(x,y,e1,e2,p,an);
            if(bj)//防止返回的时候继续查找
             return ;
        }
    }
    return ;
}
void bfs(int s1,int s2,int e1,int e2)//在BFS中仍有(搜索离不开标记)
{
    queue<node>q;
    memset(vis,false,sizeof(vis));
    while(!q.empty())
        q.pop();
    node f1,f2;
    f1.a=s1;
    f1.b=s2;
    f1.ans=1;
    vis[f1.a][f1.b]=true;
    q.push(f1);
    while(!q.empty())
    {
        f1=q.front();
        q.pop();
        if(Map[f1.a][f1.b]=='E')
        {
            printf("%d\n",f1.ans);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            f2=f1;
            f2.a+=cx[i];
            f2.b+=cy[i];
            if(!vis[f2.a][f2.b]&&f2.a>=1&&f2.a<=m&&f2.b>=1&&f2.b<=n&&Map[f2.a][f2.b]!='#')
            {
                vis[f2.a][f2.b]=true;
                f2.ans++;
                q.push(f2);
            }
        }
    }
    return ;
}
int main()
{
    int cla,s1,s2,e1,e2;
    bool bj;
    scanf("%d",&cla);
    while(cla--)
    {
        scanf("%d%d",&n,&m);
        getchar();
        for(int i=1;i<=m;i++)
            scanf("%s",Map[i]+1);
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(Map[i][j]=='S')
                {
                    s1=i;s2=j;
                }
                if(Map[i][j]=='E')
                {
                    e1=i;e2=j;
                }
            }
        }
        bj=false;
        int an1=0,an2=0;
        dfs(s1,s2,e1,e2,0,an1);//在处理,从同一点不同向不同的方向找的时候,很巧妙(可以换位思考从终点向起点找,就可以调用同一函数了)。
        bj=false;
        dfs(e1,e2,s1,s2,0,an2);
        printf("%d %d ",an1,an2);
        bfs(s1,s2,e1,e2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值