luogu P2895 [USACO08FEB]Meteor Shower S(bfs)

题目链接:https://www.luogu.com.cn/problem/P2895

题意:输入n,外加n组x y t(分别表示流星砸下的横纵坐标即时间)。砸下后周围4个坐标都被污染。

最开始坐标为(0,0)。

为最少多少次能达到安全距离。

题解:bfs,知道mp[i][j]=-1(从未被污染过)就ok了。注意很多小细节。。。。(mad之前想的什么东西,快被这一题耗尽了热情)。

代码:

#include 

#define ll int
#define pi acos(-1)
#define pb push_back
#define mst(a, i) memset(a, i, sizeof(a))
#define pll pair
#define fi first
#define se second
#define dbg(x) cout << #x << "===" << x << endl
#define dbgg(i, x) cout << #x << "[" << i << "]===" << x[i] << endl
using namespace std;
template  void read(T &x) {
    T res = 0, f = 1;
    char c = getchar();
    while (!isdigit(c)) {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (isdigit(c)) {
        res = (res << 3) + (res << 1) + c - '0';
        c = getchar();
    }
    x = res * f;
}
void print(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
const ll maxn = 3e2 + 10;
const ll mod = 1e9 + 7;

ll n, x, y, t;
ll mp[maxn][maxn], vis[maxn][maxn];
ll dis[maxn][maxn];
ll dx[] = {0, 0, 1, 0, -1}, dy[] = {0, 1, 0, -1, 0};
ll ch(ll x) {
    if (x == -1)
        return 1e9;
    else
        return x;
}
int main() {
    ll _s = 1;
    // read(_s);
    for (ll _ = 1; _ <= _s; _++) {
        mst(mp, -1);
        read(n);
        for (ll i = 1; i <= n; i++) {
            read(x), read(y), read(t);
            for (ll j = 0; j < 5; j++) {
                ll nx = x + dx[j], ny = y + dy[j];
                if (nx >= 0 && ny >= 0 && (mp[nx][ny] == -1 || mp[nx][ny] > t))
                    mp[nx][ny] = t;
            }
        }
        queue q;
        vis[0][0] = 1;
        q.push(make_pair(0, 0));
        while (!q.empty()) {
            pll now = q.front();
            q.pop();
            x = now.fi, y = now.se;
            ll s = dis[x][y] + 1;
            if (mp[x][y] == -1) {
                cout << s - 1 << endl;
                return 0;
            }
            for (ll i = 1; i <= 4; i++) {
                ll nx = x + dx[i], ny = y + dy[i];
                if (nx >= 0 && ny >= 0 && s < ch(mp[nx][ny]) &&
                    vis[nx][ny] == 0) {
                    q.push(make_pair(nx, ny));
                    vis[nx][ny] = 1;
                    dis[nx][ny] = s;
                }
            }
        }
        cout << -1 << endl;
    }
    return 0;
}
/*
input:::
output:::

*/

 

你可能感兴趣的:(#,ACM_杂项,算法)