您的位置:首页 > 其它

Children of the Candy Corn(dfs+bfs)

2016-03-24 13:25 405 查看
B - Children of the Candy CornCrawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64uSubmitStatusPracticePOJ 3083Appoint description:System Crawler (2016-03-19)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 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 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 by h lines of w characterseach 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 singlespace 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
题意:
要你找出左边优先,右边优先,和最短的路径的长度。
思路:
最短路要用bfs搜索,不然会超时,而左右可以用dfs搜索。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<vector>#include<queue>using namespace std;#define CRL(a) memset(a,0,sizeof(a))#define T 55int n,m,xx,yy,cnt,flag;int bo[T][T];char map[T][T];int dl[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};int dr[][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};void dfs(int x,int y,int d,int fx[][2]){int k,tx,ty;for(k=0;k<4;++k){int j = (d+3+k)%4;tx = x + fx[j][0];ty = y + fx[j][1];if(tx==xx&&ty==yy){cnt++,flag=1;return;}if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]=='.'){cnt++;dfs(tx,ty,j,fx);if(flag)return;cnt++;}}}struct node{pair<int,int> v;int sum;}a,b;int bfs(int x,int y){queue<node> s;a.v.first = x;a.v.second = y;a.sum = 1;s.push(a);int tx,ty;while(!s.empty()){b = s.front();s.pop();for(int i=0;i<4;++i){tx = dl[i][0] + b.v.first;ty = dl[i][1] + b.v.second;if(tx==xx&&ty==yy)return b.sum+1;if(tx>=0&&tx<n&&ty>=0&&ty<m&&!bo[tx][ty]&&map[tx][ty]!='#'){a.v.first = tx,a.v.second = ty;bo[tx][ty] = 1;a.sum = b.sum + 1;s.push(a);}}}}int main(){/*freopen("input.txt","r",stdin);*/int N,i,j,x,y;scanf("%d",&N);while(N--){CRL(bo);flag = 0;cnt = 1;scanf("%d%d",&m,&n);for(i=0;i<n;++i){for(j=0;j<m;++j){scanf(" %c",&map[i][j]);if(map[i][j]=='S'){x = i,y = j;}if(map[i][j]=='E'){xx = i,yy = j;}}}int d1,d2;if(x == 0) { d1 = 3; d2 = 3; }else if(x == n-1) { d1 = 1; d2 = 1; }else if(y == 0) { d1 = 2; d2 = 0; }else if(y == m-1) { d1 = 0; d2 = 2; }dfs(x,y,d1,dl);printf("%d ",cnt);cnt=1;flag=0;dfs(x,y,d2,dr);printf("%d ",cnt);printf("%d\n",bfs(x,y));}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs bfs