poj-3083 Children of the Candy Corn-DFS+BFS
2015-08-01 14:53
549 查看
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
Sample Output
题意是从S开始,沿左边的墙走,沿右边的墙走来找E和直接找到E,输出沿左走沿右走和直接以最短路径到达E所需的步数。该题需要用DFS和BFS相结合,BFS可以方便快速的直接找到E,DFS可以遍历所有沿墙走的情况并最后到达E。
注意:DFS时需要控制走的方向,以向左走为例,每个点处优先查看这个点所朝方向的左边(例如点朝向上,那么就先看左边的点。点朝向右,就先看上边的点。因为沿左边走向左优先)然后沿顺时针查看其他方向。
沿右走本人是以E为起点沿左走找S,这样可以避免写两个DFS。
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和直接找到E,输出沿左走沿右走和直接以最短路径到达E所需的步数。该题需要用DFS和BFS相结合,BFS可以方便快速的直接找到E,DFS可以遍历所有沿墙走的情况并最后到达E。
注意:DFS时需要控制走的方向,以向左走为例,每个点处优先查看这个点所朝方向的左边(例如点朝向上,那么就先看左边的点。点朝向右,就先看上边的点。因为沿左边走向左优先)然后沿顺时针查看其他方向。
沿右走本人是以E为起点沿左走找S,这样可以避免写两个DFS。
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #define INF 0x3f3f3f3f using namespace std; char map[255][255]; int d[][2]={{-1,0},{0,1},{1,0},{0,-1}}; int ans,ansl,ansr,anss; int xs,ys,xe,ye,w,h; int flag; int way; struct node { int x,y,step; }next,now,que[10000]; int vis[255][255]; void DFS(int x,int y,int step) { int fx,fy,i; for (i=1;i<=4;i++) { if(flag) return; if(i==1) way+=3; else way++; if(way>4) way %= 4; fx = x+d[way-1][0]; fy = y+d[way-1][1]; if(fx<0||fy<0||fx>=h||fy>=w) continue; if(fx==xe&&fy==ye) {ans=step+1;flag=1;return;} if(map[fx][fy]=='.') DFS(fx,fy,step+1); } } void BFS(int x,int y,int step) { memset(vis,0,sizeof(vis)); int s=0,e=1,i; vis[x][y]=1; que[0].x=x; que[0].y=y; que[0].step=step; while(s<e) { now=que[s++]; for (i=1;i<=4;i++) { if(i==1) {next.x=now.x+1;next.y=now.y;} if(i==2) {next.x=now.x-1;next.y=now.y;} if(i==3) {next.x=now.x;next.y=now.y+1;} if(i==4) {next.x=now.x;next.y=now.y-1;} next.step=now.step+1; if(map[next.x][next.y]=='#'||next.x<0||next.y<0||next.x>=h||next.y>=w) continue; if(next.x==xe&&next.y==ye) { anss=next.step; flag=1;break; } if(!vis[next.x][next.y]) { vis[next.x][next.y]=1; que[e++]=next; } } if(flag) break; } } int main() { int t,i,j; int ch; scanf("%d",&t); while(t--) { memset(map,0,sizeof(map)); scanf("%d%d",&w,&h); for (i=0;i<h;i++) { scanf("%s",map[i]); } for (i=0;i<h;i++) { for (j=0;j<w;j++) { if(map[i][j]=='S') { xs=i; ys=j; } else if(map[i][j]=='E') { xe=i; ye=j; } } } flag=0; BFS(xs,ys,1); way=1; flag=0; DFS(xs,ys,1); ansl=ans; way=1; flag=0; ch=xs,xs=xe,xe=ch; //交换S和E的坐标; ch=ys,ys=ye,ye=ch; DFS(xs,ys,1); ansr=ans; printf("%d %d %d\n",ansl,ansr,anss); } return 0; }
相关文章推荐
- Servlet实现用户数的统计
- windows的socket_tcp客服端端与linux的socket_tc服务端简单通讯
- HDU 5224 Tom and paper(最小周长)
- USB 3G dongle E303在android4.4上的移植支持
- Bootstrap组件之媒体对象
- 微软沈向洋:计算机视觉未来在语义层 “两大一精”是关键
- Android实现网络多线程断点续传下载原理
- 机试算法讲解: 第49题 动态规划之最长递增子序列问题
- java基本算法题及答案
- Java线程中run和start方法的区别
- 【剑指Offer面试题】 九度OJ1523:从上往下打印二叉树
- 黑马程序员——java学习9(毕15-16)——TreeSet、Map、泛型、字符排序
- 怎样才是理想的程序员
- zoj 1951 Goldbach's Conjecture(素数筛选继续水)
- LA3357 Pinary (递推)
- MVC4 引用DLL 成功越狱实体模型 自由调用存储过程 自由传递参数
- 【CSS学习】CSS 边框
- 网络的可靠性
- MongoDB 在windows shell环境下的基本操作和命令的使用示例(一)
- 插入排序