Children of the Candy Corn(搜索方向变化问题)(bfs)(dfs)

来源:互联网 发布:淘宝详情页设计多少钱 编辑:程序博客网 时间:2024/04/30 17:08

    点击打开链接

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 67   Accepted Submission(s) : 25
Problem 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'. < br>< br>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 ('#'). < br>< br>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
 

这个题难在题意上,题意说是给一个地图,“ . ”是路,“#”是墙,从S走到E,有三种走法,各求最少经过多少个点,要算上S、E,一个点如果走过一次还可以在走一次,要算上次数的。但是关键的走法:1.向左走  2.向右走  3.随便 。3都可以想到,最短路径,广搜做。那1、2是个什么走法呢?我研究好久才研究出来,拿走法1 向左走来说,你从上一个点来到现在所处的点,肯定是上下左右(就按这个地图来说)某个方向过来的,那么就以你现在面朝的方向为前方,优先向左走(哪怕右边就是终点也不能向右);如果左边没路,再优先向前走;前边没路,优先向右;右边还没路,向后走,后边不可能没路的,你刚过来。总结起来,你在任何点,要走的话方向顺序应该是 “左前右后”。 同理,走法2,顺序就是“右前左后”。别问我怎么知道的,比着地图试出来的规律。

 

// 搜索有方向的问题 #if 0#include<iostream>#include<cstring>#include<queue>using namespace std;bool vis[50][1000];char mapk[50][1000];int sx,sy,ex,ey,m,n;int f[5][2]={{0,0},{-1,0},{0,1},{1,0},{0,-1}}; //上右下左 struct node{int x,y,t;};int ok(int x,int y){if(x>=0&&x<m && y>=0&&y<n){if(mapk[x][y]=='#')return 0;elsereturn 1;}elsereturn 0; }void l_s(int x,int y,int pre,int step)      {int d[5];if(x==ex&&y==ey){cout<<step+1<<" ";return;}if(pre==1){d[1]=4,d[2]=1,d[3]=2,d[4]=3;} else{                                                      //上 右 下 左 if(pre==2){d[1]=1,d[2]=2,d[3]=3,d[4]=4;}else{if(pre==3){d[1]=2,d[2]=3,d[3]=4,d[4]=1; }else{if(pre==4){d[1]=3,d[2]=4,d[3]=1,d[4]=2;}} }}for(int i=1; i<=4; i++){int a,b;a=x+f[d[i]][0];b=y+f[d[i]][1];if(ok(a,b)){l_s(a,b,d[i],step+1);break;}}}void r_s(int x,int y,int pre,int step)      {int d[5];if(x==ex&&y==ey){cout<<step+1<<" ";return;}if(pre==1){d[1]=2,d[2]=1,d[3]=4,d[4]=3;} else{                                                      //上 右 下 左 if(pre==2){d[1]=3,d[2]=2,d[3]=1,d[4]=4;}else{if(pre==3){d[1]=4,d[2]=3,d[3]=2,d[4]=1; }else{if(pre==4){d[1]=1,d[2]=4,d[3]=3,d[4]=2;}} }}for(int i=1; i<=4; i++){int a,b;a=x+f[d[i]][0];b=y+f[d[i]][1];if(ok(a,b)){r_s(a,b,d[i],step+1);break;}}}void bfs(){queue<node> q;node now,next;now.t=0;now.x=sx;now.y=sy;vis[sx][sy]=1;q.push(now);while(!q.empty()){now=q.front();q.pop();if(now.x==ex&&now.y==ey){cout<<now.t+1<<endl;return;}else{for(int i=0; i<4; i++){next.x=now.x+f[i][0];next.y=now.y+f[i][1];if(ok(next.x,next.y) && !vis[next.x][next.y]){vis[next.x][next.y]=1;next.t=now.t+1;q.push(next);}}}}}int main(){int t;cin>>t;while(t--){memset(vis,0,sizeof(vis));memset(mapk,0,sizeof(mapk));cin>>n>>m;for(int i=0; i<m; i++){for(int j=0; j<n; j++){cin>>mapk[i][j];if(mapk[i][j]=='S'){sx=i; sy=j;}elseif(mapk[i][j]=='E'){ex=i;ey=j;}}}for(int i=1; i<=4; i++){int x,y;x=sx+f[i][0];y=sy+f[i][1];if(mapk[x][y]=='.'){l_s(sx,sy,i,0);r_s(sx,sy,i,0);break;}}bfs();}}#endif 

原创粉丝点击