一、每日一题
今天每日一题是个多源BFS题目,本质上是从当前这个点像水波一样向外扩展。每次把源头加入队列,然后向上下左右四个方向高度加一,每次记录高度就行了。
class Solution {
public:
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
queue<pair<int, int>> q;
int m = isWater.size();
int n = isWater[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (isWater[i][j] == 1) {
isWater[i][j] = 0;
q.push(make_pair(i, j));
} else {
isWater[i][j] = m*n+1;
}
}
}
int height = 0;
while (!q.empty()) {
int size = q.size();
height++;
while (size--) {
pair<int, int> p = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int i = p.first + dx[d];
int j = p.second + dy[d];
if (i < 0 || j < 0 || i >= m || j >= n) continue;
if (isWater[i][j] > height) {
isWater[i][j] = height;
q.push(make_pair(i, j));
}
}
}
}
return isWater;
}
};
二、DFS
练习有一点感觉了,我逐渐理解一切。
大概题目就是树类和网格类,这里记录一下大概的思路。
首先树类,大概递归代码:
void dfs(TreeNode *root) {
// base case
if (root == nullptr) {
return;
}
// 访问左子树右子树
dfs(root->left);
dfs(root->right);
}
要点两个,一是base case,在什么情况下不再进行递归,一般是!root。二是访问节点,树形结构的话很单纯,就是左节点和右节点。
相对的网格型题目,base case包括了索引越界和不需要继续遍历两种情况,按照题目去写。其次访问节点即是(i-1,j),(i+1,j),(i,j-1),(i,j+1)四个。
void dfs(vector<vector<int>>& grid, int i, int j) {
// 判断 base case
// 如果坐标超出了网格范围,直接返回,check()表示的是是否需要继续搜索
if (!inArea(grid, r, c) || !check()) {
return;
}
// 访问上、下、左、右四个相邻结点
dfs(grid, i - 1, j);
dfs(grid, i + 1, j);
dfs(grid, i, j - 1);
dfs(grid, i, j + 1);
}
// 判断坐标 (r, c) 是否在网格中
bool inArea(int[][] grid, int i, int j)
{
return 0 <= r && r < grid.size()
&& 0 <= c && c < grid[0].size();
}
网格型的题目里面,岛屿系列题目非常经典,记一下今天写的其中几个题目。
class Solution {
public:
int n,m;
void dfs(vector<vector<char>>& grid,int i,int j)
{
if(i<0||i>=n||j<0||j>=m) return;
grid[i][j] ='0';
if(i-1>=0&& grid[i-1][j] == '1')dfs(grid,i-1,j);
if(i+1<n&& grid[i+1][j] == '1') dfs(grid,i+1,j);
if(j-1>=0&& grid[i][j-1] == '1') dfs(grid,i,j-1);
if(j+1<m&& grid[i][j+1] == '1') dfs(grid,i,j+1);
}
int numIslands(vector<vector<char>>& grid) {
n=grid.size(),m=grid[0].size();
int num =0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(grid[i][j]=='1')
{
num++;
dfs(grid,i,j);
}
}
}
return num;
}
};
class Solution {
public:
int n,m;
bool isvalid(int i,int j)
{
return i>=0&&i<n&&j>=0&&j<m;
}
int dfs(vector<vector<int>>& grid,int i,int j)
{
if(!isvalid(i,j)||grid[i][j]==0) return 1;
if(grid[i][j] == 2) return 0;
grid[i][j] =2;
return dfs(grid,i-1,j)+dfs(grid,i+1,j)+dfs(grid,i,j-1)+dfs(grid,i,j+1);
}
int islandPerimeter(vector<vector<int>>& grid) {
n = grid.size(),m = grid[0].size();
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(grid[i][j] ==1)
{
ans = dfs(grid,i,j);
}
}
}
return ans;
}
};
class Solution {
public:
int n,m;
bool isvalid(int i,int j)
{
return i>=0&&i<n&&j>=0&&j<m;
}
void dfs(vector<vector<char>>& board,int i,int j)
{
if(!isvalid(i,j)) return;
if(board[i][j]=='O')
{
board[i][j] = 'A';
dfs(board,i-1,j);
dfs(board,i+1,j);
dfs(board,i,j+1);
dfs(board,i,j-1);
}
else return;
}
void solve(vector<vector<char>>& board) {
n=board.size(),m=board[0].size();
for(int i=0;i<n;i++)
{
dfs(board,i,0);
dfs(board,i,m-1);
}
for(int j=0;j<m;j++)
{
dfs(board,0,j);
dfs(board,n-1,j);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(board[i][j]=='O') board[i][j]='X';
if(board[i][j]=='A') board[i][j]='O';
}
}
}
};