社区讨论
60分求调,玄关
P3855[TJOI2008] Binary Land参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhj11mei
- 此快照首次捕获于
- 2025/11/03 18:58 4 个月前
- 此快照最后确认于
- 2025/11/03 18:58 4 个月前
WA on #3 #5 #7 #8
用的bfs,提交记录
code
CPP#include<bits/stdc++.h>
#define int long long
using namespace std;
const int dGx[4]={-1,1,0,0};
const int dGy[4]={0,0,-1,1};
const int dMx[4]={-1,1,0,0};
const int dMy[4]={0,0,1,-1};
int r,c;
char a[40][40];
int Gsx,Gsy,Msx,Msy;
int ex,ey;
struct Node{
int Gx,Gy;
int Mx,My;
int step;
};
queue<Node> q;
bool vis[40][40][40][40];
bool check(int x,int y){
return (x>=1 && x<=r && y>=1 && y<=c && a[x][y]!='X' && a[x][y]!='#');
}
void bfs(int Gx,int Gy,int Mx,int My,int step){
q.push(Node{Gx,Gy,Mx,My,step});
vis[Gx][Gy][Mx][My]=true;
while(!q.empty()){
Node now=q.front();
q.pop();
if(now.Gx==ex && now.Gy==ey && now.Mx==ex && now.My==ey){
cout<<now.step;
return ;
}
for(int i=0;i<4;i++){
int tGx=now.Gx+dGx[i];
int tGy=now.Gy+dGy[i];
int tMx=now.Mx+dMx[i];
int tMy=now.My+dMy[i];
if(!check(tGx,tGy)){
tGx=now.Gx;
tGy=now.Gy;
}
if(!check(tMx,tMy)){
tMx=now.Mx;
tMy=now.My;
}
if(check(tGx,tGy) && check(tMx,tMy) && !vis[tGx][tGy][tMx][tMy]){
vis[tGx][tGy][tMx][tMy]=true;
q.push(Node{tGx,tGy,tMx,tMy,now.step+1});
}
}
}
cout<<"no";
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>r>>c;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
cin>>a[i][j];
if(a[i][j]=='G'){
Gsx=i;
Gsy=j;
}
if(a[i][j]=='M'){
Msx=i;
Msy=j;
}
if(a[i][j]=='T'){
ex=i;
ey=j;
}
}
}
bfs(Gsx,Gsy,Msx,Msy,0);
return 0;
}
请在我的代码上进行修改,改对必关!
回复
共 3 条回复,欢迎继续交流。
正在加载回复...