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;
}