Timeline
Timeline
2025-11-02
init
Problem:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 | using std::vector;class Solution { public: int countUnguarded(int m, int n, vector<vector<int> > &guards, vector<vector<int> > &walls) { // 0 as not protected, 1 as protected, 2 as guards, 3 as walls // inital as not protected(0) vector<vector<int> > grid_map(m, vector<int>(n, 0)); int i, j; int len; int g_i, g_j; int count = 0; len = walls.size(); for (i = 0; i < len; i++) { grid_map[walls[i][0]][walls[i][1]] = 3; } len = guards.size(); for (i = 0; i < len; i++) { grid_map[guards[i][0]][guards[i][1]] = 2; } for (i = 0; i < len; i++) { // guards[i][0], guards[i][1] g_i = guards[i][0]; g_j = guards[i][1]; // Up j = 1; while (g_i - j >= 0) { if (grid_map[g_i - j][g_j] == 2 || grid_map[g_i - j][g_j] == 3) { break; } grid_map[g_i - j][g_j] = 1; j++; } // Down j = 1; while (g_i + j < m) { if (grid_map[g_i + j][g_j] == 2 || grid_map[g_i + j][g_j] == 3) { break; } grid_map[g_i + j][g_j] = 1; j++; } // Left j = 1; while (g_j - j >= 0) { if (grid_map[g_i][g_j - j] == 2 || grid_map[g_i][g_j - j] == 3) { break; } grid_map[g_i][g_j - j] = 1; j++; } // Right j = 1; while (g_j + j < n) { if (grid_map[g_i][g_j + j] == 2 || grid_map[g_i][g_j + j] == 3) { break; } grid_map[g_i][g_j + j] = 1; j++; } } for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { if (grid_map[i][j] == 0) { count++; } } } return count; }};int main(){ vector<vector<int> > guards = { { 0, 0 }, { 1, 1 }, { 2, 3 } }; vector<vector<int> > walls = { { 0, 1 }, { 2, 2 }, { 1, 4 } }; int m=4, n=6; Solution s; printf("%d\n",s.countUnguarded(m, n, guards, walls));} |
