FloodFill问题
class Solution {
queue<pair<int, int>> q;
int original_color;
int new_color;
unordered_multimap<int, int> direction = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0}
};
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
original_color = image[sr][sc], new_color = color;
if (original_color == new_color) return image;
image[sr][sc] = new_color;
q.push({ sr, sc });
while (!q.empty()) {
auto base = q.front();
q.pop();
for (auto& e : direction) {
int x = base.first + e.first, y = base.second + e.second;
if ((x >= 0 && x < image.size())
&& (y >= 0 && y < image[0].size())
&& (image[x][y] == original_color)) {
image[x][y] = new_color;
q.push({ x, y });
}
}
}
return image;
}
};
class Solution {
vector<vector<bool>> mark;
queue<pair<int, int>> q;
int ret = 0;
unordered_multimap<int, int> direction = {
{0, 1}, // 右
{0, -1}, // 左
{1, 0}, // 上
{-1, 0} // 下
};
public:
void bfs(vector<vector<char>>& grid) {
while (!q.empty()) {
auto base = q.front();
q.pop();
for (auto& e : direction) {
int x = base.first + e.first, y = base.second + e.second;
if ((x >= 0 && x < grid.size())
&& (y >= 0 && y < grid[0].size())
&& (mark[x][y] == false)
&& (grid[x][y] == '1')) {
mark[x][y] = true;
q.push({x, y});
}
}
}
}
int numIslands(vector<vector<char>>& grid) {
int m = grid.size(), n = grid[0].size();
mark = vector<vector<bool>>(m, vector<bool>(n, false));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mark[i][j] == false && grid[i][j] == '1') {
mark[i][j] = true;
q.push({i, j});
bfs(grid);
++ret;
}
}
}
return ret;
}
};
class Solution {
vector<vector<bool>> mark;
queue<pair<int, int>> q;
int max_area = 0;
unordered_multimap<int, int> direction = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
};
public:
int bfs(vector<vector<int>>& grid) {
int area = 1;
while (!q.empty()) {
auto base = q.front();
q.pop();
for (auto& e : direction) {
int x = base.first + e.first, y = base.second + e.second;
if ((x >= 0 && x < grid.size())
&& (y >= 0 && y < grid[0].size())
&& (mark[x][y] == false)
&& (grid[x][y] == 1)) {
mark[x][y] = true;
q.push({x, y});
++area;
}
}
}
return area;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
mark = vector<vector<bool>>(m, vector<bool>(n, false));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mark[i][j] == false && grid[i][j] == 1) {
mark[i][j] = true;
q.push({i, j});
max_area = max(max_area, bfs(grid));
}
}
}
return max_area;
}
};
class Solution {
vector<vector<bool>> mark;
queue<pair<int, int>> q;
unordered_multimap<int, int> direction = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
};
public:
void dfs(vector<vector<char>>& board) {
while (!q.empty()) {
auto base = q.front();
q.pop();
for (auto& e : direction) {
int x = base.first + e.first, y = base.second + e.second;
if ((x >= 0 && x < board.size())
&& (y >= 0 && y < board[0].size())
&& (mark[x][y] == false)
&& (board[x][y] == 'O')) {
mark[x][y] = true;
q.push({x, y});
}
}
}
}
void solve(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size();
mark = vector<vector<bool>>(m, vector<bool>(n, false));
// 第一行 board[0][j]
// 最后一行 board[m-1][j]
for (int j = 0; j < n; ++j) {
if (mark[0][j] == false && board[0][j] == 'O') {
mark[0][j] = true;
q.push({0, j});
dfs(board);
}
if (mark[m-1][j] == false && board[m-1][j] == 'O') {
mark[m-1][j] = true;
q.push({m-1, j});
dfs(board);
}
}
// 第一列 board[i][0]
// 最后一列 board[i][n-1]
for (int i = 0; i < m; ++i) {
if (mark[i][0] == false && board[i][0] == 'O') {
mark[i][0] = true;
q.push({i, 0});
dfs(board);
}
if (mark[i][n-1] == false && board[i][n-1] == 'O') {
mark[i][n-1] = true;
q.push({i, n-1});
dfs(board);
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mark[i][j] == false && board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
}
};