1、BFS解决FloodFill算法
1、1图像渲染
733. 图像渲染 - 力扣(LeetCode)
class Solution
{typedef pair<int,int> PII;int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};
public:vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {int n = image.size(), m = image[0].size();int aim = image[sr][sc];//标记像素值if(aim == color) return image;//处理边界情况queue<PII> q;q.push({sr,sc});//入第一个位置while(!q.empty()){auto [a,b] = q.front();//拿到对头q.pop();//然后删除,front和pop是绑定在一起的。image[a][b] = color;//修改像素值for(int i = 0;i<4;i++){int x = a + dx[i], y = b + dy[i];if(x >= 0 && x < n && y >= 0 && y < m && image[x][y] == aim)//符合条件{q.push({x,y});}}}return image;}
};
1、2岛屿数量
200. 岛屿数量 - 力扣(LeetCode)
class Solution
{int dx[4] = {0,0,1,-1};//向量表示int dy[4] = {1,-1,0,0};bool vis[301][301];//标记int n,m;int res;typedef pair<int,int> PII;
public:void bfs(int i,int j,vector<vector<char>>& grid){queue<PII> q;q.push({i,j});vis[i][j] = true;while(!q.empty()){auto [a,b] = q.front();q.pop();//vis[a][b] = true;//不能在出队的时候才改变状态,否则前面的结点可能也会遍历到for循环里面已经找过的结点,结果会重复计算//必须遍历到是陆地的结点时,立马修改状态;否则就会引起重复计算for(int k = 0;k<4;k++){int x = a + dx[k], y = b + dy[k];if(x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && grid[x][y] == '1'){vis[x][y] = true;q.push({x,y});}}}}int numIslands(vector<vector<char>>& grid) {n = grid.size(),m = grid[0].size();for(int i = 0;i<n;i++)for(int j = 0;j<m;j++){if(grid[i][j] == '1' && !vis[i][j]){res++;bfs(i,j,grid);//进去将相连的陆地标记}}return res;}
};
1、3岛屿的最大面积
695. 岛屿的最大面积 - 力扣(LeetCode)
class Solution
{int res = 0;//最大岛屿面积int path;//当前陆地面积bool vis[51][51];//记录访问过的陆地int m,n;int dx[4] = {0,0,1,-1};//向量int dy[4] = {1,-1,0,0};typedef pair<int,int> PII;
public:void bfs(vector<vector<int>>& grid,int i,int j){queue<PII> q;q.push({i,j});int count = 0;vis[i][j] = true;while(!q.empty()){auto [a,b] = q.front();q.pop();count++;//vis[a][b] = true;//不能在出队的时候才改变状态,否则前面的结点可能也会遍历到for循环里面已经找过的结点,结果会重复计算//必须遍历到是陆地的结点时,立马修改状态;否则就会引起重复计算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]){vis[x][y] = true;q.push({x,y});}}}res = max(res,count);}int maxAreaOfIsland(vector<vector<int>>& grid) {m = grid.size(),n = grid[0].size();for(int i = 0;i<m;i++)for(int j = 0;j<n;j++){if(grid[i][j] == 1 && !vis[i][j]){bfs(grid,i,j);}}return res;}
};
1、4被围绕的区域
130. 被围绕的区域 - 力扣(LeetCode)
class Solution
{ vector<vector<bool>> vis;int m,n;int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};typedef pair<int,int> PII;
public:void bfs(vector<vector<char>>& board,int i,int j){queue<PII> 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 && board[x][y] == 'O' && !vis[x][y]){vis[x][y] = true;q.push({x,y});}}}}void solve(vector<vector<char>>& board) {m = board.size(), n =board[0].size();vis = vector<vector<bool>>(m,vector<bool>(n,false));//将在边上未被包围的岛屿的vis变为true;for(int j = 0;j<n;j++){if(!vis[0][j] && board[0][j] == 'O'){bfs(board,0,j);}if(!vis[m-1][j] && board[m-1][j] == 'O'){bfs(board,m-1,j);} }for(int i = 0;i<m;i++){if(!vis[i][0] && board[i][0] == 'O'){bfs(board,i,0);}if(!vis[i][n-1] && board[i][n-1] == 'O'){bfs(board,i,n-1);} }//再将矩阵内部的被包围并且没有被访问过的岛屿改为xfor(int i = 1;i<m-1;i++)for(int j = 1;j<n-1;j++){if(!vis[i][j] && board[i][j] == 'O'){board[i][j] = 'X';}}}};
2、BFS解决最短路径问题
边权为1的最短路问题
2、1迷宫中离入口最近的出口
1926. 迷宫中离入口最近的出口 - 力扣(LeetCode)
class Solution
{int m,n;bool vis[101][101];int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};typedef pair<int,int> PII;
public:int nearestExit(vector<vector<char>>& maze, vector<int>& e) {m = maze.size(), n = maze[0].size();queue<PII> q;q.push({e[0],e[1]});vis[e[0]][e[1]] = true;int count = 0;//步数while(!q.empty()){count++;//扩展一层int size = q.size();//当前队列中有多少元素,然后“同时出队”,同时bfsfor(int p = 0;p<size;p++){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 &&