All_Wrong_Answer @ 2024-10-07 09:53:23
rt
代码:
#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;
}
by All_Wrong_Answer @ 2024-10-07 10:05:44
已过,此贴结
by liaowenqiaa @ 2024-10-07 10:05:52
他刚刚A了,此贴完。