https://leetcode.com/problems/count-ung ... 2024-11-21
Вот мое решение:
Код: Выделить всё
class Solution {
public:
// Helper to mark cells guarded in a specific direction
void markGuarded(int i, int j, int di, int dj, int m, int n, vector& arr) {
while (i >= 0 && i < m && j >= 0 && j < n) {
if (arr[i][j] == 1 || arr[i][j] == 2) break; // Stop at walls or guards
if (arr[i][j] == 0) arr[i][j] = 4; // Mark as guarded
i += di; // Move in the given direction
j += dj;
}
}
int countUnguarded(int m, int n, vector& guards, vector& walls) {
vector arr(m, vector(n, 0));
// Mark guards and walls on the grid
for (auto& g : guards) arr[g[0]][g[1]] = 2; // Guard
for (auto& w : walls) arr[w[0]][w[1]] = 1; // Wall
// Traverse for each guard
for (auto& g : guards) {
int i = g[0], j = g[1];
markGuarded(i, j, 0, -1, m, n, &arr); // Left
markGuarded(i, j, 0, 1, m, n, &arr); // Right
markGuarded(i, j, -1, 0, m, n, &arr); // Up
markGuarded(i, j, 1, 0, m, n, &arr); // Down
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79217003/c-vector-compile-error-for-passing-by-reference[/url]