暴力枚举大法,再加个广搜,包ac的
2024-07-08 09:18:47
发布于:广东
23阅读
0回复
0点赞
代码放了
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>
using namespace std;
const int MAX_N = 5000;
const int INF = 1e9;
int n, M, K;
vector<pair<int, int>> enemies;
vector<tuple<int, int, int>> zhxTroops;
int dist[MAX_N][MAX_N];
bool isEnemy[MAX_N][MAX_N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int main() {
cin >> n >> M >> K;
enemies.resize(M);
zhxTroops.resize(K);
for (int i = 0; i < M; ++i) {
int x, y;
cin >> x >> y;
enemies[i] = {x - 1, y - 1};
isEnemy[x - 1][y - 1] = true;
}
for (int i = 0; i < K; ++i) {
int sx, sy, t;
cin >> sx >> sy >> t;
zhxTroops[i] = {sx - 1, sy - 1, t};
}
for (int i = 0; i < n; ++i) {
fill(dist[i], dist[i] + n, INF);
}
queue<tuple<int, int, int>> q;
for (const auto& troop : zhxTroops) {
int sx, sy, t;
tie(sx, sy, t) = troop;
q.push({sx, sy, t});
dist[sx][sy] = min(dist[sx][sy], t);
}
while (!q.empty()) {
int x, y, t;
tie(x, y, t) = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < n && dist[nx][ny] > t + 1) {
dist[nx][ny] = t + 1;
q.push({nx, ny, t + 1});
}
}
}
int maxTime = 0;
for (const auto& enemy : enemies) {
int x = enemy.first;
int y = enemy.second;
maxTime = max(maxTime, dist[x][y]);
}
cout << maxTime << endl;
return 0;
}
这里空空如也
有帮助,赞一个