Time Limit: 3000 ms Memory Limit: 256 MB
Total Submission: 93 Submission Accepted: 21
Total Submission: 93 Submission Accepted: 21
Judge By Case
Description
在一片广阔的土地上,有一个鸟人,他需要从这里穿过原野,回到基地。这片原野上,有平地(P)、有湖泊(L),因为鸟人可以飞,所以呢,有的时候,他可以飞越湖泊。现在,鸟人需要用最快的时间,回到基地。
假设原野是一个m*n的矩阵,有两种地形,用P和L表示。鸟人只能停留在平地上。他目前处在(1,1)这个位置,而目的地是(m,n)。他可以向上下左右四个方向移动,或者飞行。每移动一格需要1个单位时间。而飞行无论飞多远,都只需要1个单位时间。飞行的途中不可以变方向,也就是说飞行也只能是上下左右四个方向。并且一次飞行最终必须降落在平地上。当然,受到能量的限制,鸟人不能无限制的飞行,他总共最多可以飞行的距离为D。
假设原野是一个m*n的矩阵,有两种地形,用P和L表示。鸟人只能停留在平地上。他目前处在(1,1)这个位置,而目的地是(m,n)。他可以向上下左右四个方向移动,或者飞行。每移动一格需要1个单位时间。而飞行无论飞多远,都只需要1个单位时间。飞行的途中不可以变方向,也就是说飞行也只能是上下左右四个方向。并且一次飞行最终必须降落在平地上。当然,受到能量的限制,鸟人不能无限制的飞行,他总共最多可以飞行的距离为D。
Input
第一行是三个整数,m,n,D,三个数都不超过100,下面是一个m*n的矩阵,表示原野
Output
一个整数,为最短时间,如果无法到达,则输出“impossible”
Sample Input
Original | Transformed |
4 4 2 PLLP PPLP PPPP PLLP
4[SP]4[SP]2[EOL] PLLP[EOL] PPLP[EOL] PPPP[EOL] PLLP[EOF]
Sample Output
Original | Transformed |
5
vis用三维表示剩余能量为d时点(x,y)是否到达过
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<sstream>
#include<map>
//#define DEBUG
const int maxn =150;
using namespace std;
int m, n, d,ans;
char G[maxn][maxn];
bool vis[maxn][maxn][maxn];
//int dis[maxn][maxn];
bool check(int x, int y);
struct point {
int x, y, d, t;
point(int _x, int _y, int _d, int _t) :x(_x), y(_y), d(_d), t(_t) {};
bool operator<(const point &rhs)const {
if (t == rhs.t)
return d < rhs.d;
else
return t < rhs.t;
}
};
int main() {
#ifdef DEBUG
freopen("Text.txt", "r", stdin);
#endif // DEBUG
cin.tie(0);
cin.sync_with_stdio(false);
while (scanf("%d %d %d",&m,&n,&d)!=EOF){
//cout << m << n << d << endl;
int i, j, k;
ans = -1;
memset(vis, 0, sizeof(vis));
//memset(dis, 0, sizeof(dis));
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
scanf("\n%c", &G[i][j]);
/*for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) {
printf("%c", G[i][j]);
}
cout << endl;
}*/
queue<point>q;
while (!q.empty()) q.pop();
int dir[][2] = { {1,0},{-1,0},{0,-1},{0,1} };
q.push(point(1,1,d,0));
vis[1][1][d] = 1;
while (!q.empty()) {
int x = q.front().x;
int y = q.front().y;
int d = q.front().d;
int t = q.front().t;
q.pop();
if (x == m&&y == n) {
ans = t;
break;
}
for (i = 0; i < 4; i++) {
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (!check(xx, yy))
continue;
if (G[xx][yy] != 'P')
continue;
if (vis[xx][yy][d])
continue;
q.push(point(xx,yy,d,t+1));
vis[xx][yy][d] = 1;
}
for (i = 0; i < 4; i++) {
int xx, yy;
for (j = 1; j <= d; j++) {
xx = x + dir[i][0] * j;
yy = y + dir[i][1] * j;
if (!check(xx, yy))
continue;
if (G[xx][yy] != 'P')
continue;
if (vis[xx][yy][d-j])
continue;
q.push(point(xx, yy, d - j, t + 1));
vis[xx][yy][d - j] = 1;
}
}
}
if (ans == -1)
printf("impossible\n");
else
printf("%d\n", ans);
}
return 0;
}
bool check(int x, int y) {
if (x > 0 && x <= m&&y > 0 && y <= n)
return true;
else
return false;
}