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
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 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
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.
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
题目大意: w*h的矩形空间 w是宽度 h是高度 起点是S 终点是E 问:从左搜从右搜 和最短的路径步数
解决思路:2个dfs和一个bfs
bfs没有疑问 正常出队入队即可 。
dfs最关键的两点:
1.方向不是固定的 即每一次要往哪个方向移动要根据上一次的方向确定 例如自定义一个方向 : 左上右下 。 因为是左优先
那么当向上移动时 下一步的左是方向数组的上 而不是最开始的左 ,右优先也是这样。
2.不需要对点进行标记 (vistited) 因为一个方向优先很有可能走进死胡同 因此要往回走 并且就此题而言 往回走也是算步数的。
附上AC code:
//这是 一道方向随时改变的题 所以方向的参数要放在搜索递归函数里
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define MAX 41
typedef pair<int,int> P;
int der[4][2] = { {0,-1} , {-1,0} , {0,1} , {1,0} }; //左上右下
int dir[4][2] = { {-1,0} , {0,-1} , {1,0} , {0,1} }; //右上左下
int ans1;
int ans2;
int flag;
vector<vector<bool> > visited;
vector<vector<int> > c; //BFS的距离
class Solution{
public:
int count;
Solution(){
count = 1;
}
void countClear( int h , int w ){
count = 1;
visited = vector<vector<bool> >(h+1,vector<bool>(w+1,false) );
}
int getCount(){
return count;
}
vector<int> writeData( char maze[][MAX] , int w , int h ){
vector<int> vec(4);
int i,j;
for( i = 0 ; i < h ; i ++ ){
for( j = 0 ; j < w ; j ++ ){
cin >> maze[i][j];
if( maze[i][j] == 'S' ){
vec[0] = i;
vec[1] = j;
}
else if( maze[i][j] == 'E' ){
vec[2] = i;
vec[3] = j;
}
}
maze[i][j] = '\0';
}
maze[i][0] = '\0';
return vec;
}
bool check( int x , int y , int w , int h ){
if( x >= 0 && x < h && y >= 0 && y < w )
return true;
return false;
}
void findLeftSolution( char maze[][MAX] , P start , P end , int w , int h , int step , int d ){
if( flag )
return;
if( start == end ){
ans1 = step;
flag = 1;
return;
}
for( int k = 0 ; k < 4 ; k ++ ){
int newx=start.first+der[(k+d+3)%4][0];
int newy=start.second+der[(k+d+3)%4][1];
if( check( newx , newy, w, h ) && maze[newx][newy] != '#' ){
findLeftSolution(maze,P(newx,newy),end,w,h,step+1,(k+d+3)%4 );
}
}
}
void findRightSolution( char maze[][MAX] , P start , P end , int w , int h , int step , int d ){
if( flag )
return;
if( start == end ){
ans2 = step;
flag = 1;
return;
}
for ( int k = 1 ; k >= -2 ; k -- ){
int newx = start.first+der[(k+d+4)%4][0];
int newy = start.second+der[(k+d+4)%4][1];
if ( check(newx,newy,w,h) && maze[newx][newy] != '#' ){
findRightSolution(maze,P(newx,newy),end,w,h,step+1,(k+d+4)%4);
}
}
return;
}
int BFS( char maze[][MAX] , P start , P end , int w , int h ){
c.clear();
c = vector<vector<int> >(h,vector<int>(w,0));
queue<P> q;
q.push(start);
c[start.first][start.second] = 1;
visited[start.first][start.second] = true;
while( !q.empty() ){
P temp = q.front();
q.pop();
for( int k = 0 ; k < 4 ; k ++ ){
int newX = temp.first + der[k][0];
int newY = temp.second + der[k][1];
if( check(newX,newY,w,h) && maze[newX][newY] != '#' && !visited[newX][newY] ){
q.push( P(newX,newY) );
c[newX][newY] = c[temp.first][temp.second] + 1;
visited[newX][newY] = true;
if( q.back() == end )
return c[newX][newY];
}
}
}
}
};
int main()
{
int n;
cin >> n;
Solution sou;
while( n -- ){
char maze[MAX][MAX];
int w;
int h;
cin >> w;
cin >> h;
if( h == 0 || w == 0 ){
cout << "0 " << "0 " << "0" << endl;
continue;
}
vector<int> v = sou.writeData(maze,w,h);
pair<int,int> start(v[0],v[1]);
pair<int,int> end(v[2],v[3]);
sou.countClear(h,w);
flag = 0;
sou.findLeftSolution(maze,start,end,w,h,1,1);
cout << ans1 <<" ";
sou.countClear(h,w);
flag = 0;
sou.findRightSolution(maze,start,end,w,h,1,1);
cout << ans2 << " ";
sou.countClear(h,w);
cout << sou.BFS(maze,start,end,w,h) << endl;
}
return 0;
}
借用一种 pair类型的二元组来存储坐标.