题目描述
在一片广阔的土地上,有一个鸟人,他需要从这里穿过原野,回到基地。这片原野上,有平地 §、有湖泊 (L),因为鸟人可以飞,所以呢,有的时候,他可以飞越湖泊。现在,鸟人需要用最快的时间,回到基地。
假设原野是一个 m∗n 的矩阵,有两种地形,用 P 和 L 表示。鸟人只能停留在平地上。他目前处在 (1,1) 这个位置,而目的地是 (m,n),起点和目的地必为平地。他可以向上下左右四个方向移动,或者飞行。每移动一格需要 1 个单位时间。而飞行无论飞多远,都只需要 1 个单位时间。飞行的途中不可以变方向,也就是说飞行也只能是上下左右四个方向。并且一次飞行最终必须降落在平地上。当然,受到能量的限制,鸟人不能无限制的飞行,他总共最多可以飞行的距离为 D(总计飞行距离,不是一次飞行距离)。现求飞到目的地的最短时间,若无法到达则输出 impossible。
输入
第一行三个整数 m,n,D。(1≤m,n,D≤100)
接下来是一个 m∗n 的矩阵,表示原野。
输出
输出一个数,表示最短时间,如果无法到达,则输出 impossible。
样例输入
4 4 2
PLLP
PPLP
PPPP
PLLP
样例输出
5
数据规模与约定
时间限制:1 s
内存限制:256 M
100% 的数据保证 1≤m,n,D≤100
#include <iostream>
#include <queue>
using namespace std;
struct node {
int x, y, s, d;
};
queue<node> que;
int m, n, d, check[105][105][105];
char map[105][105];
int dir[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };
int main() {
cin >> n >> m>> d;
for (int i = 1; i <= n; i++) {
cin >> &map[i][1];
}
que.push({ 1,1,0,d });
for (int i = 0; i <= d; i++) {
check[1][1][i] = 1;
}
while (!que.empty()) {
node t = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int x = t.x + dir[i][0];
int y = t.y + dir[i][1];
if (x == n && y == m) {
cout << t.s + 1 << endl;
return 0;
}
if (map[x][y] == 'P' && check[x][y][t.d] == 0) {
check[x][y][t.d] = 1;
que.push({ x,y,t.s + 1,t.d });
}
for (int j = 2; j <= t.d; j++) {
int x = t.x + j * dir[i][0];
int y = t.y + j * dir[i][1];
if (x == n && y == m) {
cout << t.s + 1 << endl;
return 0;
}
if (map[x][y] == 0) break; //越界
if (map[x][y] == 'P' && check[x][y][t.d - j] == 0) {
check[x][y][t.d - j] = 1;
que.push({ x,y,t.s + 1,t.d - j });
}
}
}
}
cout << "impossible" << endl;
return 0;
}