SDUT 1124-leap wilderness-3D BFS
Leap wilderness Time Limit: 5000 ms Memory limit: 65536 K any questions? Click Here ^_^ After the brave fario completed the task, he was quickly retreating to his own base. However, because there is a large group of follow-up troops behind it, fario should return to the base as soon as possible, or else he will be caught by the enemy.
Finally, fario came to the last stop: The field of telachel, where he can return to the base. However, the enemy persists. However, the geographical conditions of telazhir are very favorable for fario, and many lakes are everywhere. The enemy needs to bypass the road, but fario decided to find a way to return to the base as soon as possible.
Suppose that the field in telashir is a matrix of m * n, which has two kinds of terrain: P indicating flat, L indicating Lake, and fario can only stay on flat. The current position is in the upper left corner (), and the destination is in the lower right corner (m, n ). Fario can move or fly in the left and right directions. It takes 1 unit of time to move one cell. The flight time is mainly due to deformation, and the flight time consumption is very short. Therefore, no matter how far a flight is, only one unit of time is required. On the way to the flight, you cannot change directions, and a flight must eventually land on the ground. Of course, due to the limitation of energy, fario cannot fly without restrictions. The maximum distance he can fly is D. After knowing the above information, please help fario calculate the time it takes for him to arrive at the Base as quickly as possible. The first line of the input is a three integer, m (1 ≤ m ≤ 100), n (1 ≤ n ≤ 100), D (1 ≤ D ≤ 100 ). It indicates that the field is a matrix of m * n, and fario can only fly at most D distance. There are n characters in each line of the next m line, and there is no space between them. P indicates that the current position is flat, and L indicates the lake. Assume that () and (m, n) must be flat. Output an integer to indicate the shortest time required for fario to arrive at the base. If you cannot reach the base, output impossible. Sample Input
4 4 2PLLPPPLPPPPPPLLP
Sample output
5
QAQ cannot go through eight search directions with two-dimensional bfs anger. Wjj says that State synchronization is only available in three dimensions, and sad is not sensitive to three dimensions.
This topic uses rows and columns x and y to establish a three-dimensional search idea based on the distance from D, and then let it walk and fly in four directions each time. Nothing else.
#include
//BFS#include
#include
#include #include
using namespace std;char ma[110][110];bool vis[110][110][110];typedef struct node{int x,y,d,time;};int n,m,d;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};void bfs(){ queue
Q; node t;int i,j; t.x=0;t.y=0;t.time=0;t.d=d; Q.push(t); while(!Q.empty()) { node v=Q.front(); Q.pop(); if(v.x==m-1&&v.y==n-1){cout<
=0&&t.x
=0&&t.y
=0&&t.x
=0&&t.y
>m>>n>>d){memset(vis,0,sizeof(vis));for(i=0;i
>ma[i];bfs();} return 0;}