POJ 3083 (bfs + dfs)

20 篇文章 3 订阅
7 篇文章 2 订阅

Children of the Candy Corn

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 13904Accepted: 6014

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

题意:

有一个迷宫,从S出发到E。现在让求出优先级是左的步数和优先级是右的步数和最短路。分别输出三个答案既可。

思路:

求出步数需要用到dfs,求出最短路需要用到bfs,但是关键是管理优先级的问题。

当左优先的时候

​ 面向 转向顺序

那么我们如何用一个dfs表示每次转向的时候记录面向呢?其实根据表格我们可以看出其实这是有规律的,我们可以只用第一行表示就行了。其转向顺序和和下表有关系,我们看((d + 3)%4 + i)%4其中(d + 3)%4表示下标向后移动一个下标,而i是按照顺序继续相加。那么很明显当面向上的时候d = 1,转向之后是面向右d应该是 0,这样移动一个下标后从下标3开始进行转向。

当右优先的时候类似


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

using namespace std;

const int maxn = 50;

class step
{
public:
    int x,y;
    int cnt;
};

int n,m;
char str[maxn][maxn];
int ex,ey,sx,sy;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int lto[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
int rto[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};

int dfs(int x,int y,int num,int d,int dir[][2])
{
    for(int i = 0;i < 4; i++) {
        int to = ((d+3)%4 + i)%4;
        int tox = x + dir[to][0];
        int toy = y + dir[to][1];
        if(tox == ex && toy == ey) return num + 1;
        if(tox < 0 || tox >= n || toy < 0 || toy >= m) continue;
        if(str[tox][toy] == '#') continue;
        return dfs(tox,toy,num+1,to,dir);
    }
}

int bfs(int x,int y)
{
    int vis[maxn][maxn];
    memset(vis,0,sizeof(vis));
    step now,next;
    now.x = sx;
    now.y = sy;
    now.cnt = 1;
    vis[sx][sy] = true;
    queue<step> q;
    q.push(now);
    while(!q.empty()) {
        now = q.front();
        q.pop();
        if(now.x == ex && now.y == ey) return now.cnt;
        for(int i = 0;i < 4; i++) {
            next.x = dx[i] + now.x;
            next.y = dy[i] + now.y;
            next.cnt = now.cnt + 1;
            if(next.x < 0 || next.x >= n || next.y < 0 || next.y >= m) continue;
            if(vis[next.x][next.y]) continue;
            if(str[next.x][next.y] != '#') {
                vis[next.x][next.y] = true;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    //freopen("in.txt","r",stdin);

    int ncase;
    scanf("%d",&ncase);
    while(ncase--) {

        memset(str,0,sizeof(str));
        scanf("%d%d",&m,&n);
        for(int i = 0;i < n; i++) {
            scanf("%s",str[i]);
            for(int j = 0;j < m; j++) {
                if(str[i][j] == 'S') {
                    sx = i;
                    sy = j;
                }
                if(str[i][j] == 'E') {
                    ex = i;
                    ey = j;
                }
            }
        }
        int d1,d2;
        if(sx == 0)     {d1 = 3;d2 = 3;};
        if(sx == n-1)   {d1 = 1;d2 = 1;};
        if(sy == 0)     {d1 = 2;d2 = 0;};
        if(sy == m-1)   {d1 = 0;d2 = 2;};
        printf("%d ",dfs(sx,sy,1,d1,lto));
        printf("%d ",dfs(sx,sy,1,d2,rto));
        printf("%d\n",bfs(sx,sy));
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值