题解:P13096 [FJCPC 2025] 难以控制的滑板火箭
前言:
学懂了就要讲出来!
一道有趣的分层图题。
看个乐子
注意:文中
简要题意:
这题目简化后变成了每一次可以走 -1。
思路:
一、当 -1,可行就能走越远走越远,所以输出为
二、当
预处理:建立一个分层图,每个点分割成
一、当
所以我们考虑有无长度为偶数的最短路。若有,输出 -1。
二、当
综上,该题已做完。
在我们不断地探索中,一道有深意的绿题就被我们成功通过了。
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1001;
int t,n,m,l,r,o,e;
int dx[]={-1,0,1,1,1,0,-1,-1},dy[]={-1,-1,-1,0,1,1,1,0};
char a[N][N];
int mp[N][N][2];
bool vis[N][N][2];
struct node{
int x,y,col;
};
signed main(){
cin>>t;
while(t--){
cin>>n>>m;
cin>>l>>r;
o=INT_MAX;
e=INT_MAX;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
mp[i][j][0]=INT_MAX;
mp[i][j][1]=INT_MAX;
vis[i][j][0]=0;
vis[i][j][1]=0;
}
}
queue<node>q;
q.push(node{1,1,0});
mp[1][1][0]=0;
vis[1][1][0]=1;
while(!q.empty()){
node tp=q.front();
q.pop();
int x=tp.x,y=tp.y,col=tp.col;
for(int i=0;i<8;i++){
int tx=x+dx[i],ty=y+dy[i];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&a[tx][ty]=='1'&&!vis[tx][ty][col^1]){
mp[tx][ty][col^1]=mp[x][y][col]+1;
vis[tx][ty][col^1]=1;
q.push(node{tx,ty,col^1});
}
}
}
if(mp[n][m][1]==INT_MAX&&mp[n][m][0]==INT_MAX){
cout<<"-1\n";
continue;
}
o=mp[n][m][1];
e=mp[n][m][0];
if(l<r){
cout<<min(e/r+(e%r!=0),o/r+(o%r!=0))<<"\n";
}
else{
if(r%2==0){
if(e==INT_MAX){
cout<<"-1\n";
}
else cout<<(e/r+(e%r!=0))<<"\n";
}
else{
int et=INT_MAX,ot=INT_MAX;
if(e%r==0){
et=e/r;
}
else
{
if((r-e%r)%2){
et=e/r+2;
}
else et=e/r+1;
}
if(o%r==0){
ot=o/r;
}
else{
if((r-o%r)%2) ot=o/r+2;
else ot=o/r+1;
}
cout<<min(et,ot)<<"\n";
}
}
}
return 0;
}
后话:
感谢管理员的审核时的提醒,谢谢。