输出全为负一,求大佬解释

P2895 [USACO08FEB] Meteor Shower S

Fu_Tao @ 2021-08-30 10:21:20

#define ll long long
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;
ll n,x,y,t,times[1001][1001],book[1001][1001],ans[1001][1001];
ll head,tail;
struct node{
    ll tx,ty;
}f[10001];
ll next[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int main(){
    cin>>n;
    memset(times,1e7,sizeof(times));
    for(int i=1;i<=n;i++){
        cin>>x>>y>>t;
        times[x][y]=min(times[x][y],t);
        if(x>0)times[x-1][y]=min(times[x-1][y],t);
        times[x+1][y]=min(times[x+1][y],t);
        if(y>0)times[x][y-1]=min(times[x][y-1],t);
        times[x][y+1]=min(times[x][y+1],t);
    }
    book[0][0]=1;
    for(int head=1;head<=tail;head++){
        if(times[f[head].tx][f[head].ty]==1e7){
            cout<<ans[f[head].tx][f[head].ty];
            return 0;
        }
        for(int i=0;i<4;i++){
            ll dx=f[head].tx+next[i][0];
            ll dy=f[head].ty+next[i][1];
            if(dx>0&&dy>0&&ans[f[head].tx][f[head].ty]+1<times[dx][dy]&&book[dx][dy]==0){
                book[dx][dy]=1;
                ans[dx][dy]=ans[f[head].tx][f[head].ty]+1;
                f[++tail].tx=dx;
                f[tail].ty=dy;
            }
        }
    }
    cout<<-1;
    return 0;
} 

有些是借鉴的题解,但是输出是负一


by qwqszxc45rtnhy678ikj @ 2021-08-30 11:11:18

memset不是你这样用科学计数法的,比如memset(times,0x7f,sizeof(times)); 如果你这样写,下面就是0x7f7f7f7f


by Fu_Tao @ 2021-08-30 15:32:13

@zzp110305

#define ll long long
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;
ll n,x,y,t,times[1001][1001],book[1001][1001],ans[1001][1001];
ll head,tail;
struct node{
    ll tx,ty;
}f[10001];
ll next[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int main(){
    cin>>n;
    memset(times,100000007,sizeof(times));
    for(int i=1;i<=n;i++){
        cin>>x>>y>>t;
        times[x][y]=min(times[x][y],t);
        if(x>0)times[x-1][y]=min(times[x-1][y],t);
        times[x+1][y]=min(times[x+1][y],t);
        if(y>0)times[x][y-1]=min(times[x][y-1],t);
        times[x][y+1]=min(times[x][y+1],t);
    }
    book[0][0]=1;
    for(int head=1;head<=tail;head++){
        if(times[f[head].tx][f[head].ty]==100000007){
            cout<<ans[f[head].tx][f[head].ty];
            return 0;
        }
        for(int i=0;i<4;i++){
            ll dx=f[head].tx+next[i][0];
            ll dy=f[head].ty+next[i][1];
            if(dx>0&&dy>0&&ans[f[head].tx][f[head].ty]+1<times[dx][dy]&&book[dx][dy]==0){
                book[dx][dy]=1;
                ans[dx][dy]=ans[f[head].tx][f[head].ty]+1;
                f[++tail].tx=dx;
                f[tail].ty=dy;
            }
        }
    }
    cout<<-1;
    return 0;
} 

这样也是错的


by vectorli1 @ 2021-08-30 16:58:08

如果真的想用 memset 赋值为 inf,可以这样写:

memset(time, 0x3f3f3f3f, sizeof(times));

或者干脆用 for 循环手动赋值。


by qwqszxc45rtnhy678ikj @ 2021-08-31 16:01:52

@醉梦伊人归 啊啊啊,memset是给每个字节复制。


|