话说有一天 linyorson 在我的世界开了一个 �×�n×n 的方阵现在他有 �m 个火把和 �k 个萤石分别放在 �1�1∼����x 1 y 1 ∼x m y m 和 �1�1∼����o 1 p 1 ∼o k p k 的位置没有光并且没放东西的地方会生成怪物。请问在这个方阵中有几个点会生成怪物?PS 火把的照亮范围是: 暗暗 光 暗暗 暗光 光 光暗
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int countMonsters(int n, int m, int k, vector<pair<int, int>>& torches, vector<pair<int, int>>& glowstones) {
int count = 0;
vector<vector<int>> grid(n, vector<int>(n, 0)); // 0: no light, 1: torch light, 2: glowstone light
// Place torches
for (const auto& torch : torches) {
int x = torch.first;
int y = torch.second;
grid[x-1][y-1] = 1;
// Light up surrounding cells
for (int i = max(0, x-2); i <= min(n-1, x); i++) {
for (int j = max(0, y-2); j <= min(n-1, y); j++) {
if (grid[i][j] == 0) {
grid[i][j] = 1;
}
}
}
}
// Place glowstones
for (const auto& glowstone : glowstones) {
int x = glowstone.first;
int y = glowstone.second;
grid[x-1][y-1] = 2;
}
// Count cells with no light
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
count++;
}
}
}
return count;
}
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, int>> torches(m);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
torches[i] = make_pair(x, y);
}
vector<pair<int, int>> glowstones(k);
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
glowstones[i] = make_pair(x, y);
}
int result = countMonsters(n, m, k, torches, glowstones);
cout << result << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/jgkm 著作权归作者所有。请勿转载和采集!