图像渲染
广度优先搜索入门题目,框架基本都是这样的
class Solution {
public:
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 newColor) {
if(image[sr][sc] == newColor){
return image;
}
int m = image.size();
int n = image[0].size();
int color = image[sr][sc];
image[sr][sc] = newColor;
queue<pair<int,int>>q;
q.emplace(sr,sc);
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int mx = x + dx[i];
int my = y + dy[i];
if(mx >= 0 && mx < m && my >= 0 && my < n && image[mx][my] == color){
image[mx][my] = newColor;
q.emplace(mx,my);
}
}
}
return image;
}
};
建队列保存每一个暂时可行的可行解
单独取出队首的可行解,再深入一点检测是否仍然满足
就是这个套路
广度搜索是一个淘汰赛,大家先投简历,筛掉一部分,再笔试,筛掉一部分,再面试,再筛一部分,最后的就是可行解,有的还要找出最优解,再加一轮Hr面
岛屿周长
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
const int dx[4] = {0,0,-1,1};
const int dy[4] = {1,-1,0,0};
int m = grid.size();
int n = grid[0].size();
queue<pair<int,int>>q;
int count = 0;
int oneNum = 0;
vector<vector<int>>visited(m,vector<int>(n,0));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(visited[i][j] == 0 && grid[i][j] == 1){
q.emplace(i,j);
visited[i][j] = 1;
oneNum++;
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int mx = x + dx[i];
int my = y + dy[i];
if(mx >= 0 && mx < m && my >= 0 && my < n && grid[mx][my] == 1){
if(visited[mx][my] == 0){
oneNum++;
visited[mx][my] = 1;
q.emplace(mx,my);
}
count++;
}
}
}
}
}
}
return oneNum*4 - count;
}
};
被围绕区域
按常规广搜思路找满足的,因为不确定右边和左边的是否是满足的,遍历比较困难:
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 'O' && visited[i][j] == 0){
q.emplace(0,0);
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int k = 0; k < 4; k++){
int mx = x + dx[k];
int my = y + dy[k];
if(board[mx][my] == 'X' )
}
}
}
}
}
逆向思维,我们现在要求找到所有被 ‘X’ 围绕的区域,而他的反面是没有被围绕的区域,没有被围绕必须直接或间接和边上的O相连,所以问题转化为找到所有与边上相连的O,然后将所有没有与边相连的O变为X.
先遍历四个边,找到O就入队,通过这个初始队列找到所有间接/直接与边上O相连的并记录,最后把所有么有被记录的改为X
class Solution {
public:
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
void solve(vector<vector<char>>& board) {
int n = board.size();
if (n == 0) {
return;
}
int m = board[0].size();
queue<pair<int, int>> que;
for (int i = 0; i < n; i++) {
if (board[i][0] == 'O') {
que.emplace(i, 0);
board[i][0] = 'A';
}
if (board[i][m - 1] == 'O') {
que.emplace(i, m - 1);
board[i][m - 1] = 'A';
}
}
for (int i = 1; i < m - 1; i++) {
if (board[0][i] == 'O') {
que.emplace(0, i);
board[0][i] = 'A';
}
if (board[n - 1][i] == 'O') {
que.emplace(n - 1, i);
board[n - 1][i] = 'A';
}
}
while (!que.empty()) {
int x = que.front().first, y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int mx = x + dx[i], my = y + dy[i];
if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != 'O') {
continue;
}
que.emplace(mx, my);
board[mx][my] = 'A';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] == 'A') {
board[i][j] = 'O';
} else if (board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/surrounded-regions/solution/bei-wei-rao-de-qu-yu-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
广搜误我:迷宫离入口最近的出口
和昨天的题很相似,从每一个出口出发,统计每一个出口到入口的路径,求最短即可
这里做了太长时间,主要是距离的保存太麻烦,直接打算用二元数组来保存,导致后续太复杂,维护起来不方便
主要最后卡在入口在边上,而边上又有连着的出口,会导致连增改变队列中出口的初始值。。。
class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};
int m = maze.size();
int n = maze[0].size();
int startx = entrance[0];
int starty = entrance[1];
vector<vector<int>>visited(m,vector<int>(n,0));
visited[startx][starty] = -1;
queue<pair<int,int>>q;
for(int i = 0; i < m; i++){
if(maze[i][0] == '.' && visited[i][0] != -1){
q.emplace(i,0);
}
if(maze[i][n-1] == '.' && visited[i][n-1] != -1){
q.emplace(i,n-1);
}
}
for(int i = 1; i < n-1; i++){
if(maze[0][i] == '.' && visited[0][i] != -1){
q.emplace(0,i);
}
if(maze[m-1][i] == '.' && visited[m-1][i] != -1){
q.emplace(m-1,i);
}
}
int res = INT_MAX;
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i <4; i++){
int mx = x + dx[i];
int my = y + dy[i];
if(mx < 0 || mx >= m || my < 0 || my >= n || maze[mx][my] != '.'){
continue;
}
if(mx == 0 || mx == m || my == 0 || my == n){
if(visited[mx][my] == -1)
{
res = min(res,visited[x][y]+1);
}
}
else{
if(visited[mx][my] == -1)
{
res = min(res,visited[x][y]+1);
}else if(visited[mx][my] == 0){
visited[mx][my] = visited[x][y]+1;
q.emplace(mx,my);
}else{
visited[mx][my] = min(visited[mx][my],visited[x][y]+1);
}
}
}
}
return res == INT_MAX ? -1 : res;
}
};
官方题解是从入口开始,因为是广度优先遍历,找到第一个就是最优解
注意三元组tuple的使用
class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int m = maze.size();
int n = maze[0].size();
// 上下左右四个相邻坐标对应的行列变化量
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
queue<tuple<int, int, int>> q;
// 入口加入队列并修改为墙
q.emplace(entrance[0], entrance[1], 0);
maze[entrance[0]][entrance[1]] = '+';
while (!q.empty()){
auto [cx, cy, d] = q.front();
q.pop();
// 遍历四个方向相邻坐标
for (int k = 0; k < 4; ++k){
int nx = cx + dx[k];
int ny = cy + dy[k];
// 新坐标合法且不为墙
if (nx >= 0 && nx < m && ny >= 0 && ny < n && maze[nx][ny] == '.'){
if (nx == 0 || nx == m - 1 || ny == 0 || ny == n - 1){
// 新坐标为出口,返回距离作为答案
return d + 1;
}
// 新坐标为空格子且不为出口,修改为墙并加入队列
maze[nx][ny] = '+';
q.emplace(nx, ny, d + 1);
}
}
}
// 不存在到出口的路径,返回 -1
return -1;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/nearest-exit-from-entrance-in-maze/solution/mi-gong-zhong-chi-ru-kou-zui-jin-de-chu-0ued5/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。