28分求助! ! !

P2895 [USACO08FEB] Meteor Shower S

Libingyue2011 @ 2023-01-22 16:21:22


#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
};
int a[310][310],t[310][310];
bool back(int x,int y){
    return (x>=0 && y>=0 && x<=305 && y<=305);
}
int n,wk[4][2]={{0,1},{1,0},{-1,0},{0,-1}}; 
queue<node>q;
void print(){
    for(int i=0;i<=10;i++){
        for(int j=0;j<=10;j++){
            cout<<a[i][j]<<" ";
        }
        cout<<"\n";
    }
} 
int main() {
    memset(a,255,sizeof a);
    memset(t,127,sizeof t);
    cin>>n;
    for(int i=1;i<=n;i++){
        int x2,y2,t2;
        cin>>x2>>y2>>t2;
        t[x2][y2]=min(t[x2][y2],t2);
        for(int i=0;i<4;i++){
            if(back(x2+wk[i][0],y2+wk[i][1])){
                t[x2+wk[i][0]][y2+wk[i][1]]=min(t[x2][y2],t2);
            }
        }
    }
    a[0][0]=0;
    q.push((node){0,0});
    while(!q.empty()){
        node u=q.front();
        int ux=u.x,uy=u.y;
        q.pop();
//      if(t[ux][uy]>2e9){
//          cout<<a[ux][uy];
//          return 0;
//      }
        for(int i=0;i<4;i++){
            int wx=ux+wk[i][0],wy=uy+wk[i][1];
            if(!back(wx,wy) || a[ux][uy]+1>=t[wx][wy] || a[wx][wy]!=-1){
                continue;
            }
            a[wx][wy]=a[ux][uy]+1;
            q.push((node){wx,wy});
        }
    }
    int ans=2e9;
    for(int i=0;i<=305;i++){
        for(int j=0;j<=305;j++){
            if(t[i][j]>1e9 && a[i][j]!=-1){
                ans=min(ans,a[i][j]);
            }
        }
    }
    if(ans==2e9){
        puts("-1");
    }
    else{
        cout<<ans<<"\n";
    }
    print();
    return 0;
}

by Zjc20120331 @ 2023-01-22 16:34:45

puts的问题(吗?)


|