poj 3083 bfs+dfs

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11537 Accepted: 4958
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

本题主要坑点在对状态的确定,沿着左走和沿着右走时,下一步方向的确定和当前的位置以及上一步的位置有关。找到走的路径,模拟搜索即可,最短路径就直接bfs确定。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

typedef struct{
    int x,y;
    int h;
}Po;

char G[50][50];
int n,m;
Po move[] = {-1,0,0, 1,0,0, 0,-1,0, 0,1,0};
Po moveL1[] = {0,-1,0, -1,0,0, 0,1,0};
Po moveL2[] = {1,0,0, 0,-1,0, -1,0,0};
Po moveL3[] = {0,1,0, 1,0,0, 0,-1,0};
Po moveL4[] = {-1,0,0, 0,1,0, 1,0,0};

Po moveR1[] = {0,1,0, -1,0,0, 0,-1,0};
Po moveR2[] = {-1,0,0, 0,-1,0, 1,0,0};
Po moveR3[] = {0,-1,0, 1,0,0, 0,1,0};
Po moveR4[] = {1,0,0, 0,1,0, -1,0,0};

bool IsTrue(Po a){
    int x = a.x,y = a.y;
    if(x>=0&&x<n&&y>=0&&y<m){
        if(G[x][y] == '.'||G[x][y] == 'E')
            return true;
    }
    return false;
}

Po* way(int x,int y,int LR){
    if(LR == 0){
        if(x == -1&&y == 0) return moveL1;
        if(x == 0&&y == -1) return moveL2;
        if(x == 1&&y == 0) return moveL3;
        if(x == 0&&y == 1) return moveL4;
    }
    if(LR == 1){
        if(x == -1&&y == 0) return moveR1;
        if(x == 0&&y == -1) return moveR2;
        if(x == 1&&y == 0) return moveR3;
        if(x == 0&&y == 1) return moveR4;
    }
}

int bfs(Po S,Po E){
    int i,j;
    queue<Po>q;
    q.push(S);
    while(!q.empty()){
        Po te = q.front();
        q.pop();
        for(i = 0;i<4;i++){
            Po term;
            term.x = te.x + move[i].x;
            term.y = te.y + move[i].y;
            term.h = te.h + 1;
            if(IsTrue(term)){
                q.push(term);
                if(G[term.x][term.y] == 'E') return term.h;
                G[term.x][term.y] = '#';
            }
        }
    }
    return -1;
}

int dfs(Po S,Po per,int LR){
    while(1){
        if(G[S.x][S.y] == 'E') return S.h;
        Po* MoveW = way(S.x - per.x,S.y - per.y,LR);
        int i;
        Po term;
        int ok = 0;
        for(i = 0;i<3;i++){
            term = S;
            term.x += MoveW[i].x;
            term.y += MoveW[i].y;
            if(IsTrue(term)){
                ok = 1;
                break;
            }
        }
        term.h++;
        if(ok == 0){
            per.h+=2;
            term = S;
            S = per;
            per = term;
        }
        else{
            per = S;
            S = term;
        }
    }
}

int main()
{
//    freopen("data.in","r",stdin);
//    freopen("out1.txt","w",stdout);
    int N;
    scanf("%d",&N);
    while(N--){
        scanf("%d%d",&m,&n);
        int i,j;
        for(i = 0;i<n;i++) scanf("%s",G[i]);
        getchar();
        Po S,E;
        S.x = E.x = -1;
        for(i = 0;i<n;i++)
            for(j = 0;j<m;j++){
                if('S' == G[i][j]){
                    S.x = i;
                    S.y = j;
                }
                if('E' == G[i][j]){
                    E.x = i;
                    E.y = j;
                }
            }
        S.h = 1;
        Po per = S;
        if(S.x == 0) S.x++;
        else if(S.x == n-1) S.x--;
        else if(S.y == 0) S.y++;
        else if(S.y == m-1) S.y--;
        S.h++;
        int LL = dfs(S,per,0);
        int RR = dfs(S,per,1);
        S = per;
        int smin = bfs(S,E);
        printf("%d %d %d\n",LL,RR,smin);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值