您的位置:首页 > 其它

POJ 3083-Children of the Candy Corn

2015-02-27 22:50 288 查看
Children of the Candy Corn
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 10452Accepted: 4504
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 characters each that represent the mazelayout. 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 onesquare 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
小结:
第一次写博客,还真的是第一次同时用到DFS和BFS,感触蛮深的,只是感觉到新的知识需要完全掌握还是需要很长一段时间啊,顺便秀一秀高中语文水平吧!常言道,不积跬步,无以至千里,不积小流,无以成江海,真正的游戏终于开始了!
好了,不谈感悟了,说些正事吧,这道题目,应该是属于DFS和BFS的水题吧,尽管如此,我完成的依然很艰辛,需要注意的是,输入的是列和行的数目,而不是通常观念的行和列。
求最短路径用BFS即可,求靠左靠右的路径长度时,我是设立flag代表玩家所面朝的方向,靠左则顺时针改变朝向,靠右则逆时针改变朝向,然后不断记录步数,得出结果。
以下是AC代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>char map[50][50];int startx,starty,endx,endy,step1,step2,step3,flag;struct node{int x,y,step;}queue[2000],start;int dis[4][2]={0,-1,-1,0,0,1,1,0};int num1,num2;//flag 0,1,2,3分别代表左上右下int ok(int a,int b){if(a>=0&&a<num1&&b>=0&&b<num2&&map[a][b]!='#')return 1;return 0;}void searchleft(int a,int b){int temp=(flag+3)%4;int aa,bb;step1++;if(map[a][b]=='E')return;for(int i=0;i<4;i++){aa=a+dis[temp][0];bb=b+dis[temp][1];if(ok(aa,bb))break;temp++;temp=temp%4;}flag=temp;//printf("%d\n",step1);searchleft(aa,bb);}void searchright(int a,int b){int temp=(flag+1)%4;int aa,bb;step2++;if(map[a][b]=='E')return;for(int i=0;i<4;i++){aa=a+dis[temp][0];bb=b+dis[temp][1];if(ok(aa,bb))break;temp+=3;temp=temp%4;}flag=temp;//printf("%d\n",step2);searchright(aa,bb);}int bfs(struct node start){int top,end;struct node in,next;top=end=0;queue[top]=start;while(top>=end){in=queue[end];end++;for(int i=0;i<4;i++){next.x=in.x+dis[i][0];next.y=in.y+dis[i][1];if(!ok(next.x,next.y))continue;if(map[next.x][next.y]=='E')return ++in.step;if(map[next.x][next.y]!='#'){map[next.x][next.y]='#';next.step=in.step+1;top++;queue[top]=next;}}}return -1;}int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d",&num2,&num1);getchar();flag=1;step1=step2=step3=0;for(int i=0;i<num1;i++){gets(map[i]);}for(int i=0;i<num1;i++){for(int j=0;j<num2;j++){if(map[i][j]=='S'){startx=i;starty=j;start.x=i;start.y=j;start.step=0;}}}searchleft(startx,starty);searchright(startx,starty);step3=bfs(start);//printf("%d\n",step3+1);printf("%d %d %d\n",step1,step2,++step3);//printf("%d\n",step1);//printf("%d\n",step2);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: