C++程序:查找网格中被照亮的单元格数量
假设我们有一个h * w维度的网格。网格中的单元格可以包含灯泡或障碍物。一个灯泡单元格可以照亮自身以及其左右上下四个方向的单元格,光线可以穿过单元格,除非障碍物单元格阻挡了光线。障碍物单元格不能被照亮,并且它会阻止灯泡单元格的光线到达其他单元格。因此,给定网格中灯泡单元格的位置(在数组'bulb'中)和障碍物单元格的位置(在数组'obstacles'中),我们必须找出网格中被照亮的单元格总数。
因此,如果输入类似于h = 4,w = 4,bulb = {{1, 1}, {2, 2}, {3, 3}},obstacle = {{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
广告