POJ3083 Children of the Candy Corn (BFS ,DFS)

Children of the Candy Corn

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 6
Problem 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
 

Source
PKU

这道题就是走迷宫问题,基本是用模板,新意在总是朝左或右走,所以加了一个face记录当前脸部朝向。代码很长,但其实写出一部分后,剩下的只是复制过去稍作修改。
开始把题意搞错了,后来又耗时不少,再加上同时用到了dfs和bfs,所以整理了下。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;


int map[50][50];
bool vi[50][50];
int sx,sy,ex,ey;
int flag;
int numl,numr,num;
struct node //结构体保存数据
{
    int x,y,step,face;
}m[2000];
queue<node>q;//队列


void dfsl(int x,int y,int step,int face)//深搜1
{
    if(flag==1) return;
    if(x==ex&&y==ey){flag=1;numl=step;return;}
    if(face==1)
    {
        if(map[x+1][y]) dfsl(x+1,y,step+1,4);
        //相较当前脸部朝向,为向左走
        if(map[x][y-1]) dfsl(x,y-1,step+1,1);//向前
        if(map[x-1][y]) dfsl(x-1,y,step+1,2);//向右
        if(map[x][y+1]) dfsl(x,y+1,step+1,3);//向后
    }
    else if(face == 2)
    {
        if(map[x][y-1])    dfsl(x,y-1,step+1,1);
        if(map[x-1][y])    dfsl(x-1,y,step+1,2);
        if(map[x][y+1])    dfsl(x,y+1,step+1,3);
        if(map[x+1][y])    dfsl(x+1,y,step+1,4);
    }
    else if(face == 3)
    {
        if(map[x-1][y])    dfsl(x-1,y,step+1,2);
        if(map[x][y+1])    dfsl(x,y+1,step+1,3);
        if(map[x+1][y])    dfsl(x+1,y,step+1,4);
        if(map[x][y-1])    dfsl(x,y-1,step+1,1);
    }
    else
    {
        if(map[x][y+1])    dfsl(x,y+1,step+1,3);
        if(map[x+1][y])    dfsl(x+1,y,step+1,4);
        if(map[x][y-1])    dfsl(x,y-1,step+1,1);
        if(map[x-1][y])    dfsl(x-1,y,step+1,2);
    }
}


void dfsr(int x,int y,int step,int face)//深搜2
{
    if(flag==1) return;
    if(x==ex&&y==ey){flag=1;numr=step;return;}
    if(face==1)
    {
        if(map[x-1][y])     dfsr(x-1,y,step+1,2);
        if(map[x][y-1])     dfsr(x,y-1,step+1,1);
        if(map[x+1][y])     dfsr(x+1,y,step+1,4);
        if(map[x][y+1])     dfsr(x,y+1,step+1,3);
    }
    else if(face == 2)
    {
        if(map[x][y+1])     dfsr(x,y+1,step+1,3);
        if(map[x-1][y])     dfsr(x-1,y,step+1,2);
        if(map[x][y-1])     dfsr(x,y-1,step+1,1);
        if(map[x+1][y])     dfsr(x+1,y,step+1,4);
    }
    else if(face == 3)
    {
        if(map[x+1][y])     dfsr(x+1,y,step+1,4);
        if(map[x][y+1])     dfsr(x,y+1,step+1,3);
        if(map[x-1][y])     dfsr(x-1,y,step+1,2);
        if(map[x][y-1])     dfsr(x,y-1,step+1,1);
    }
    else
    {
        if(map[x][y-1])     dfsr(x,y-1,step+1,1);
        if(map[x+1][y])     dfsr(x+1,y,step+1,4);
        if(map[x][y+1])     dfsr(x,y+1,step+1,3);
        if(map[x-1][y])     dfsr(x-1,y,step+1,2);
    }
}
void bfs()//广搜
{
    int x,y,step;
    int count=1;//记录在队列中的位置
    memset(vi,0,sizeof(vi));
    m[0].x=sx;m[0].y=sy;m[0].step=1;
    vi[0][0]=1;
    q.push(m[0]);//入队
    while(!q.empty())//队列非空
    {
        x=q.front().x;y=q.front().y;step=q.front().step;
        q.pop();//扩展队首结点
        if(x==ex&&y==ey){num=step;return;}
        if(!vi[x-1][y]&&map[x-1][y])
        {
            vi[x-1][y]=1;
            m[count].x=x-1;
            m[count].y=y;
            m[count].step=step+1;
            q.push(m[count]);//入队
            count++;//队尾指针后移
        }
        if(!vi[x+1][y] && map[x+1][y])
        {
            vi[x+1][y] = 1;
            m[count].x = x+1;
            m[count].y = y;
            m[count].step = step + 1;
            q.push(m[count]);
            count++;
        }
        if(!vi[x][y-1] && map[x][y-1])
        {
            vi[x][y-1] = 1;
            m[count].x = x;
            m[count].y = y-1;
            m[count].step = step + 1;
            q.push(m[count]);
            count++;
        }
        if(!vi[x][y+1] && map[x][y+1])
        {
            vi[x][y+1] = 1;
            m[count].x = x;
            m[count].y = y+1;
            m[count].step = step + 1;
            q.push(m[count]);
            count++;
        }
    }
}


int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int i,j,w,h;char s[50];
        while(!q.empty()) q.pop();
        memset(map,0,sizeof(map));
        scanf("%d%d",&w,&h);
        for(i=1;i<=h;i++)
        {
            scanf("%s",&s);
            for(j=0;j<w;j++)
            {
                if(s[j]=='.') map[i][j+1]=1;
                if(s[j]=='S'){map[i][j+1]=1;sx=i;sy=j+1;}
                if(s[j]=='E'){map[i][j+1]=1;ex=i;ey=j+1;}
            }
        }
        flag=0;
        dfsl(sx,sy,1,1);
        flag=0;
        dfsr(sx,sy,1,1);
        bfs();
        printf("%d %d %d\n",numl,numr,num);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值