poj3083 DFS靠墙走+BFS
2017-07-17 17:09
375 查看
Children of the Candy Corn
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 |
解题思路:第三个数显然可以用最基础的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;
}
相关文章推荐
- poj3083 Children of the Candy Corn dfs 和 bfs 的综合,终于AC了
- POJ3083《Children of the Candy Corn》方法:DFS+BFS
- poj3083-dfs-bfs-难度等级5
- bfs+dfs(poj3083)
- poj3083 dfs+bfs一次ac
- [DFS && BFS]poj3083
- POJ3083 Children of the Candy Corn(BFS+DFS水题)
- poj3083 dfs + bfs
- POJ3083 Children of the Candy Corn(DFS+BFS)
- POJ3083 DFS+BFS
- POJ3083 Children of the Candy Corn(BFS+DFS)
- poj3083 Children of the Candy Corn【bfs+dfs】【刷题计划】
- POJ3083 Children of the Candy Corn (BFS ,DFS)
- POJ3083 Children of the Candy Corn(DFS + BFS)
- POJ3083 -- BFS和DFS
- poj3083 Children of the Candy Corn dfs 和 bfs 的综合,终于AC了
- DFS+BFS(POJ3083)
- poj3083 非常好 的入门级dfs+bfs
- FZU 1205 小鼠迷宫问题 <BFS+DFS>
- 什么时候用DFS,什么时候用BFS?(DFS和BFS的特点和异同)