【C++刷题】优选算法——递归第三辑

floodfill篇

  1. 图像渲染
cpp 复制代码
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0}
};
void dfs(vector<vector<int>>& image, int sr, int sc, int color, int val)
{
    image[sr][sc] = color;
    for(auto& e : direction)
    {
        int x = sr + e.first, y = sc + e.second;
        if((x >= 0 && x < image.size())
        && (y >= 0 && y < image[0].size())
        && (image[x][y] == val))
        {
            dfs(image, x, y, color, val);
        }
    }
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color)
{
    if(color == image[sr][sc]) return image;
    dfs(image, sr, sc, color, image[sr][sc]);
    return image;
}
  1. 岛屿数量
cpp 复制代码
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0}
};
void dfs(vector<vector<char>>& grid, vector<vector<bool>>& visit, int i, int j)
{
    visit[i][j] = true;
    for(auto& e : direction)
    {
        int x = i + e.first, y = j + e.second;
        if((x >= 0 && x < grid.size())
        && (y >= 0 && y < grid[0].size())
        && (grid[x][y] == '1')
        && (visit[x][y] == false))
        {
            dfs(grid, visit, x, y);
        }
    }
}
int numIslands(vector<vector<char>>& grid)
{
    vector<vector<bool>> visit(grid.size(), vector<bool>(grid[0].size()));
    int ret = 0;
    for(int i = 0; i < grid.size(); ++i)
    {
        for(int j = 0; j < grid[0].size(); ++j)
        {
            if(grid[i][j] == '1' && visit[i][j] == false)
            {
                ret += 1;
                dfs(grid, visit, i, j);
            }
        }
    }
    return ret;
}
  1. 岛屿的最大面积
cpp 复制代码
int area = 0;
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0}
};
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visit, int i, int j)
{
    ++area;
    visit[i][j] = true;
    for(auto& e : direction)
    {
        int x = i + e.first, y = j + e.second;
        if((x >= 0 && x < grid.size())
        && (y >= 0 && y < grid[0].size())
        && (grid[x][y] == 1)
        && (visit[x][y] == false))
        {
            dfs(grid, visit, x, y);
        }
    }
}
int maxAreaOfIsland(vector<vector<int>>& grid)
{
    vector<vector<bool>> visit(grid.size(), vector<bool>(grid[0].size()));
    int ret = 0;
    for(int i = 0; i < grid.size(); ++i)
    {
        for(int j = 0; j < grid[0].size(); ++j)
        {
            if(grid[i][j] == 1 && visit[i][j] == false)
            {
                dfs(grid, visit, i, j);
                if(area > ret) ret = area;
                area = 0;
            }
        }
    }
    return ret;
}
  1. 被围绕的区域
cpp 复制代码
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0}
};
void dfs(vector<vector<char>>& board, vector<vector<bool>>& visit, int i, int j)
{
    visit[i][j] = true;

    for(auto& e : direction)
    {
        int x = i + e.first, y = j + e.second;
        if((x >= 0 && x < board.size())
        && (y >= 0 && y < board[0].size())
        && (board[x][y] == 'O')
        && (visit[x][y] == false))
        {
            dfs(board, visit, x, y);
        }
    }
}
void solve(vector<vector<char>>& board)
{
    vector<vector<bool>> visit(board.size(), vector<bool>(board[0].size(), false));
    // 第一行 i=0
    for(int j = 0; j < board[0].size(); ++j)
    {
        if(board[0][j] == 'O' && visit[0][j] == false)
        {
            dfs(board, visit, 0, j);
        }
    }

    // 最后一行 i=board.size()-1
    for(int j = 0; j < board[0].size(); ++j)
    {
        if(board[board.size()-1][j] == 'O' && visit[board.size()-1][j] == false)
        {
            dfs(board, visit, board.size() - 1, j);
        }
    }

    // 第一列 j=0
    for(int i = 1; i < board.size() - 1; ++i)
    {
        if(board[i][0] == 'O' && visit[i][0] == false)
        {
            dfs(board, visit, i, 0);
        }
    }

    // 最后一列 j=board[0].size() - 1
    for(int i = 1; i < board.size() - 1; ++i)
    {
        if(board[i][board[0].size() - 1] == 'O' && visit[i][board[0].size() - 1] == false)
        {
            dfs(board, visit, i, board[0].size() - 1);
        }
    }

    for(int i = 0; i < board.size(); ++i)
    {
        for(int j = 0; j < board[0].size(); ++j)
        {
            if(board[i][j] == 'O' && visit[i][j] == false)
            {
                board[i][j] = 'X';
            }
        }
    }
}
  1. 太平洋大西洋水流问题
