E - Stronger Takahashi (01bfs)

本文介绍了一种利用双端队列和广度优先搜索(BFS)策略,解决不花费体力优先,然后逐步探索破墙路径的最短成本问题。通过实例代码展示了如何在AtCoder比赛任务中应用这种算法找到从起点到终点的最低花费路线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://atcoder.jp/contests/abc213/tasks/abc213_e


如果一条路可以通过不花费体力到达,那就尽量不花费。

那么如果不花费无法到达终点,那么我们用花费体力从之前的所有路中探索没有走过的路。

由此,采用双端队列 + bfs来操作,优先将不花费体力走的路压入队列前端,将要花费体力的路压入队列后端。

直到不花费体力走的路在队列中没有了且没达到终点,这时候就会轮到队列的back()元素来更新了。

#include<iostream>
#include<vector>
#include<queue>
#include<deque>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=510;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL dx[4]={1,0,-1,0};
LL dy[4]={0,1,0,-1};
LL n,m;
char ma[maxn][maxn];
LL cost[maxn][maxn];
struct P{
    LL x,y;
};
void bfs(LL sx,LL sy){
    deque<P>que;
    memset(cost,0x3f,sizeof(cost));
    cost[sx][sy]=0;
    que.push_front({sx,sy});
    while(!que.empty()){
        P now=que.front();que.pop_front();
        ///更新完不破墙能到达的路
        for(LL k=0;k<4;k++){
            LL nx=now.x+dx[k];
            LL ny=now.y+dy[k];
            if(nx<1||ny<1||nx>n||ny>m) continue;
            if(ma[nx][ny]=='#') continue;
            if(cost[nx][ny]>cost[now.x][now.y]){
                cost[nx][ny]=cost[now.x][now.y];
                que.push_front({nx,ny});
            }
        }
        ///对必须要破墙的路进行破,不过要先队列的末尾,最后再处理,cost要提前更新
        for(LL k1=-2;k1<=2;k1++){
            for(LL k2=-2;k2<=2;k2++){
                LL nx=now.x+k1;
                LL ny=now.y+k2;
                if(abs(k1)+abs(k2)==4) continue;
                if(nx<1||ny<1||nx>n||ny>m) continue;
                if(cost[nx][ny]>cost[now.x][now.y]+1){
                    cost[nx][ny]=cost[now.x][now.y]+1;
                    que.push_back({nx,ny});
                }
            }
        }
    }
}
int main(void){
   	cin.tie(0);std::ios::sync_with_stdio(false);
    cin>>n>>m;
    for(LL i=1;i<=n;i++){
        for(LL j=1;j<=m;j++){
            cin>>ma[i][j];
        }
    }
    bfs(1,1);
    cout<<cost[n][m]<<"\n";
   	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值