Children of the Candy Corn-(bfs+dfs)

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'. <br> <br>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 ('#'). <br> <br>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

题意:

题目较长,意思就是找出从起点到终点的贴着左墙走的步数、贴着右墙走的步数和最短步数

解题思路:

其实题目与经典的深搜差不多,这里就是要用face记录当前方向,对于不同方向,按不同的顺序走,就可以

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
using namespace std;
int col,row,sx,sy,ex,ey,lcnt,rcnt,cnt;
bool a[50][50];
bool ok[50][50];
bool flag;
void dfsl(int x,int y,int step,int face)  //x,y是当前点,step是步数,face是方向
{                                         //1是往左,2是往上,3是往右,4是往下,这里的方向是对于整个棋盘而言的
    if(flag) return;
    if(x==ex&&y==ey)
    {
        flag=true;
        lcnt=step;
        return;
    }
    if(face==1)  //从右边过来的,下面是墙,方向优先度:下左上右
    {
        if(a[x+1][y]) dfsl(x+1,y,step+1,4);
        if(a[x][y-1]) dfsl(x,y-1,step+1,1);
        if(a[x-1][y]) dfsl(x-1,y,step+1,2);
        if(a[x][y+1]) dfsl(x,y+1,step+1,3);
    }
    else if(face==2) //从下边过来的,左面是墙,方向优先度:左上右下
    {
        if(a[x][y-1]) dfsl(x,y-1,step+1,1);
        if(a[x-1][y]) dfsl(x-1,y,step+1,2);
        if(a[x][y+1]) dfsl(x,y+1,step+1,3);
        if(a[x+1][y]) dfsl(x+1,y,step+1,4);
    }
    else if(face==3) //从左边过来的,上面是墙,方向优先度:上右下左
    {
        if(a[x-1][y]) dfsl(x-1,y,step+1,2);
        if(a[x][y+1]) dfsl(x,y+1,step+1,3);
        if(a[x+1][y]) dfsl(x+1,y,step+1,4);
        if(a[x][y-1]) dfsl(x,y-1,step+1,1);
    }
    else    //从上边过来的,右面是墙,方向优先度:右下左上
    {
        if(a[x][y+1]) dfsl(x,y+1,step+1,3);
        if(a[x+1][y]) dfsl(x+1,y,step+1,4);
        if(a[x][y-1]) dfsl(x,y-1,step+1,1);
        if(a[x-1][y]) dfsl(x-1,y,step+1,2);
    }
}
void dfsr(int x,int y,int step,int face)  //x,y是当前点,step是步数,face是方向
{                                         //1是往左,2是往上,3是往右,4是往下,这里的方向是对于整个棋盘而言的
    if(flag) return;
    if(x==ex&&y==ey)
    {
        flag=true;
        rcnt=step;
        return;
    }
    if(face==1)  //从右边过来的,上面是墙,方向优先度:上左下右
    {
        if(a[x-1][y]) dfsr(x-1,y,step+1,2);
        if(a[x][y-1]) dfsr(x,y-1,step+1,1);
        if(a[x+1][y]) dfsr(x+1,y,step+1,4);
        if(a[x][y+1]) dfsr(x,y+1,step+1,3);
    }
    else if(face==2) //从下边过来的,右面是墙,方向优先度:右上左下
    {
        if(a[x][y+1]) dfsr(x,y+1,step+1,3);
        if(a[x-1][y]) dfsr(x-1,y,step+1,2);
        if(a[x][y-1]) dfsr(x,y-1,step+1,1);
        if(a[x+1][y]) dfsr(x+1,y,step+1,4);
    }
    else if(face==3) //从左边过来的,下面是墙,方向优先度:下右上左
    {
        if(a[x+1][y]) dfsr(x+1,y,step+1,4);
        if(a[x][y+1]) dfsr(x,y+1,step+1,3);
        if(a[x-1][y]) dfsr(x-1,y,step+1,2);
        if(a[x][y-1]) dfsr(x,y-1,step+1,1);
    }
    else    //从上边过来的,左面是墙,方向优先度:左下右上
    {
        if(a[x][y-1]) dfsr(x,y-1,step+1,1);
        if(a[x+1][y]) dfsr(x+1,y,step+1,4);
        if(a[x][y+1]) dfsr(x,y+1,step+1,3);
        if(a[x-1][y]) dfsr(x-1,y,step+1,2);
    }
}
struct node {
    int x,y,s;
};
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
void bfs()
{
    queue<node> Q;
    node t1,t2;
    t1.x=sx; t1.y=sy; t1.s=1;
    ok[sx][sy]=true;
    Q.push(t1);
    while(!Q.empty())
    {
        t1=Q.front();
        Q.pop();
        if(t1.x==ex&&t1.y==ey) {cnt=t1.s; return;}
        for(int k=0;k<4;k++)
        {
            t2.x=t1.x+dx[k];
            t2.y=t1.y+dy[k];
            if(t2.x<=row&&t2.x>=1&&t2.y>=1&&t2.y<=col)
            {
                if(a[t2.x][t2.y]&&ok[t2.x][t2.y]==false)
                {
                    ok[t2.x][t2.y]=true;
                    t2.s=t1.s+1;
                    Q.push(t2);
                }
            }
        }
    }
}
int main()
{
    int t,i,j;
    char ch;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&col,&row);
        memset(a,false,sizeof(a));
        for(i=1;i<=row;i++)
            for(j=1;j<=col;j++)
        {
            cin>>ch;
            if(ch=='S')
            {a[i][j]=true; sx=i; sy=j;}
            else if(ch=='E')
            {a[i][j]=true; ex=i; ey=j;}
            else if(ch=='.')
            {a[i][j]=true;}
        }
        lcnt=rcnt=cnt=0;
        memset(ok,false,sizeof(ok));
        flag=false;
        dfsl(sx,sy,1,1);
        flag=false;
        dfsr(sx,sy,1,1);
        bfs();
        printf("%d %d %d\n",lcnt,rcnt,cnt);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值