P2895 [USACO08FEB]Meteor Shower S
作者:互联网
// Problem: P2895 [USACO08FEB]Meteor Shower S
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2895
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn
#include <bits/stdc++.h>
using namespace std;
template<class T>
void debugVector(const T &a) {
cout << "[ ";
for (size_t i = 0; i < a.size(); ++i) {
cout << a[i] << (i == a.size() - 1 ? " " : ", ");
}
cout << "]" << endl;
}
template<class T1, class T2>
void debugVectorPair(const vector<pair<T1, T2>> &a) {
cout << "{";
for (size_t i = 0; i < a.size(); ++i) {
cout << "[ " << a[i].first << ": " << a[i].second << (i == a.size() - 1 ? " ]" : " ], ");
}
cout << "}" << endl;
}
template<class T>
void debugMatrix2(const T &a) {
for (size_t i = 0; i < a.size(); ++i) {
debugVector(a[i]);
}
}
template<class T>
using matrix2 = vector<vector<T>>;
template<class T>
vector<vector<T>> getMatrix2(size_t n, size_t m, T init = T()) {
return vector<vector<T>>(n, vector<T>(m, init));
}
template<class T>
using matrix3 = vector<vector<vector<T>>>;
template<class T>
vector<vector<vector<T>>> getMatrix3(size_t x, size_t y, size_t z, T init = T()) {
return vector<vector<vector<T>>>(x, vector<vector<T>>(y, vector<T>(z, init)));
}
vector<int> genBigInteger(const string &a) {
vector<int> res;
if (a.empty()) return res;
for (int i = a.size() - 1; i >= 0; --i) {
res.push_back(a[i] - '0');
}
return res;
}
ostream &printBigInteger(const vector<int> &a) {
if (a.empty()) return cout;
for (int i = a.size() - 1; i >= 0; --i) {
cout << a[i];
}
return cout;
}
vector<int> maxBigInteger(const vector<int> &a, const vector<int> &b) {
if (a.size() > b.size()) {
return a;
} else if (a.size() < b.size()) {
return b;
}
for (int i = a.size() - 1; i >= 0; --i) {
if (a[i] > b[i]) {
return a;
} else if (a[i] < b[i]) {
return b;
}
}
return a;
}
vector<int> addBigInteger(const vector<int> &a, const vector<int> &b) {
vector<int> res;
int pre = 0;
for (size_t i = 0; i < a.size() || i < b.size() || pre; ++i) {
if (i < a.size()) pre += a[i];
if (i < b.size()) pre += b[i];
res.push_back(pre % 10);
pre /= 10;
}
while (res.size() > 1 && res.back() == 0) {
res.pop_back();
}
return res;
}
vector<int> mulBigInteger(const vector<int> &a, int b) {
vector<int> res;
int pre = 0;
for (size_t i = 0; i < a.size() || pre; ++i) {
if (i < a.size()) pre += a[i] * b;
res.push_back(pre % 10);
pre /= 10;
}
while (res.size() > 1 && res.back() == 0) {
res.pop_back();
}
return res;
}
vector<int> divBigInteger(const vector<int> &a, int b, int &r) {
vector<int> res;
for (int i = a.size() - 1; i >= 0; --i) {
r = 10 * r + a[i];
res.push_back(r / b);
r %= b;
}
reverse(res.begin(), res.end());
while (res.size() > 1 && res.back() == 0) {
res.pop_back();
}
return res;
}
int bfs(matrix2<int> &info) {
if (info[0][0] == 0) {
return -1;
}
info[0][0] = 0;
vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, 1, 0, -1};
queue<pair<int, int>> q;
q.push({0, 0});
while (!q.empty()) {
int tx = q.front().first;
int ty = q.front().second;
// 如果 范围在合法区间内 且 能够到达的时间小于变成焦土的时间 且 在此前没有到达过 更新到达此点的最短时间
for (int i = 0; i < dx.size(); ++i) {
if (tx + dx[i] > 310 || tx + dx[i] < 0 || ty + dy[i] > 310 || ty + dy[i] < 0 ||
info[tx + dx[i]][ty + dy[i]] == 0 ||
info[tx + dx[i]][ty + dy[i]] < 0 && info[tx][ty] + 1 >= -info[tx + dx[i]][ty + dy[i]] ||
info[tx + dx[i]][ty + dy[i]] > 0 && info[tx][ty] + 1 >= info[tx + dx[i]][ty + dy[i]]) {
continue;
} else {
if (info[tx + dx[i]][ty + dy[i]] == INT_MAX) {
return info[tx][ty] + 1;
}
info[tx +dx[i]][ty + dy[i]] = info[tx][ty] + 1;
q.push({tx + dx[i], ty + dy[i]});
}
}
q.pop();
}
return -1;
}
// 坑点:move to a new, safer location
// 不能在原地不动,没有翻译
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int m;
cin >> m;
vector<int> dx = {0, -1, 0, 1, 0};
vector<int> dy = {0, 0, 1, 0, -1};
matrix2<int> info = getMatrix2(311, 311, INT_MAX);
// 区域最早在t时间变为焦土。其值存储为-t。当遇到一个INT_MAX的格子,安全的格子
for (int i = 0; i < m; ++i) {
int x, y, t;
cin >> x >> y >> t;
for (int j = 0; j < dx.size(); ++j) {
if (x + dx[j] > 310 || x + dx[j] < 0 || y + dy[j] > 310 || y + dy[j] < 0 ||
info[x + dx[j]][y + dy[j]] == 0 ||
info[x + dx[j]][y + dy[j]] < 0 && info[x + dx[j]][y + dy[j]] > -t) {
continue;
} else {
info[x + dx[j]][y + dy[j]] = -t;
}
}
}
cout << bfs(info) << endl;
return 0;
}
标签:info,res,Shower,vector,dx,dy,USACO08FEB,P2895,size 来源: https://www.cnblogs.com/pannnn/p/16092100.html