749.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <iostream>
  2. #include <vector>
  3. using std::vector;
  4. using std::cout;
  5. using std::endl;
  6. class Solution {
  7. public:
  8. int containVirus(vector<vector<int>> &grid) {
  9. int walls = 0;
  10. do {
  11. walls += build_wall(grid);
  12. } while (spread(grid));
  13. return walls;
  14. }
  15. private:
  16. int build_wall(vector<vector<int>> &grid) {
  17. bool flag[grid.size()][grid[0].size()];
  18. memset(flag, 0, sizeof(flag));
  19. return 0;
  20. }
  21. bool spread(vector<vector<int>> &grid) {
  22. for (int i = 0; i < grid.size(); i++) {
  23. vector<int> &col = grid[i];
  24. for (int j = 0; j < col.size(); j++) {
  25. for (int k = 0; k < 4; k++) {
  26. int x = i + dx[k], y = j + dy[k];
  27. if (x < 0 || grid.size() <= x || y < 0 || col.size() <= y) continue;
  28. if (grid[x][y] == 1 && grid[i][j] == 0) grid[i][j] = -1;
  29. }
  30. }
  31. }
  32. int cnt = 0;
  33. for (vector<int> &col : grid) {
  34. for (int &row : col) {
  35. if (row == -1) {
  36. row = 1;
  37. cnt++;
  38. }
  39. }
  40. }
  41. return 0 < cnt;
  42. }
  43. int dx[4] = {-1, 0, 1, 0};
  44. int dy[4] = {0, -1, 0, 1};
  45. };
  46. int main() {
  47. vector<vector<int>> grid =
  48. {{1, 1, 1, 0, 0, 0, 0, 0, 0},
  49. {1, 0, 1, 0, 1, 1, 1, 1, 1},
  50. {1, 1, 1, 0, 0, 0, 0, 0, 0}};
  51. Solution* sol = new Solution();
  52. cout << "actual: " << sol->containVirus(grid) << ", expected: " << 13 << endl;
  53. return 0;
  54. }