Children of the Candy Corn POJ 3083(bfs)

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
Source

South Central USA 2006

题意 :从S到E 一定有路存在 S好像也必定靠着墙 输出靠左走要几步 靠着右走要几步 和最短步数


#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int mapp[50][50];
int v[50][50];
int x2,y2;
int m,n;
int go(int x,int y)
{
    if(0<=x&&x<n&&0<=y&&y<m)
        return 1;
    return 0;
}
int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
struct node
{
    int x,y,time;
    int flag;//是人当前面朝方向
//flag 
//0 右
//1 下
//2 左
//3 上
};
int bfs()//求最短步数
{
    memset(v,0,sizeof(v));
    queue<node>q;
    node st,ed;
    st.x=x2;
    st.y=y2;
    st.time=1;
    q.push(st);
    v[x2][y2]=1;
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        if(mapp[st.x][st.y]=='E')
        {
            return st.time;
        }
        for(int i=0; i<4; i++)
        {
            ed.x=st.x+dir[i][0];
            ed.y=st.y+dir[i][1];
            if(go(ed.x,ed.y)&&!v[ed.x][ed.y]&&mapp[ed.x][ed.y]!='#')
            {
                ed.time=st.time+1;
                v[ed.x][ed.y]=1;
                q.push(ed);
            }
        }
    }
    return -1;
}
int bfs1()//左
{
    queue<node>q;
    node st,ed;
    st.x=x2;
    st.y=y2;
    st.time=1;
    st.flag=1;
    q.push(st);
    while(!q.empty())
    {
       st=q.front();
        q.pop();
       if(mapp[st.x][st.y]=='E')
        return st.time;
       ed.flag=(st.flag-1+4)%4;//从左方向开始 
       for(int i=0;i<4;i++)
       {
           ed.x=st.x+dir[(ed.flag+i)%4][0];
           ed.y=st.y+dir[(ed.flag+i)%4][1];
           if(go(ed.x,ed.y)&&mapp[ed.x][ed.y]!='#')
           {
               ed.time=st.time+1;
               ed.flag=(ed.flag+i)%4;
               q.push(ed);
               break;
           }
       }
    }
}
int bfs2()
{
    queue<node>q;
    node st,ed;
    st.x=x2;
    st.y=y2;
    st.time=1;
    st.flag=1;
    q.push(st);
    while(!q.empty())
    {
       st=q.front();
        q.pop();
       if(mapp[st.x][st.y]=='E')
        return st.time;
       ed.flag=(st.flag+1)%4;
       for(int i=0;i<4;i++)
       {
           ed.x=st.x+dir[(ed.flag-i+4)%4][0];
           ed.y=st.y+dir[(ed.flag-i+4)%4][1];
           if(go(ed.x,ed.y)&&mapp[ed.x][ed.y]!='#')
           {
               ed.time=st.time+1;
               ed.flag=(ed.flag-i+4)%4;
               q.push(ed);
               break;
           }
       }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0; i<n; i++)
        {
            getchar();
            for(int j=0; j<m; j++)
            {
                scanf("%c",&mapp[i][j]);
                if(mapp[i][j]=='S')
                {
                    x2=i,y2=j;
                }
            }
        }
        int p3=bfs();
        int p1=bfs1();
        int p2=bfs2();
        printf("%d %d %d\n",p1,p2,p3);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值