25. 进圈


龙龙最近迷上了一款名叫 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;  
}  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值