# Sdut 1124-leap wilderness-3D BFS

Source: Internet
Author: User
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 <cstdio>//BFS#include <iostream>#include <cstring>#include <algorithm>#include <queue>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 <node> 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<<v.time<<endl;return ;}  for(i=0;i<4;i++){t.x=v.x+dir[i][0];t.y=v.y+dir[i][1];t.d=v.d;if(t.x>=0&&t.x<m&&t.y>=0&&t.y<n&&!vis[t.x][t.y][t.d]&&ma[t.x][t.y]=='P')//run{vis[t.x][t.y][t.d]=1;t.time=v.time+1;Q.push(t);}for(j=2;j<=v.d;j++)//fly{t.x=v.x+j*dir[i][0];t.y=v.y+j*dir[i][1];if(t.x>=0&&t.x<m&&t.y>=0&&t.y<n&&!vis[t.x][t.y][v.d-j]&&ma[t.x][t.y]=='P'){vis[t.x][t.y][v.d-j]=1;t.d=v.d-j;t.time=v.time+1;Q.push(t);}}}  }  puts("impossible");}int main(){int i,j;while(cin>>m>>n>>d){memset(vis,0,sizeof(vis));for(i=0;i<m;i++)cin>>ma[i];bfs();}    return 0;}`

• Submit
• Status
• Discussion
Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.