leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2257.cpp (1104B)
0 class Solution { 1 public: 2 int countUnguarded(const int n, const int m, const vector<vector<int>> &guards, 3 const vector<vector<int>> &walls) const { 4 const auto idx = [m](int x, int y) { return x * m + y; }; 5 6 bitset<100001> stop = 0, seen = 0; 7 for (const auto &wall : walls) 8 stop.set(idx(wall[0], wall[1])); 9 for (const auto &guard : guards) 10 stop.set(idx(guard[0], guard[1])); 11 12 static const int offset[] = {-1, 0, 1, 0, -1}; 13 14 int res = m * n - size(walls) - size(guards); 15 for (const auto &guard : guards) { 16 for (int k = 0; k < 4; k++) { 17 int x = guard[0] + offset[k], y = guard[1] + offset[k + 1]; 18 while (x >= 0 && x < n && y >= 0 && y < m) { 19 const int index = idx(x, y); 20 if (stop.test(index)) break; 21 if (!seen.test(index)) res--; 22 seen.set(index); 23 x += offset[k], y += offset[k + 1]; 24 } 25 } 26 } 27 28 return res; 29 } 30 };