赞
踩
目录
思路:广度优先搜索一层一层扫描,从上下左右四个方向扫描,直接修改值即可。
- class Solution {
- public:
- typedef pair<int, int> P;
- int dx[4] = {1,-1,0,0,};
- int dy[4] = {0, 0, -1, 1};
- vector<vector<int>> floodFill(vector<vector<int>>& image,
- int sr, int sc, int color) {
- int prev = image[sr][sc];
- if (prev == color) {
- return image;
- }
- int m = image.size(), n = image[0].size();
- queue<P> q;
- q.push(P{sr, sc});
- while (!q.empty()) {
- auto a = q.front();
- image[a.first][a.second] = color;
- q.pop();
- for (int i = 0; i < 4; i++) {
- int x = dx[i] + a.first, y = dy[i] + a.second;
- if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev) {
- q.push(P{x, y});
- }
- }
- }
- return image;
- }
- };
思路:遍历数组每个元素,遇到“1“则岛屿数量加一,然后使用广度优先搜索一层一层扫描,从上下左右四个方向扫描,需要借助一个数组标识当前岛屿的位置,以避免重复统计。
C11支持:
auto [a, b]
是 C++11 引入的结构化绑定(Structured Binding)语法,它允许你一次性声明多个变量,并且这些变量会自动与某个结构体、元组或者其他聚合类型(包括std::pair)的成员对应起来。在这个上下文中,auto [a, b]
就是用来直接解构 queue.front()
返回的 std::pair<int, int>
类型元素,其中 a
和 b
会分别绑定到 pair 中的第一和第二个元素。
- class Solution {
- public:
- int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
- int m, n;
- vector<vector<bool>> vis;
- int numIslands(vector<vector<char>>& grid) {
- int ret = 0;
- m = grid.size(), n = grid[0].size();
- vis.resize(m, vector<bool>(n, false));
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- if (grid[i][j] == '1' && vis[i][j] == false) {
- ret++;
- bfs(grid, i, j);
- }
- }
- }
- return ret;
- }
- void bfs(vector<vector<char>>& grid, int i, int j) {
- queue<pair<int, int>> q;
- q.push({i, j});
- vis[i][j] = true;
- while (!q.empty()) {
- auto [a, b] = q.front();
- q.pop();
- for (int k = 0; k < 4; k++) {
- int x = a + dx[k], y = b + dy[k];
- if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' &&
- vis[x][y] == false) {
- q.push({x, y});
- vis[x][y] = true;
- }
- }
- }
- }
- };
C98支持:
- class Solution {
- public:
- vector<vector<bool>> vis;
- int m, n;
- int numIslands(vector<vector<char>>& grid) {
- m = grid.size(), n = grid[0].size();
- int ret = 0;
- vis.resize(m, vector<bool>(n, false));
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- if (grid[i][j] == '1' && !vis[i][j]) {
- ret++;
- bfs(grid, i, j);
- }
- }
- }
- return ret;
- }
- int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};
- void bfs(vector<vector<char>>& grid, int i, int j) {
- queue<pair<int, int>> q;
- q.push(make_pair(i, j));
- vis[i][j] = true;
- while (!q.empty()) {
- auto a = q.front();
- q.pop();
- for (int k = 0; k < 4; k++) {
- int x = a.first + dx[k], y = a.second + dy[k];
- if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' &&
- !vis[x][y]) {
- q.push(make_pair(x, y));
- vis[x][y] = true;
- }
- }
- }
- }
- };
思路:遍历数组每个元素,在每个元素的BFS中统计岛屿面积,BFS结束返回当前岛屿的面积,再与上次的岛屿面积相比取最大值。
- class Solution {
- public:
- vector<vector<bool>> vis;
- int m, n;
- int maxAreaOfIsland(vector<vector<int>>& grid) {
- m = grid.size(), n = grid[0].size();
- int ret = 0;
- vis.resize(m, vector<bool>(n, false));
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- if (grid[i][j] == 1 && !vis[i][j]) {
- ret = max(ret, bfs(grid, i, j));
- }
- }
- }
- return ret;
- }
- int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};
- int bfs(vector<vector<int>>& grid, int i, int j) {
- int count = 0;
- queue<pair<int, int>> q;
- q.push(make_pair(i, j));
- vis[i][j] = true;
- count++;
- while (!q.empty()) {
- auto [a, b] = q.front();
- q.pop();
- for (int k = 0; k < 4; k++) {
- int x = a + dx[k], y = b + dy[k];
- if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 &&
- !vis[x][y]) {
- count++;
- q.push({x, y});
- vis[x][y] = true;
- }
- }
- }
- return count;
- }
- };
思路:与边界处的“O”相邻的不能算被围绕的区域,所以先BFS位于边界的“O”,将它们暂时换成一个其他字符比如“.”,处理完边界的“O”,遍历数组如果为“O”则改成“X”,为“.”改成O,这样既保护住了边界的“O”,有修改了被包围的“O”为“X”。
- class Solution {
- public:
- int m, n;
- void solve(vector<vector<char>>& board) {
- m = board.size(), n = board[0].size();
- for (int i = 0; i < m; i++) {
- if (board[i][0] == 'O')
- bfs(board, i, 0);
- if (board[i][n - 1] == 'O')
- bfs(board, i, n - 1);
- }
- for (int j = 0; j < n; j++) {
- if (board[0][j] == 'O')
- bfs(board, 0, j);
- if (board[m - 1][j] == 'O')
- bfs(board, m - 1, j);
- }
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- if (board[i][j] == '.')
- board[i][j] = 'O';
- else if (board[i][j] == 'O')
- board[i][j] = 'X';
- }
- }
- }
- int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
- void bfs(vector<vector<char>>& board, int i, int j) {
- queue<pair<int, int>> q;
- q.push({i, j});
- board[i][j] = '.';
- while (!q.empty()) {
- auto [a, b] = q.front();
- q.pop();
- for (int k = 0; k < 4; k++) {
- int x = a + dx[k], y = b + dy[k];
- if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') {
- q.push({x, y});
- board[x][y] = '.';
- }
- }
- }
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。