洪水填充
- 设置路径信息进行剪枝和统计,类似感染的过程
- 路径信息不撤销,可以保证每一片的感染过程能够区分开
- 遍历次数和样本数量的规模一致
| #include <iostream> |
| #include <vector> |
| #include <stack> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int rows; |
| int columns; |
| |
| void dfs(vector<vector<char>> &grid, int row, int col) { |
| |
| if (row < 0 || row >= rows |
| || col < 0 || col >= columns |
| || grid[row][col] != '1') |
| return; |
| |
| grid[row][col] = 0; |
| dfs(grid, row - 1, col); |
| dfs(grid, row + 1, col); |
| dfs(grid, row, col - 1); |
| dfs(grid, row, col + 1); |
| } |
| |
| int numIslands(vector<vector<char>> &grid) { |
| rows = grid.size(); |
| columns = grid[0].size(); |
| int res = 0; |
| |
| |
| for (int i = 0; i < rows; ++i) { |
| for (int j = 0; j < columns; ++j) { |
| if (grid[i][j] == '1') { |
| dfs(grid, i, j); |
| res++; |
| } |
| } |
| } |
| |
| return res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <stack> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int rows; |
| int columns; |
| |
| bool isCoordinateLegal(int row, int col) { |
| return row >= 0 && row < rows && col >= 0 && col < columns; |
| } |
| |
| void convert2F(vector<vector<char>> &board, int row, int col) { |
| |
| if (!isCoordinateLegal(row, col) || board[row][col] != 'O') return; |
| |
| board[row][col] = 'F'; |
| convert2F(board, row - 1, col); |
| convert2F(board, row + 1, col); |
| convert2F(board, row, col - 1); |
| convert2F(board, row, col + 1); |
| } |
| |
| void solve(vector<vector<char>> &board) { |
| rows = board.size(); |
| columns = board[0].size(); |
| |
| |
| for (int j = 0; j < columns; ++j) { |
| if (board[0][j] == 'O') convert2F(board, 0, j); |
| if (board[rows - 1][j] == 'O') convert2F(board, rows - 1, j); |
| } |
| |
| |
| for (int i = 0; i < rows; ++i) { |
| if (board[i][0] == 'O') convert2F(board, i, 0); |
| if (board[i][columns - 1] == 'O') convert2F(board, i, columns - 1); |
| } |
| |
| |
| |
| for (int i = 0; i < rows; ++i) { |
| for (int j = 0; j < columns; ++j) { |
| if (board[i][j] == 'O') board[i][j] = 'X'; |
| if (board[i][j] == 'F') board[i][j] = 'O'; |
| } |
| } |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <stack> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int rows; |
| int columns; |
| |
| bool isCoordinateLegal(int row, int col) { |
| return row >= 0 && row < rows && col >= 0 && col < columns; |
| } |
| |
| void dfs(vector<vector<int>> &grid, int row, int col, int id) { |
| if (!isCoordinateLegal(row, col) || grid[row][col] != 1) return; |
| |
| grid[row][col] = id; |
| dfs(grid, row - 1, col, id); |
| dfs(grid, row + 1, col, id); |
| dfs(grid, row, col - 1, id); |
| dfs(grid, row, col + 1, id); |
| } |
| |
| int largestIsland(vector<vector<int>> &grid) { |
| int id = 2; |
| rows = grid.size(); |
| columns = grid[0].size(); |
| |
| |
| for (int i = 0; i < rows; ++i) |
| for (int j = 0; j < columns; ++j) |
| if (grid[i][j] == 1) |
| dfs(grid, i, j, id++); |
| |
| |
| vector<int> sizes(id, 0); |
| |
| int res = 0; |
| for (int i = 0; i < rows; ++i) { |
| for (int j = 0; j < columns; ++j) { |
| if (grid[i][j] <= 1) continue; |
| res = max(res, ++sizes[grid[i][j]]); |
| } |
| } |
| |
| vector<bool> visited(id, false); |
| for (int i = 0; i < rows; ++i) { |
| for (int j = 0; j < columns; ++j) { |
| if (grid[i][j] != 0) continue; |
| |
| int up = i > 0 ? grid[i - 1][j] : 0; |
| int down = i + 1 < rows ? grid[i + 1][j] : 0; |
| int left = j > 0 ? grid[i][j - 1] : 0; |
| int right = j + 1 < columns ? grid[i][j + 1] : 0; |
| |
| visited[up] = true; |
| int merge = 1 + sizes[up]; |
| if (!visited[down]) { |
| merge += sizes[down]; |
| visited[down] = true; |
| } |
| if (!visited[left]) { |
| merge += sizes[left]; |
| visited[left] = true; |
| } |
| if (!visited[right]) { |
| merge += sizes[right]; |
| visited[right] = true; |
| } |
| res = max(res, merge); |
| visited[up] = false; |
| visited[down] = false; |
| visited[left] = false; |
| visited[right] = false; |
| } |
| } |
| |
| return res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <stack> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int rows; |
| int columns; |
| |
| bool isCoordinateLegal(int row, int col) { |
| return row >= 0 && row < rows && col >= 0 && col < columns; |
| } |
| |
| |
| int convertTo2(vector<vector<int>> &grid, int row, int col) { |
| if (!isCoordinateLegal(row, col) || grid[row][col] != 1) return 0; |
| grid[row][col] = 2; |
| return 1 |
| + convertTo2(grid, row - 1, col) |
| + convertTo2(grid, row + 1, col) |
| + convertTo2(grid, row, col - 1) |
| + convertTo2(grid, row, col + 1); |
| } |
| |
| |
| bool connectTo2(vector<vector<int>> &grid, int row, int col) { |
| return (row > 0 && grid[row - 1][col] == 2) |
| || (row + 1 < rows && grid[row + 1][col] == 2) |
| || (col > 0 && grid[row][col - 1] == 2) |
| || (col + 1 < columns && grid[row][col + 1] == 2); |
| } |
| |
| vector<int> hitBricks(vector<vector<int>> &grid, vector<vector<int>> &hits) { |
| rows = grid.size(); |
| columns = grid[0].size(); |
| |
| |
| for (auto &hit: hits) |
| grid[hit[0]][hit[1]]--; |
| |
| |
| for (int j = 0; j < columns; ++j) |
| convertTo2(grid, 0, j); |
| |
| vector<int> res(hits.size(), 0); |
| |
| for (int i = hits.size() - 1; i >= 0; i--) { |
| int x = hits[i][0]; |
| int col = hits[i][1]; |
| grid[x][col]++; |
| |
| if (grid[x][col] <= 0) continue; |
| |
| if (connectTo2(grid, x, col) || x == 0) |
| |
| res[i] = convertTo2(grid, x, col) - 1; |
| } |
| return res; |
| } |
| }; |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18402764
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步