A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls.
The world is modeled as a 2-D array of cells, where 0 represents uninfected cells, and 1 represents cells contaminated with the virus. A wall (and only one wall) can be installed between any two 4-directionally adjacent cells, on the shared boundary.
Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources are limited. Each day, you can install walls around only one region – the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night. There will never be a tie.
Can you save the day? If so, what is the number of walls required? If not, and the world becomes fully infected, return the number of walls used.
Example 1:
Input: grid =
[[0,1,0,0,0,0,0,1],
[0,1,0,0,0,0,0,1],
[0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0]]
Output: 10
Explanation:
There are 2 contaminated regions.
On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is:
[[0,1,0,0,0,0,1,1],
[0,1,0,0,0,0,1,1],
[0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,1]]
On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained.
Example 2:
Input: grid =
[[1,1,1],
[1,0,1],
[1,1,1]]
Output: 4
Explanation: Even though there is only one cell saved, there are 4 walls built.
Notice that walls are only built on the shared boundary of two different cells.
Example 3:
Input: grid =
[[1,1,1,0,0,0,0,0,0],
[1,0,1,0,1,1,1,1,1],
[1,1,1,0,0,0,0,0,0]]
Output: 13
Explanation: The region on the left only builds two new walls.
Note:
The number of rows and columns of grid will each be in the range [1, 50].
Each grid[i][j] will be either 0 or 1.
Throughout the described process, there is always a contiguous viral region that will infect strictly more uncontaminated squares in the next round.
此题实际上是一个模拟+贪心,首先可以简单计算每次感染后得到的边界大小。优先选择感染边界最大的,这样可以减少后续的感染人数。接下来,只需要确定每次感染后的图像,根据这些图像重新一轮计算,直到没有新的区域产生。
感觉这种题是做不出来的
这道题看起来很难,其实仔细分析还可以,用到DFS寻找病毒群,BFS模拟冰毒传播,有点烦,考试的时候估计做不出来
代码如下:
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
using namespace std;
class Solution
{
public:
int containVirus(vector<vector<int>>& grid)
{
int ans = 0;
while (true)
{
int walls = process(grid);
if (walls == 0)
break; // No more walls to build
ans += walls;
}
return ans;
}
private:
int process(vector<vector<int>>& grid)
{
int m = grid.size(), n = grid[0].size();
// cnt is max area to be affected by a single virus region; ans is corresponding walls
int cnt = 0, ans = 0, color = -1, row = -1, col = -1;
// visited virus as 1, visited 0 using different color to indicate being affected by different virus
vector<vector<int>> visited(m, vector<int>(n, 0));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (grid[i][j] == 1 && visited[i][j] == 0)
{
int walls = 0, area = dfs(grid, visited, i, j, color, walls);
if (area > cnt)
{
ans = walls;
cnt = area;
row = i;
col = j;
}
color--;
}
}
}
// set this virus region inactive
buildWall(grid, row, col);
// propagate other virus by 1 step
visited = vector<vector<int>>(m, vector<int>(n, 0));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (grid[i][j] == 1 && visited[i][j] == 0)
spread(grid, visited, i, j);
}
}
return ans;
}
int dfs(vector<vector<int>>& grid, vector<vector<int>>& visited, int row, int col, int color, int& walls)
{
int m = grid.size(), n = grid[0].size(), ans = 0;
if (row < 0 || row >= m || col < 0 || col >= n) return 0;
if (grid[row][col] == 0)
{
walls++;
if (visited[row][col] == color)
return 0;
visited[row][col] = color;
return 1;
}
// grid[row][col] could be -1, inactive virus
if (visited[row][col] == 1 || grid[row][col] != 1)
return 0;
visited[row][col] = 1;
vector<int> dir = { -1, 0, 1, 0, -1 };
for (int i = 0; i < 4; i++)
ans += dfs(grid, visited, row + dir[i], col + dir[i + 1], color, walls);
return ans;
}
void buildWall(vector<vector<int>>& grid, int row, int col)
{
int m = grid.size(), n = grid[0].size();
if (row < 0 || row >= m || col < 0 || col >= n || grid[row][col] != 1)
return;
grid[row][col] = -1; //set inactive
vector<int> dir = { -1, 0, 1, 0, -1 };
for (int i = 0; i < 4; i++)
buildWall(grid, row + dir[i], col + dir[i + 1]);
}
void spread(vector<vector<int>>& grid, vector<vector<int>>& visited, int row, int col)
{
int m = grid.size(), n = grid[0].size();
if (row < 0 || row >= m || col < 0 || col >= n || visited[row][col] == 1) return;
if (grid[row][col] == 0)
{
grid[row][col] = 1;
visited[row][col] = 1;
}
else if (grid[row][col] == 1)
{
visited[row][col] = 1;
vector<int> dir = { -1, 0, 1, 0, -1 };
for (int i = 0; i < 4; i++)
spread(grid, visited, row + dir[i], col + dir[i + 1]);
}
}
};