BZOJ 1611 Usaco Meteor Shower

好多奶牛題目是真的坑啊,不標數據範圍或者題目描述殘缺。。。
讓我WA了3次才過。spa

迴歸正題,一道不算難的BFS題目。
注意棋盤範圍是301 若是走出301也算成功逃生(無語=W=)code

#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
 
struct Point{
    int x,y,Time;
};
 
std::queue<Point>Q;
 
const int INF=0x3f3f3f3f;
int n,End=-1;
int x,y,t;
int vis[305][305];
int Die[305][305];
 
int fx[5]={0,0,-1,0,1},fy[5]={0,-1,0,1,0};
 
void BFS(){
    while( !Q.empty() ){
        Point Start = Q.front();
        Q.pop();
        for(int i=1;i<=4;i++){
            int tx = Start.x+fx[i];
            int ty = Start.y+fy[i];
            if(tx>=301 && ty>=301){
                End = Start.Time+1;
                return;
            }
            if(tx>=0 && ty>=0 && tx<=301 && ty<=301 && !vis[tx][ty] ){
                if(Start.Time+1>=Die[tx][ty]) continue;
                if(Die[tx][ty]==INF){
                    End = Start.Time+1;
                    return;
                }
                vis[tx][ty]=1;
                Point now;
                now.x=tx;
                now.y=ty;
                now.Time=Start.Time+1;
                Q.push(now);
            }
        }
    }
    return;
}
 
int main(){
    memset(Die,INF,sizeof(Die));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&x,&y,&t);
        Die[x][y]=std::min(Die[x][y],t);
        for(int i=1;i<=4;i++){
            int nx = x+fx[i];
            int ny = y+fy[i];
            if(ny >= 0 && nx>=0 && nx<=301 && ny<=301) Die[nx][ny]=std::min(Die[nx][ny],t);
        }
    }
    Point New;
    New.x=0;
    New.y=0;
    New.Time=0; 
    vis[0][0]=1;
    Q.push(New);
    if(Die[0][0]==INF){
        printf("0\n");
        return 0;
    }
    BFS();
    printf("%d\n",End);
    return 0;
}
相關文章
相關標籤/搜索