cpp 复制代码
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0}
};
int m, n;
void dfs(vector<vector<int>>& heights, vector<vector<bool>>& visit, int i , int j)
{
    visit[i][j] = true;
    for(auto& e : direction)
    {
        int x = i + e.first, y = j + e.second;
        if((x >= 0 && x < m)
        && (y >= 0 && y < n)
        && (heights[i][j] <= heights[x][y])
        && (visit[x][y] == false))
        {
            dfs(heights, visit, x, y);
        }
    }
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights)
{
    m = heights.size();
    n = heights[0].size();
    vector<vector<bool>> visit1(m, vector<bool>(n, false));
    vector<vector<bool>> visit2(m, vector<bool>(n, false));
    
    // 第一行 i=0
    for(int j = 0; j < n; ++j)
    {
        if(visit1[0][j] == false)
        {
            dfs(heights, visit1, 0, j);
        }
    }
    // 第一列 j=0
    for(int i = 0; i < m; ++i)
    {
        if(visit1[i][0] == false)
        {
            dfs(heights, visit1, i, 0);
        }
    }

    // 最后一行 i=m-1
    for(int j = 0; j < n; ++j)
    {
        if(visit2[m-1][j] == false)
        {
            dfs(heights, visit2, m-1, j);
        }
    }
    // 最后一列 j=n-1
    for(int i = 0; i < m; ++i)
    {
        if(visit2[i][n-1] == false)
        {
            dfs(heights, visit2, i, n-1);
        }
    }

    vector<vector<int>> ret;
    for(int i = 0; i < m; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            if(visit1[i][j] && visit2[i][j])
            {
                ret.push_back({i, j});
            }
        }
    }

    return ret;
}
  1. 扫雷游戏
cpp 复制代码
unordered_multimap<int, int> direction = {
    {-1, -1},
    {-1, 0},
    {-1, 1},
    {0, -1},
    {0, 1},
    {1, -1},
    {1, 0},
    {1, 1}
};
void dfs(vector<vector<char>>& board, int i, int j)
{
    char ch = '0';
    for(auto& e : direction)
    {
        int x = i + e.first, y = j + e.second;
        if((x >= 0 && x < board.size())
        && (y >= 0 && y < board[0].size())
        && (board[x][y] == 'M'))
            ++ch;
    }
    if(ch > '0') 
    {
        board[i][j] = ch;
        return;
    }
    
    board[i][j] = 'B';
    for(auto& e : direction)
    {
        int x = i + e.first, y = j + e.second;
        if((x >= 0 && x < board.size())
        && (y >= 0 && y < board[0].size())
        && (board[x][y] == 'E'))
            dfs(board, x, y);
    }
}
vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
{
    int r = click[0], c = click[1];
    if(board[r][c] != 'M' && board[r][c] != 'E') return board;

    if(board[r][c] == 'M')
    {
        board[r][c] = 'X';
        return board;
    }

    // board[r][c]是未挖出的的空方块 - 'E'
    dfs(board, r, c);
    return board;
}
  1. 衣橱整理
cpp 复制代码
int ret = 0;
unordered_multimap<int, int> direction = {
    {0, 1},
    {0, -1},
    {1, 0},
    {1, -1}
};
int digit(int x)
{
    int sum = 0;
    while(x)
    {
        sum += (x % 10);
        x /= 10;
    }
    return sum;
}
void dfs(vector<vector<bool>>& visit, int i, int j, int cnt)
{
    ++ret;
    visit[i][j] = true;
    for(auto& e : direction)
    {
        int x = i + e.first, y = j + e.second;
        if((x >= 0 && x < visit.size())
        && (y >= 0 && y < visit[0].size())
        && (visit[x][y] == false)
        && ((digit(x) + digit(y) <= cnt)))
        {
            dfs(visit, x, y, cnt);
        }
    }
}
int wardrobeFinishing(int m, int n, int cnt)
{
    vector<vector<bool>> visit(m, vector<bool>(n, false));
    dfs(visit, 0, 0, cnt);
    return ret;
}
相关推荐
不想当程序猿_4 小时前
【蓝桥杯每日一题】分糖果——DFS
c++·算法·蓝桥杯·深度优先
XiaoLeisj1 天前
【递归,搜索与回溯算法 & 综合练习】深入理解暴搜决策树:递归,搜索与回溯算法综合小专题(二)
数据结构·算法·leetcode·决策树·深度优先·剪枝
single5942 天前
【c++笔试强训】(第四十一篇)
java·c++·算法·深度优先·图论·牛客
清弦墨客2 天前
【数据结构与算法】深度优先搜索:树与图的路径探寻之道
数据结构·python·算法·蓝桥杯·深度优先
_nut_2 天前
图论基础算法/DFS+BFS+Trie树
算法·深度优先·图论
mit6.8244 天前
[数据结构#2] 图(1) | 概念 | 邻接矩阵 | 邻接表 | 模拟
算法·深度优先·图论
牧歌悠悠5 天前
【深度学习】 零基础介绍卷积神经网络(CNN)
人工智能·深度学习·cnn·深度优先
菜鸟起航ing8 天前
数据结构---图(Graph)
java·数据结构·算法·深度优先
想喝冰拿铁8 天前
图论笔记1
笔记·深度优先·图论
XiaoLeisj8 天前
【递归,搜索与回溯算法 & 递归算法】递归算法入门详解:递归算法小专题
java·算法·leetcode·深度优先·推荐算法