h * w 차원의 그리드가 주어졌다고 가정합니다. 그리드의 셀에는 전구 또는 장애물이 포함될 수 있습니다. 전구 셀은 자체 조명과 오른쪽, 왼쪽, 위, 아래에 있는 셀을 조명하며 장애물 셀이 빛을 차단하지 않는 한 빛이 셀을 통해 비출 수 있습니다. 장애물 셀은 조명을 받을 수 없으며 전구 셀에서 다른 셀에 도달하는 빛을 차단합니다. 따라서 'bulb' 배열의 그리드에서 전구 셀의 위치와 'obstacles' 배열의 장애물 셀 위치가 주어지면 조명을 받는 그리드의 총 셀 수를 찾아야 합니다.
따라서 입력이 h =4, w =4, 전구 ={{1, 1}, {2, 2}, {3, 3}}, 장애물 ={{0, 0}, {2, 3과 같으면 }}, 출력은 13이 됩니다.

이미지에서 격자에서 조명된 셀을 볼 수 있습니다.
이 문제를 해결하기 위해 다음 단계를 따릅니다. −
bulbSize := size of bulb
blockSize := size of obstacle
Define one 2D array grid
for initialize i := 0, when i < bulbSize, update (increase i by 1), do:
x := first value of bulb[i]
y := second value of bulb[i]
grid[x, y] := 1
for initialize i := 0, when i < blockSize, update (increase i by 1), do:
x := first value of obstacle[i]
y := first value of obstacle[i]
grid[x, y] := 2
result := h * w
Define one 2D array check
for initialize i := 0, when i < h, update (increase i by 1), do:
gd := 0
for initialize j := 0, when j < w, update (increase j by 1), do:
if grid[i, j] is same as 2, then:
gd := 0
if grid[i, j] is same as 1, then:
gd := 1
check[i, j] := check[i, j] OR gd
gd := 0
for initialize j := w - 1, when j >= 0, update (decrease j by 1), do:
if grid[i, j] is same as 2, then:
gd := 0
if grid[i, j] is same as 1, then:
gd := 1
check[i, j] := check[i, j] OR gd
for initialize j := 0, when j < w, update (increase j by 1), do:
k := 0
for initialize i := 0, when i < h, update (increase i by 1), do:
if grid[i, j] is same as 2, then:
k := 0
if grid[i, j] is same as 1, then:
k := 1
check[i, j] := check[i, j] OR k
k := 0
for initialize i := h - 1, when i >= 0, update (decrease i by 1), do:
if grid[i, j] is same as 2, then:
k := 0
if grid[i, j] is same as 1, then:
k := 1
check[i, j] := check[i, j] OR k
for initialize i := 0, when i < h, update (increase i by 1), do:
for initialize j := 0, when j < w, update (increase j by 1), do:
result := result - NOT(check[i, j])
return result 예시
이해를 돕기 위해 다음 구현을 살펴보겠습니다. −
#include <bits/stdc++.h>
using namespace std;
int solve(int h, int w, vector<pair<int, int>> bulb, vector<pair<int, int>> obstacle){
int bulbSize = bulb.size();
int blockSize = obstacle.size();
vector<vector<int>> grid(h, vector<int>(w, 0));
for (int i = 0; i < bulbSize; i++) {
int x = bulb[i].first;
int y = bulb[i].second;
grid[x][y] = 1;
}
for (int i = 0; i < blockSize; i++) {
int x = obstacle[i].first;
int y = obstacle[i].second;
grid[x][y] = 2;
}
int result = h * w;
vector<vector<bool>> check(h, vector<bool>(w, 0));
for (int i = 0; i < h; i++) {
bool gd = 0;
for (int j = 0; j < w; j++) {
if (grid[i][j] == 2)
gd = 0;
if (grid[i][j] == 1)
gd = 1;
check[i][j] = check[i][j] | gd;
}
gd = 0;
for (int j = w - 1; j >= 0; j--) {
if (grid[i][j] == 2)
gd = 0;
if (grid[i][j] == 1)
gd = 1;
check[i][j] = check[i][j] | gd;
}
}
for (int j = 0; j < w; j++) {
bool k = 0;
for (int i = 0; i < h; i++) {
if (grid[i][j] == 2)
k = 0;
if (grid[i][j] == 1)
k = 1;
check[i][j] = check[i][j] | k;
}
k = 0;
for (int i = h - 1; i >= 0; i--) {
if (grid[i][j] == 2)
k = 0;
if (grid[i][j] == 1) k = 1;
check[i][j] = check[i][j] | k;
}
}
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
result -= !check[i][j];
return result;
}
int main() {
int h = 4, w = 4;
vector<pair<int, int>> bulb = {{1, 1}, {2, 2}, {3, 3}}, obstacle = {{0, 0}, {2, 3}};
cout<< solve(h, w, bulb, obstacle);
return 0;
} 입력
4, 4, {{1, 1}, {2, 2}, {3, 3}}, {{0, 0}, {2, 3}}
출력
13