您的位置:首页 > 其它

poj3083 DFS靠墙走+BFS

2017-07-17 17:09 375 查看
Children of the Candy Corn

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13926 Accepted: 6027
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 ('.'), t
4000
he 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
    题意:给出由"S""E""#""."组成的一张图。输出的第一个数是从S开始,靠着左边的墙走,总共的步数,第二个数是从E开始,靠着右边的墙走,总共的步数,第三个数是从S到E最短路步数。
    解题思路:第三个数显然可以用最基础的BFS写出来。前两个数是两个DFS。这里我们需要注意,所谓的靠着左边墙走与靠着右边的墙走,就是每次走的时候有优先方向。看了很多大牛的博客,才写出来。
例如靠着左边的墙走上一次走过来的方向是↑,那么方向顺序是←↑→↓……
列出表   ↑     ←↑→↓
       →     ↑→↓←
       ↓     →↓←↑
       ←     ↓←↑→
靠着右边走的方向顺序
列出表    ↑     →↑←↓
        →    ↓→↑←
        ↓    ←↓→↑
既然有优先方向,我就在写DFS的时候把上一次的走的方向传到下一个函数里面。在这里用了一个取模,来代替创立多个方向数组。以下是我的AC代码#include <cstdio>
#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
#define maxn 45

int flag[maxn][maxn];
char pic[maxn][maxn];
int mm[4][2]= {{0,-1},{-1,0},{0,1},{1,0}};
int w,h,ex,ey;
struct trip
{
int x;
int y;
int step;
};
int ans;
bool dfs1(int x, int y, int o, int step) {
if (pic[x][y] == 'E') {
ans = step;
return true;
}
for (int i = o; i < o + 4; ++i) {
int xx = x + mm[i%4][0];
int yy = y + mm[i%4][1];
if (pic[xx][yy] != '#') {
if (dfs1(xx, yy, (i-1+4)%4, step+1))
return true;
}
}
return false;
}
bool dfs2(int x, int y, int o, int step) {
if (pic[x][y] == 'E') {
ans = step;
return true;
}

for (int i = o + 4; i > o; --i) {
int xx = x + mm[i%4][0];
int yy = y + mm[i%4][1];

if (pic[xx][yy] != '#') {
if (dfs2(xx, yy, (i+1)%4, step+1))
return true;
}
}
return false;
}

int check(int r,int s)
{
if(r<0||r>h||s<0||s>w||pic[r][s]=='#') return 1;
return flag[r][s];
}
void bfs(int r,int s)
{
queue<trip> q;
trip a,next;
a.x=r;
a.y=s;
a.step=1;
flag[r][s]=1;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
if(a.x==ex&&a.y==ey)
{
printf("%d",a.step);
break;
}
for(int i=0; i<4; i++)
{
next.x=a.x+mm[i][0];
next.y=a.y+mm[i][1];
if(check(next.x,next.y)) continue;
next.step=a.step+1;
flag[next.x][next.y]=1;
q.push(next);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&w,&h);
memset(pic,'#',sizeof(pic));
for(int i=1; i<=h; i++)
scanf("%s",&pic[i][1]);
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++)
if(pic[i][j]=='E')
{
ex=i;
ey=j;
}
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++)
if(pic[i][j]=='S')
{
dfs1(i,j,0,1);
printf("%d ",ans);
dfs2(i,j,0,1);
printf("%d ",ans);
memset(flag,0,sizeof(flag));
bfs(i,j);
break;
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: