POJ_3083——贴左右墙DFS,最短路径BFS
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.
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.
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
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 struct node 7 { 8 int x, y; 9 int step; 10 }start,end; 11 12 int T, w, h, L_step, R_step, step; 13 14 int dir[4][2]={-1,0,0,-1,1,0,0,1}; 15 16 char map[50][50], *p; 17 18 //左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格 19 bool DFS_Left(int x,int y,int d) 20 { 21 int _d, _dx, _dy; 22 if(map[x][y]=='E') 23 { 24 return true; 25 } 26 27 L_step++; 28 29 //根据上一次走的方向,用公式推出下一次要走的方向 30 31 //往左走 32 _d = (d+1)%4; 33 _dx = x + dir[_d][0]; 34 _dy = y + dir[_d][1]; 35 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 36 { 37 if(DFS_Left(_dx,_dy,_d)) 38 return true; 39 } 40 41 //往原始方向走 42 _d = d; 43 _dx = x + dir[_d][0]; 44 _dy = y + dir[_d][1]; 45 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 46 { 47 if(DFS_Left(_dx,_dy,_d)) 48 return true; 49 } 50 51 //往右边走 52 _d = (d+3)%4; 53 _dx = x + dir[_d][0]; 54 _dy = y + dir[_d][1]; 55 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 56 { 57 if(DFS_Left(_dx,_dy,_d)) 58 return true; 59 } 60 61 //往回走 62 _d = (d+2)%4; 63 _dx = x + dir[_d][0]; 64 _dy = y + dir[_d][1]; 65 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 66 { 67 if(DFS_Left(_dx,_dy,_d)) 68 return true; 69 } 70 71 L_step--; 72 return false; 73 } 74 75 bool DFS_Right(int x,int y,int d) 76 { 77 int _d, _dx, _dy; 78 if(map[x][y]=='E') 79 { 80 return true; 81 } 82 83 R_step++; 84 85 //根据上一次走的方向,用公式推出下一次要走的方向 86 87 //往右走 88 _d = (d+3)%4; 89 _dx = x + dir[_d][0]; 90 _dy = y + dir[_d][1]; 91 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 92 { 93 if(DFS_Right(_dx,_dy,_d)) 94 return true; 95 } 96 97 //往原始方向走 98 _d = d; 99 _dx = x + dir[_d][0]; 100 _dy = y + dir[_d][1]; 101 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 102 { 103 if(DFS_Right(_dx,_dy,_d)) 104 return true; 105 } 106 107 //往左边走 108 _d = (d+1)%4; 109 _dx = x + dir[_d][0]; 110 _dy = y + dir[_d][1]; 111 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 112 { 113 if(DFS_Right(_dx,_dy,_d)) 114 return true; 115 } 116 117 //往回走 118 _d = (d+2)%4; 119 _dx = x + dir[_d][0]; 120 _dy = y + dir[_d][1]; 121 if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w) 122 { 123 if(DFS_Right(_dx,_dy,_d)) 124 return true; 125 } 126 127 R_step--; 128 return false; 129 } 130 131 //寻找最短路只能用BFS 132 void BFS() 133 { 134 node temp,next; 135 queue<node>p; 136 p.push(start); 137 while(!p.empty()) 138 { 139 temp=p.front(); 140 p.pop(); 141 142 if(temp.x==end.x && temp.y==end.y) 143 { 144 step = temp.step; 145 break; 146 } 147 148 next.step = temp.step + 1; 149 for(int i=0;i<4;i++) 150 { 151 next.x = temp.x + dir[i][0]; 152 next.y = temp.y + dir[i][1]; 153 if(map[next.x][next.y]!='#' && next.x>=0 && next.x<h && next.y>=0 && next.y<w) 154 { 155 //BFS中在原始地图中记录走过的路径,会把结束标记给覆盖掉,所以要预先存储 156 //终点坐标,或者另开一个数组记录路径 157 map[next.x][next.y]='#'; 158 p.push(next); 159 } 160 } 161 } 162 return; 163 } 164 165 int main() 166 { 167 scanf("%d",&T); 168 while(T--) 169 { 170 scanf("%d%d",&w,&h); 171 for(int i=0;i<h;i++) 172 { 173 scanf("%s",map[i]); 174 //获取起点坐标 175 p=strchr(map[i],'S'); 176 if(p!=NULL) 177 { 178 start.x=i; 179 start.y=p-map[i]; 180 } 181 182 p=strchr(map[i],'E'); 183 if(p!=NULL) 184 { 185 end.x=i; 186 end.y=p-map[i]; 187 } 188 } 189 190 L_step=R_step=1; start.step=1; 191 192 DFS_Left(start.x,start.y,0); 193 DFS_Right(start.x,start.y,0); 194 BFS(); 195 printf("%d %d %d\n",L_step,R_step,step); 196 } 197 return 0; 198 }
——现在的努力是为了小时候吹过的牛B!!