龙龙最近迷上了一款名叫 PUBG(PLAYERUNKNOWN’S BATTLEGROUNDS)的手游,那是一款关乎生存挑战的 RPG 逃亡游戏。
考虑到游戏的环节过于复杂,龙龙决定简化一下场景:整个地图可以看做一个长为n宽为m的二维格点平面。龙龙需要从 x1y1逃亡到x2y2以逃离毒圈,但有些格点上存在障碍#不能行走,有些格点是沙地.。龙龙只能移动在允许行走的沙地上,同时每一时刻,龙龙只能朝着当前位置周围的上、下、左、右四个方向移动。同时因为龙龙使用了能量饮料,每分钟最多可以朝着一个方向行走 k 步。
毒圈快要来啦,请你帮龙龙尽快安排一下可行的路线,使得它能够以最短的时间顺利进圈。
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1100;
int n,m,k;
int d[N][N],st[N][N];
string g[N];
int sx,sy,ex,ey;
int bfs()
{
queue<PII> q;
d[sx][sy] = 0;
st[sx][sy]=1;
q.push({sx, sy});
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; //四个方向
while (q.size()) //bfs利用队列进行
{
auto t = q.front();
q.pop();
int tt=d[t.first][t.second] + 1;
for (int i = 0; i < 4; i ++ )
{
for(int zz=1;zz<=k;zz++){
int x = t.first + dx[i]*zz, y = t.second + dy[i]*zz; //一直走直到走不了
if (x < 0 || x >= n || y < 0 || y >= m ||st[x][y])continue;
if( g[x][y] == '#' ){
break;
}
st[x][y]=1;
d[x][y] = tt ;
q.push({x, y});
if(x==ex&&y==ey)return tt;
}
}
}
return -1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t=1;
while(t--){
memset(d, 0x3f, sizeof d); //初始化
memset(st, 0, sizeof st);
cin >> n >> m>>k;
for (int i = 0; i < n; i ++ ){
cin >> g[i];
}
cin>>sx>>sy>>ex>>ey;
sx--,sy--,ex--,ey--;
cout << bfs() << endl;
}
return 0;
}