我的世界:计算怪物生成点
话说有一天 linyorson 在'我的世界'开了一个 n*n 的方阵,现在他有 m 个火把和 k 个萤石,分别放在 (x1, y1)∼(xm, ym) 和 (o1, p1)∼(ok, pk) 的位置,没有光并且没放东西的地方会生成怪物。请问在这个方阵中有几个点会生成怪物?
P.S. 火把的照亮范围是:
|暗|暗| 光 |暗|暗|
|暗|光| 光 |光|暗|
|光|光|火把|光|光|
|暗|光| 光 |光|暗|
|暗|暗| 光 |暗|暗|
萤石:
|光|光| 光 |光|光|
|光|光| 光 |光|光|
|光|光|萤石|光|光|
|光|光| 光 |光|光|
|光|光| 光 |光|光|
输入格式 输入共 m+k+1 行。 第一行为 n,m,k。 第 2 到第 m+1 行分别是火把的位置 xi, yi 。 第 m+2 到第 m+k+1 行分别是萤石的位置 oi, pi 。
注:可能没有萤石,但一定有火把。
输出格式 有几个点会生出怪物。
输入输出样例
输入 #1复制
5 1 0
3 3
输出 #1复制
12
c++代码内容:```cpp
#include
int countMonsters(int n, int m, int k, vector<pair<int, int>>& torches, vector<pair<int, int>>& glowstones) {
int count = 0;
vector<vector
// 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/cIAJ 著作权归作者所有。请勿转载和采集!