您的位置:首页 > 其它

POJ 3083 Children of the Candy Corn (DFS+BFS)

2016-07-31 18:37 489 查看
O - Children of the Candy CornTime Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld& %lluSubmit Status Practice POJ3083Appoint description: System Crawler  (Jul 24, 2016 10:45:01 PM)DescriptionThe 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 tofind 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 seldomthe 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 confoundingvisitors.InputInput 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 byh 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 alsobe separated by at least one wall ('#'). You may assume that the maze exit is always reachable from the start point.OutputFor 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 的步数。再沿着右边的墙从 S 一直走,求到达 E 的步数。最后求最短路。 s思路:最后一步很好办,主要是前两步,要弄清怎么转的。当前方向     检索顺序     ↑ :      ← ↑ → ↓    → :        ↑ → ↓ ←      ↓ :      → ↓ ← ↑     ← :        ↓ ← ↑ → 如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。在DFS时,加一个参数,用来保存当前的方向。#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;struct node{int x,y,time;};int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};int dl[][2]={{0,-1},{-1,0},{0,1},{1,0}};int dr[][2]={{0,1},{-1,0},{0,-1},{1,0}};int ex,ey,sx,sy,n,m;char map[110][110];int dfs(int x,int y,int d,int step,int dir[][2])//d为当前方向{for(int i=0;i<4;i++){int j=((d-1+4)%4+i)%4;int nx=x+dir[j][0];int ny=y+dir[j][1];if(nx==ex&&ny==ey)return step+1;if(nx<0||ny<0||nx>n||ny>m)continue;if(map[nx][ny]=='#')continue;return dfs(nx,ny,j,step+1,dir);}}int bfs(int sx,int sy){bool vis[110][110];memset(vis,false,sizeof(vis));queue<node>q;node now,t,next;t.x=sx;t.y=sy;t.time=1;q.push(t);vis[sx][sy]=true;while(!q.empty()){now=q.front();q.pop();if(now.x==ex&&now.y==ey)return now.time;for(int i=0;i<4;i++){next.x=now.x+dx[i];next.y=now.y+dy[i];next.time=now.time+1;if(next.x<0||next.y<0||next.x>n||next.y>m)continue;if(vis[next.x][next.y])continue;if(map[next.x][next.y]!='#'){vis[next.x][next.y]=true;q.push(next);}}}return -1;}int main(){int t,d1,d2;scanf("%d",&t);while(t--){scanf("%d%d",&m,&n);for(int i=0;i<n;i++){scanf("%s",map[i]);for(int j=0;j<m;j++){if(map[i][j]=='S'){sx=i;sy=j;}if(map[i][j]=='E'){ex=i;ey=j;}}}if(sx == 0){d1 = 3;d2 = 3;}else if(sx == n-1){d1 = 1;d2 = 1;}else if(sy == 0){d1 = 2;d2 = 0;}else if(sy == m-1){d1 = 0;d2 = 2;}printf("%d ",dfs(sx,sy,d1,1,dl));printf("%d ",dfs(sx,sy,d2,1,dr));printf("%d\n",bfs(sx,sy));}return 0;}
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: