POJ 3083
来源:互联网 发布:网页服务器软件 编辑:程序博客网 时间:2024/06/07 09:01
Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14380 Accepted: 6205
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.
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.
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
28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########
Sample Output
37 5 517 17 9
Source
South Central USA 2006
题意:
搜索,从S走到E。求出3种方案分别的步数。
1:每次都靠着左墙走。2:每次都靠着右墙。3:机智的他走的最短。
题目保证有左 右墙。
POINT:
两次贪心的DFS。一次BFS。
#include <string>#include <string.h>#include <iostream>#include <queue>#include <stdio.h>using namespace std;const int maxn = 55;char mp[maxn][maxn];int n,m;int ansleft,ansright,ans;int dir[4][2]={-1,0,0,1,1,0,0,-1};void findS(int &xx,int &yy,int &dd){ for(int i=1;i<=n;i++){ if(mp[i][1]=='S'){ xx=i,yy=1,dd=1; return; } } for(int i=1;i<=n;i++){ if(mp[i][m]=='S'){ xx=i,yy=m,dd=3; return;} } for(int i=1;i<=m;i++){ if(mp[1][i]=='S'){ xx=1,yy=i,dd=2; return; } } for(int i=1;i<=m;i++){ if(mp[n][i]=='S'){ xx=n,yy=i,dd=0; return; } }}void dfsleft(int x,int y,int d,int cnt){if(mp[x][y]=='E'){ansleft=cnt;return;}int num[4];if(d==0){num[0]=3,num[1]=0,num[2]=1,num[3]=2;}else if(d==1){num[0]=0,num[1]=1,num[2]=2,num[3]=3;}else if(d==2){num[0]=1,num[1]=2,num[2]=3,num[3]=0;}else if(d==3){num[0]=2,num[1]=3,num[2]=0,num[3]=1;}for(int i=0;i<4;i++){int xx=x+dir[num[i]][0];int yy=y+dir[num[i]][1];if(mp[xx][yy]=='#'||xx<1||xx>n||yy<1||yy>m){continue;}else{dfsleft(xx,yy,num[i],cnt+1);break;}}}void dfsright(int x,int y,int d,int cnt){if(mp[x][y]=='E'){ansright=cnt;return;}int num[4];if(d==0){num[0]=1,num[1]=0,num[2]=3,num[3]=2;}else if(d==1){num[0]=2,num[1]=1,num[2]=0,num[3]=3;}else if(d==2){num[0]=3,num[1]=2,num[2]=1,num[3]=0;}else if(d==3){num[0]=0,num[1]=3,num[2]=2,num[3]=1;}for(int i=0;i<4;i++){int xx=x+dir[num[i]][0];int yy=y+dir[num[i]][1];if(mp[xx][yy]=='#'||xx<1||xx>n||yy<1||yy>m){continue;}else{dfsright(xx,yy,num[i],cnt+1);break;}}}struct node{int x,y;int cnt;node(int xx,int yy,int cc):x(xx),y(yy),cnt(cc){}};void bfs(int x,int y){int flag[maxn][maxn];memset(flag,0,sizeof flag);queue<node> q;q.push(node(x,y,0));while(!q.empty()){x=q.front().x;y=q.front().y;int cnt=q.front().cnt;q.pop();if(mp[x][y]=='E'){ans=cnt;return;}for(int i=0;i<4;i++){int xx=x+dir[i][0];int yy=y+dir[i][1];if(mp[xx][yy]=='#'||xx<1||xx>n||yy<1||yy>m||flag[xx][yy]) continue;flag[xx][yy]=1;q.push(node(xx,yy,cnt+1));}}}int main(){ int T; for(scanf("%d",&T);T--;){ scanf("%d %d",&m,&n); for(int i=1;i<=n;i++){ scanf("%s",mp[i]+1); } int xx,yy,dd; findS(xx,yy,dd); dfsleft(xx,yy,dd,0);printf("%d ",ansleft+1); dfsright(xx,yy,dd,0);printf("%d ",ansright+1);bfs(xx,yy);printf("%d\n",ans+1); } return 0;}
阅读全文
0 0
- POJ 3083
- poj 3083
- POJ 3083
- poj 3083
- poj 3083
- poj 3083
- poj.3083
- POJ 3083
- POJ 3083
- poj-3083
- poj 3083
- poj 3083
- POJ -3083
- poj 3083
- POJ 3083
- POJ 3083 BFS
- poj 3083 TLE
- poj 3083 搜索
- IntelliJ-IDEA和Git、GitHub、Gitlab的使用
- PHP底层未完待续
- Nginx URL重写(rewrite)配置及信息详解
- ajax传递多个参数的两种方法和label标签中获取值
- 字符串函数模拟实现(strcpy,strcat,strstr,strchr,strcmp,memcpy,memmove)
- POJ 3083
- 汇编第九节-转移指令的原理
- TOAD与Oracle DB 12c
- C#中数据类型
- OSG3.4.0+QT5.8.0+VS2013+WIN10 64BIT
- 生成器&迭代器
- MyBatis起步
- Android——systrace使用分析
- HDU 2544 最短路(多种解法)