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

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

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



题意:有一个迷宫,出口和入口都在迷宫边缘处不为角落,S为入口,E为出口,求S到E的三种走法所需要的步数

1、贴着左侧的墙走

2、贴着右侧的墙走

3、S到E的最短路程



第三问十分简单,就是直接bfs搜索就可以了

贴墙左侧走就是左侧可以走就走,左侧不能走就直走,直走不行就右转,再不行就向后走,右侧同理就是和左侧的左右互换一下就可以了,两次dfs就可以了,因为左右只有互换一下所以dfs也可以公用,只用再加个参数标记一下就行了


因为刚写了POJ - 1835 宇航员的关系,一想到就是直接建立目前的方向,然后更新


刚开始题目看成是只能左转右转,后来才发现是贴左墙右墙睡觉


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int N = 45;
int T,n,m,sx,sy,vis[N][N],ans[5];
char mp[N][N];
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
struct node
{
    int x,y,step;
    node(int a,int b,int c){
        x = a; y = b; step = c;
    }
};
bool check(int x,int y){
    if(x<0||x>=n||y<0||y>=m) return false;
    return true;
}
void bfs(){
    memset(vis,0,sizeof vis);
    queue<node>q;
    q.push(node(sx, sy, 1));
    vis[sx][sy] = 1;
    while(!q.empty()){
        node p = q.front();q.pop();
        if(mp[p.x][p.y]=='E') {
            ans[3] = p.step;
            break;
        }
        for(int i=0;i<4;i++){
            int x = p.x + dir[i][0];
            int y = p.y + dir[i][1];
            if(!check(x,y)||vis[x][y]||mp[x][y]=='#') continue;
            q.push(node(x, y, p.step+1));
            vis[x][y] = 1;
        }
    }
    while(!q.empty()) q.pop();
}
void dfs(int x,int y,int front,int back,int lt,int rt,int step,int num){
    if((num==1&&ans[num])||(num==2&&ans[num])) return;
    if(mp[x][y]=='E'){
        ans[num] = step;
        return;
    }
    int X = x + dir[lt][0];
    int Y = y + dir[lt][1];
    if(check(X,Y)&&mp[X][Y]!='#'){
        dfs(X, Y, lt, rt, back, front, step+1, num);
    }
    X = x + dir[front][0];
    Y = y + dir[front][1];
    if(check(X,Y)&&mp[X][Y]!='#'){
        dfs(X, Y, front, back, lt, rt, step+1, num);
    }
    X = x + dir[rt][0];
    Y = y + dir[rt][1];
    if(check(X,Y)&&mp[X][Y]!='#'){
        dfs(X, Y, rt, lt, front, back, step+1, num);
    }
    X = x + dir[back][0];
    Y = y + dir[back][1];
    if(check(X,Y)&&mp[X][Y]!='#'){
        dfs(X, Y, back, front, rt, lt, step+1, num);
    }
}
int main()
{
    for(scanf("%d",&T);T--;){
        scanf("%d%d",&m,&n);
        ans[1] = ans[2] = ans[3] = 0;
        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;
                }
            }
        }
        bfs();
        int front,back,lt,rt;
        if(sx == 0){
            front = 1; back = 3; lt = 0; rt = 2;
        }else if(sx == n-1){
            front = 3; back = 1; lt = 2; rt = 0;
        }else if(sy == 0){
            front = 0; back = 2; lt = 3; rt = 1;
        }else{
            front = 2; back = 0; lt = 1; rt = 3;
        }
        dfs(sx, sy, front, back, lt, rt, 1, 1);
        dfs(sx, sy, front, back, rt, lt, 1, 2);
        printf("%d %d %d\n",ans[1],ans[2],ans[3]);
    }
    return 0;
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值