Http://acm.sdut.edu.cn/sdutoj/problem.php? Action = showproblem & problemid = 1124
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
I learned a lot about searching. This is a simple 3D BFs, but I cannot start with this question. Search questions need to be reinforced.
In BFs, You need to search for all places that can go in each step. For example, you need to search for the first step of this question ~ D * Up and down. This is the core of the algorithm.
Other parts are centered around the core of the algorithm.
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;char map[101][101];int v[101][101][101];int n,m,d;struct node{ int x,y,z; int ans;}q[1000001];int jx[]={1,-1,0,0};int jy[]={0,0,1,-1};void bfs(){ memset(v,0,sizeof(v)); int e=0; int s=0; struct node t,f; t.x=0; t.y=0; t.z=d; t.ans=0; v[t.x][t.y][t.z]=1; q[e++]=t; while(s<e) { t=q[s++]; if(t.x==n-1&&t.y==m-1) { printf("%d\n",t.ans); return ; } for(int i=0;i<4;i++) { f.x=t.x+jx[i]; f.y=t.y+jy[i]; if(f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&v[f.x][f.y][t.z]==0&&map[f.x][f.y]==‘P‘) { f.ans=t.ans+1; f.z=t.z; v[f.x][f.y][f.z]=1; q[e++]=f; } } for(int j=2;j<=t.z;j++) { for(int i=0;i<4;i++) { f.x=t.x+jx[i]*j; f.y=t.y+jy[i]*j; if(f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&v[f.x][f.y][t.z-j]==0&&map[f.x][f.y]==‘P‘) { f.ans=t.ans+1; f.z=t.z-j; v[f.x][f.y][f.z]=1; q[e++]=f; } } } } printf("impossible\n"); return ;}int main(){ while(scanf("%d%d%d",&n,&m,&d)!=EOF) { for(int i=0;i<n;i++) { scanf("%s",map[i]); } bfs(); } return 0;}