标签:des style blog color 使用 strong
Children of the Candy Corn
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‘.
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
2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########
Sample Output
37 5 5
17 17 9
题目大意:
一个迷宫,‘.‘,‘S‘,‘E‘为可以到达的点,‘#‘为墙。 (S点保证在迷宫边缘,且只有一个方向可以走)
规定了三种行动方式(计算从S->N的路程)
1)优先走当前方向的左方,前方,右方,后方。
2)优先走当前方向的右方,前方,左方,后方。
3)S->E的最短路径。
结题思路:
用DFS求前两个行动方式的解。
使用0123来表示当前所在位置的方向。 0前 1右 2后 3左
再通过当前的方向来确定递归的优先级。
BFS求最短路径。
Code:
1 #include<stdio.h> 2 #include<iostream> 3 #include<string> 4 #include<cstring> 5 #include<memory.h> 6 #include<algorithm> 7 #define MAXN 41 8 using namespace std; 9 struct qu 10 { 11 int x,y; 12 }q[2000]; 13 int N,M,end_i,end_j; 14 bool vis[MAXN+10][MAXN+10],flag[MAXN+10][MAXN+10]; 15 int dis[2000],Lstep,Rstep; 16 void dfs_left(int x1,int y1,int d) 17 { 18 Lstep++; 19 if (x1==end_i&&y1==end_j) return ; 20 if (d==0) 21 { 22 if (flag[x1][y1-1]) dfs_left(x1,y1-1,3); 23 else if (flag[x1-1][y1]) dfs_left(x1-1,y1,0); 24 else if (flag[x1][y1+1]) dfs_left(x1,y1+1,1); 25 else if (flag[x1+1][y1]) dfs_left(x1+1,y1,2); 26 } 27 if (d==1) 28 { 29 if (flag[x1-1][y1]) dfs_left(x1-1,y1,0); 30 else if (flag[x1][y1+1]) dfs_left(x1,y1+1,1); 31 else if (flag[x1+1][y1]) dfs_left(x1+1,y1,2); 32 else if (flag[x1][y1-1]) dfs_left(x1,y1-1,3); 33 } 34 if (d==2) 35 { 36 if (flag[x1][y1+1]) dfs_left(x1,y1+1,1); 37 else if (flag[x1+1][y1]) dfs_left(x1+1,y1,2); 38 else if (flag[x1][y1-1]) dfs_left(x1,y1-1,3); 39 else if (flag[x1-1][y1]) dfs_left(x1-1,y1,0); 40 } 41 if (d==3) 42 { 43 if (flag[x1+1][y1]) dfs_left(x1+1,y1,2); 44 else if (flag[x1][y1-1]) dfs_left(x1,y1-1,3); 45 else if (flag[x1-1][y1]) dfs_left(x1-1,y1,0); 46 else if (flag[x1][y1+1]) dfs_left(x1,y1+1,1); 47 } 48 } 49 void dfs_right(int x1,int y1,int d) 50 { 51 Rstep++; 52 if (x1==end_i&&y1==end_j) return ; 53 if (d==0) 54 { 55 if (flag[x1][y1+1]) dfs_right(x1,y1+1,1); 56 else if (flag[x1-1][y1]) dfs_right(x1-1,y1,0); 57 else if (flag[x1][y1-1]) dfs_right(x1,y1-1,3); 58 else if (flag[x1+1][y1]) dfs_right(x1+1,y1,2); 59 } 60 if (d==1) 61 { 62 if (flag[x1+1][y1]) dfs_right(x1+1,y1,2); 63 else if (flag[x1][y1+1]) dfs_right(x1,y1+1,1); 64 else if (flag[x1-1][y1]) dfs_right(x1-1,y1,0); 65 else if (flag[x1][y1-1]) dfs_right(x1,y1-1,3); 66 } 67 if (d==2) 68 { 69 if (flag[x1][y1-1]) dfs_right(x1,y1-1,3); 70 else if (flag[x1+1][y1]) dfs_right(x1+1,y1,2); 71 else if (flag[x1][y1+1]) dfs_right(x1,y1+1,1); 72 else if (flag[x1-1][y1]) dfs_right(x1-1,y1,0); 73 } 74 if (d==3) 75 { 76 if (flag[x1-1][y1]) dfs_right(x1-1,y1,0); 77 else if (flag[x1][y1-1]) dfs_right(x1,y1-1,3); 78 else if (flag[x1+1][y1]) dfs_right(x1+1,y1,2); 79 else if (flag[x1][y1+1]) dfs_right(x1,y1+1,1); 80 } 81 } 82 int bfs(int x1,int y1) 83 { 84 int front=0,rear=1; 85 dis[front]=1; 86 q[front].x=x1,q[front].y=y1; 87 while (front<rear) 88 { 89 int x=q[front].x,y=q[front].y; 90 if (x==end_i&&y==end_j) break; 91 int a[4]={x+1,x-1,x,x},b[4]={y,y,y+1,y-1}; 92 for (int i=0;i<=3;i++) 93 { 94 if (!(a[i]<=0||a[i]>=M+1||b[i]<=0||b[i]>=N+1)&&vis[a[i]][b[i]]==0){ 95 q[rear].x=a[i],q[rear].y=b[i]; 96 dis[rear]=dis[front]+1; 97 vis[a[i]][b[i]]=1; 98 rear++; 99 } 100 } 101 front++; 102 } 103 return dis[front]; 104 105 } 106 int main() 107 { 108 int T,start_i,start_j; 109 cin>>T; 110 while (T--) 111 { 112 Lstep=Rstep=0; 113 memset(vis,0,sizeof(vis)); 114 memset(dis,0,sizeof(dis)); 115 memset(q,0,sizeof(q)); 116 int d; 117 cin>>N>>M; 118 getchar(); 119 for (int i=1; i<=M; i++) 120 { 121 for (int j=1; j<=N; j++) 122 { 123 char tmp; 124 scanf("%c",&tmp); 125 flag[i][j]=1; 126 if (tmp==‘#‘) vis[i][j]=1,flag[i][j]=0; 127 if (tmp==‘E‘) end_i=i,end_j=j; 128 if (tmp==‘S‘) start_i=i,start_j=j; 129 } 130 getchar(); 131 } 132 if (start_i==1) d=2; 133 else if (start_i==M) d=0; 134 else if (start_j==1) d=1; 135 else if (start_j==N) d=3; 136 dfs_left(start_i,start_j,d); 137 dfs_right(start_i,start_j,d); 138 int step1=bfs(start_i,start_j); 139 printf("%d %d %d\n",Lstep,Rstep,step1); 140 } 141 return 0; 142 }
POJ3083——Children of the Candy Corn,布布扣,bubuko.com
POJ3083——Children of the Candy Corn
标签:des style blog color 使用 strong
原文地址:http://www.cnblogs.com/Enumz/p/3848308.html