描述
输入
输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
输出
输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。
样例输入
样例输出
佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?
已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
样例输入
14 4 1
#@##
**##
###+*
***
样例输入24 4 2
#@##
**##
###+
****
样例输出16
样例输出24
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
struct point
{
int x, y, ckl, time;
point (int xx,int yy, int cc, int tt):x(xx), y(yy), ckl(cc), time(tt){};
};
int r,c,t;
int min_time = 1 << 30;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char mp[210][210];
bool visited[210][210][11];
int bfs(int x, int y, int ex, int ey, int t)
{
queue<point> q;
q.push(point(x, y, t, 0));
while (!q.empty())
{
point temp = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int sx = temp.x + dx[i];
int sy = temp.y + dy[i];
if (sx == ex && sy == ey)
{
min_time = temp.time + 1;
return true;
}
if (mp[sx][sy] == '*')
{
if (sx >= 0 && sx < r && sy >= 0 && sy < c && !visited[sx][sy][temp.ckl])
{
visited[sx][sy][temp.ckl] = true;
q.push(point(sx, sy, temp.ckl, temp.time + 1));
}
}
if (mp[sx][sy] == '#')
{
if (sx >= 0 && sx < r && sy >= 0 && sy < c && !visited[sx][sy][temp.ckl - 1] && temp.ckl > 0)
{
visited[sx][sy][temp.ckl - 1] = true;
q.push(point(sx, sy, temp.ckl - 1, temp.time + 1));
}
}
}
}
return false;
}
int main(){
cin >> r >> c >> t;
int x, y, ex, ey;
memset(visited, 0, sizeof(visited));
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> mp[i][j];
if(mp[i][j] == '@')
{
x = i;
y = j;
mp[i][j] = '*';
}
if(mp[i][j] == '+')
{
ex = i;
ey = j;
mp[i][j] = '*';
}
}
}
if (bfs(x, y, ex, ey, t))
cout << min_time << endl;
else
cout << "-1" <<endl;
}