POJ 3083, Children of the Candy Corn

本文介绍了一种迷宫寻路算法,通过深度优先搜索(DFS)和广度优先搜索(BFS)两种策略来解决迷宫寻路问题。具体包括左墙跟随、右墙跟随以及最短路径的求解方法,旨在帮助迷宫设计者评估不同布局对游客的挑战程度。

Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 2934  Accepted: 1418


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
South Central USA 2006


// POJ3083.cpp : Defines the entry point for the console application.
//

#include 
<iostream>
#include 
<queue>
using namespace std;

static int step[4][2= {0-1-100110};
//toward up 0, left 1, down 2, right 3
int DFS(char map[41][41], int x, int y, int w, int h, int toward, bool left)
{
    
if (map[y][x]=='E'return 1;
    
int x1, y1, tw;
    
if (left == true)
    {
        
for (int i = toward + 1; i >= toward - 2--i)
        {
            tw 
= (8 + i)&3;
            x1 
= x + step[tw][0];
            y1 
= y + step[tw][1];
            
if (x1 >= 0 && x1 < w && y1 >=0 && y1 < h && map[y1][x1] != '#'
                
break;
        };
    }
    
else
    {
        
for (int i = toward - 1; i <= toward + 2++i)
        {
            tw 
= (8 + i)&3;
            x1 
= x + step[tw][0];
            y1 
= y + step[tw][1];
            
if (x1 >= 0 && x1 < w && y1 >=0 && y1 < h && map[y1][x1] != '#'
                
break;
        };
    }
    
return 1 + DFS(map, x1, y1, w, h, tw, left);
};
int BFS(char map[41][41], int x, int y, int w, int h)
{
    
int cnt[41][41];
    memset(cnt,
0,sizeof(cnt));
    cnt[y][x] 
= 1;

    queue
<pair<int,int>> q;
    q.push(make_pair
<int,int>(x, y));

    
int x1,y1;
    
while (!q.empty())
    {
        pair
<int,int> cur = q.front();
        q.pop();
        
        
if (map[cur.second][cur.first] == 'E'return cnt[cur.second][cur.first];
        
for (int i = 0; i < 4++i)
        {
            x1 
= cur.first + step[i][0];
            y1 
= cur.second + step[i][1];
            
if (x1 >= 0 && x1 < w && y1 >=0 && y1 < h && map[y1][x1] != '#' && cnt[y1][x1] == 0)
            {
                cnt[y1][x1] 
= cnt[cur.second][cur.first] + 1;
                q.push(make_pair
<int,int>(x1,y1));
            }
        }
    }
    
return 0;
}
int main(int argc, char* argv[])
{
    
char map[41][41];
    
int cases;
    cin 
>> cases;

    
for (int c = 0; c < cases; ++c)
    {
        
int w,h;
        scanf(
"%d %d\n"&w, &h);
        
for (int i = 0; i < h; ++i)
            gets(map[i]);

        
int x, y;
        
for (int i = 0; i < h; ++i)
        {
            x 
= distance(&map[i][0],std::find(&map[i][0],&map[i][w],'S'));
            
if (x < w)
            {
                y 
= i;
                
break;
            }
        }

        
int toward = 0;
        
if (x == w - 1) toward = 1;
        
else if (y == 0) toward = 2;
        
else if (x == 0) toward = 3;
        cout 
<< DFS(map, x, y, w, h, toward, true<< " "
    << DFS(map, x, y, w, h, toward, false<< " " << BFS(map, x, y, w, h) << endl;
    }
    
return 0;
}

转载于:https://www.cnblogs.com/asuran/archive/2009/10/10/1580104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值