POJ - 3669

POJ - 3669

BFS 预处理

女主要从\((0,0)\) 开始逃到一个没有流星砸到的地方,这题只给了流星砸落的时间没有给整个地图

地图边界按最大值计算,女主到达某个地方的时间必须要小于流星砸落的时间就能继续走。

预处理流星砸落的时间即可

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
#define endl '\n'
const int N = 350,INF = 0x3f3f3f3f;
int g[N][N],n,dist[N][N];
int dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0},ans;
bool check(int x,int y) {
    return x >= 0 && x < N && y >= 0 && y < N;
}
struct node {
    int x,y;
};
int bfs() {
    if(g[0][0] == 0) return -1;
    queue<node> q;
    q.push({0,0});
    dist[0][0] = 0;
    while(q.size()) {
        node t = q.front();
        q.pop();
        if(g[t.x][t.y] == INF) return dist[t.x][t.y];
        for(int i = 0;i < 4; ++i) {
            int nx = t.x + dx[i],ny = t.y + dy[i];
            if(check(nx,ny) && dist[nx][ny] == INF && dist[t.x][t.y] + 1 < g[nx][ny]) {
                q.push({nx,ny});
                dist[nx][ny] = dist[t.x][t.y] + 1;
            }
        }
    }
    return -1;

}
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int x,y,t;
    memset(g,0x3f,sizeof g);
    memset(dist,0x3f,sizeof dist);
    cin >> n;
    while(n --) {
        cin >> x >> y >> t;
        g[x][y] = min(g[x][y],t);
        for(int i = 0;i < 4; ++i) {
            int nx = x + dx[i],ny = y + dy[i];
            if(check(nx,ny)) g[nx][ny] = min(g[nx][ny],t);
        }
    }
    cout << bfs() << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lukelmouse/p/12442357.html
今日推荐