社区讨论
BFS28分求调,只过了前4个点,其他全WA
P2895[USACO08FEB] Meteor Shower S参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @m1yd17ug
- 此快照首次捕获于
- 2024/10/07 09:55 去年
- 此快照最后确认于
- 2025/11/04 17:45 4 个月前
rt
代码:
CPP#include <queue>
#include <iostream>
using namespace std;
queue<int>mq;
queue<int>mp;
queue<int>sj;
int m[505][505];
bool f[505][505];
int main() {
int x,a,b,c;
cin>>x;
for(int i=0;i<=500;i++){
for(int j=0;j<=500;j++) m[i][j]=-1;
}
for(int i=1; i<=x; i++) {
cin>>a>>b>>c;
if(m[a][b]==-1) m[a][b]=c;
if(m[a+1][b]==-1) m[a+1][b]=c;
if(m[a][b+1]==-1) m[a][b+1]=c;
if(a>0&&m[a-1][b]==-1) m[a-1][b]=c;
if(b>0&&m[a][b-1]==-1) m[a][b-1]=c;
}
/*for(int i=0;i<=x+2;i++){
for(int j=0;j<=x+2;j++){
cout<<m[i][j]<<" ";
}
cout<<endl;
}*/
mq.push(0);
mp.push(0);
sj.push(0);
do {
int top1=mq.front();
int top2=mp.front();
int top3=sj.front();
mq.pop();
mp.pop();
sj.pop();
if(f[top1][top2]==true) continue ;
f[top1][top2]=true;
//cout<<"\n"<<top1<<" "<<top2<<" "<<top3<<"\n";
if(m[top1][top2]==-1) {
cout<<top3;
return 0;
}
if(m[top1+1][top2]==-1){
cout<<top3+1;
return 0;
}
if(m[top1+1][top2]>(top3+1)) {
mq.push(top1+1);
mp.push(top2);
sj.push(top3+1);
}
if(m[top1][top2+1]==-1){
cout<<top3+1;
return 0;
}
if(m[top1][top2+1]>(top3+1)||m[top1][top2+1]==-1) {
mq.push(top1);
mp.push(top2+1);
sj.push(top3+1);
}
if(top1>0) {
if(m[top1-1][top2]==-1){
cout<<top3+1;
return 0;
}
if(m[top1-1][top2]>(top3+1)||m[top1-1][top2]==-1){
mq.push(top1-1);
mp.push(top2);
sj.push(top3+1);
}
}
if(top2>0) {
if(m[top1][top2-1]==-1){
cout<<top3+1;
return 0;
}
if(m[top1][top2-1]>(top3+1)||m[top1][top2-1]==-1) {
mq.push(top1);
mp.push(top2-1);
sj.push(top3+1);
}
}
} while(!mq.empty()&&!mp.empty()&&!sj.empty());
cout<<"-1";
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...