题目链接
代码随想录文章讲解链接
用时:11m43s
class Solution {
private:
vector<vector<int>> res;
vector<int> path;
void backTracking(vector<vector<int>>& graph, int i) {
if (i == graph.size() - 1) {
res.push_back(path);
return;
}
for (int j = 0; j < graph[i].size(); ++j) {
path.push_back(graph[i][j]);
backTracking(graph, graph[i][j]);
path.pop_back(); // 回溯
}
}
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
path.push_back(0);
backTracking(graph, 0);
return res;
}
};
用时:16m18s
队列中记录的元素是路径。
class Solution {
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
queue<vector<int>> que;
vector<vector<int>> res;
que.push({0});
while (!que.empty()) {
vector<int> path = que.front();
que.pop();
for (int i = 0; i < graph[path.back()].size(); ++i) {
vector<int> tmp = path;
tmp.push_back(graph[path.back()][i]);
if (tmp.back() == graph.size() - 1) res.push_back(tmp);
else que.push(tmp);
}
}
return res;
}
};
无。
无。
题目链接
代码随想录文章讲解链接
用时:17m48s
遍历每个元素,若是陆地则使用DFS搜索与之相邻的所有陆地,并使用一个二维数组记录哪些陆地已经遍历过。
当遍历到新的岛屿时,岛屿数加1。
class Solution {
private:
int m;
int n;
int dir[4][2] = {0, -1, 1, 0, 0, 1, -1, 0};
void dfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y) {
visited[x][y] = true;
for (int i = 0; i < 4; ++i) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (!(nextx < 0 || nextx >= m || nexty < 0 || nexty >= n) && !visited[nextx][nexty] && grid[nextx][nexty] == '1') dfs(grid, visited, nextx, nexty);
}
}
public:
int numIslands(vector<vector<char>>& grid) {
m = grid.size();
n = grid[0].size();
vector<vector<bool>> visited(m, vector<bool>(n, false));
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (!visited[i][j] && grid[i][j] == '1') {
++res;
dfs(grid, visited, i, j);
}
}
}
return res;
}
};
可以不用数组记录哪些陆地访问过,只用把访问过的陆地置0即可。
class Solution {
private:
int m;
int n;
int dir[4][2] = {0, -1, 1, 0, 0, 1, -1, 0};
void dfs(vector<vector<char>>& grid, int x, int y) {
grid[x][y] = '0';
for (int i = 0; i < 4; ++i) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (!(nextx < 0 || nextx >= m || nexty < 0 || nexty >= n) && grid[nextx][nexty] == '1') dfs(grid, nextx, nexty);
}
}
public:
int numIslands(vector<vector<char>>& grid) {
m = grid.size();
n = grid[0].size();
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
++res;
dfs(grid, i, j);
}
}
}
return res;
}
};
class Solution {
private:
int m;
int n;
int dir[4][2] = {0, -1, 1, 0, 0, 1, -1, 0};
void bfs(vector<vector<char>>& grid, int x, int y) {
queue<pair<int, int>> que;
grid[x][y] = '0';
que.push(pair<int, int>(x, y));
while (!que.empty()) {
pair<int, int> tmp = que.front();
que.pop();
for (int i = 0; i < 4; ++i) {
int nextx = tmp.first + dir[i][0];
int nexty = tmp.second + dir[i][1];
if (!(nextx < 0 || nextx >= m || nexty < 0 || nexty >= n) && grid[nextx][nexty] == '1') {
grid[nextx][nexty] = '0';
que.push(pair<int, int>(nextx, nexty));
}
}
}
}
public:
int numIslands(vector<vector<char>>& grid) {
m = grid.size();
n = grid[0].size();
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
++res;
bfs(grid, i, j);
}
}
}
return res;
}
};
无。
无。
题目链接
代码随想录文章讲解链接
用时:16m39s
当遇到陆地时,DFS该岛屿,记录该岛屿的面积,并将遍历过的陆地置零,最后返回最大的岛屿的面积。
class Solution {
private:
int m;
int n;
int area;
void dfs(vector<vector<int>>& grid, int x, int y) {
grid[x][y] = 0;
area += 1;
if (x - 1 >= 0 && grid[x - 1][y] == 1) dfs(grid, x - 1, y);
if (x + 1 < m && grid[x + 1][y] == 1) dfs(grid, x + 1, y);
if (y - 1 >= 0 && grid[x][y - 1] == 1) dfs(grid, x, y - 1);
if (y + 1 < n && grid[x][y + 1] == 1) dfs(grid, x, y + 1);
}
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int res = 0;
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) {
area = 0;
dfs(grid, i, j);
res = max(res, area);
}
}
}
return res;
}
};
class Solution {
private:
int m;
int n;
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int bfs(vector<vector<int>>& grid, int x, int y) {
int area = 1;
queue<pair<int, int>> que;
que.push(pair<int, int>(x, y));
grid[x][y] = 0;
while (!que.empty()) {
pair<int, int> tmp = que.front();
que.pop();
for (int i = 0; i < 4; ++i) {
int nextx = tmp.first + dir[i][0];
int nexty = tmp.second + dir[i][1];
if (!(nextx < 0 || nextx >= m || nexty < 0 || nexty >= n) && grid[nextx][nexty] == 1) {
grid[nextx][nexty] = 0;
area += 1;
que.push(pair<int, int>(nextx, nexty));
}
}
}
return area;
}
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int res = 0;
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) res = max(res, bfs(grid, i, j));
}
}
return res;
}
};
无。
BFS时,元素入列的时候就要做标记,不能在元素出列的时候才做标记,不然会重复遍历。
题目链接
代码随想录文章讲解链接
用时:20m42s
在上一题695m的基础上,加多一个变量用于记录当前岛屿是否临界,如果不是的话将岛屿的陆地数量记录。
class Solution {
private:
int m;
int n;
int num;
bool flag;
void dfs(vector<vector<int>>& grid, int x, int y) {
grid[x][y] = 0;
num += 1;
if (x - 1 < 0) flag = true;
else if (grid[x - 1][y] == 1) dfs(grid, x - 1, y);
if (x + 1 >= m) flag = true;
else if (grid[x + 1][y] == 1) dfs(grid, x + 1, y);
if (y - 1 < 0) flag = true;
else if (grid[x][y - 1] == 1) dfs(grid, x, y - 1);
if (y + 1 >= n) flag = true;
else if (grid[x][y + 1] == 1) dfs(grid, x, y + 1);
}
public:
int numEnclaves(vector<vector<int>>& grid) {
int res = 0;
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) {
num = 0;
flag = false;
dfs(grid, i, j);
if (!flag) res += num;
}
}
}
return res;
}
};
class Solution {
private:
int m;
int n;
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int bfs(vector<vector<int>>& grid, int x, int y) {
int num = 1;
queue<pair<int, int>> que;
que.push(pair<int, int>(x, y));
grid[x][y] = 0;
while (!que.empty()) {
pair<int, int> tmp = que.front();
que.pop();
for (int i = 0; i < 4; ++i) {
int nextx = tmp.first + dir[i][0];
int nexty = tmp.second + dir[i][1];
if (nextx < 0 || nextx >= m || nexty < 0 || nexty >= n) num = INT_MIN;
else if (grid[nextx][nexty] == 1) {
grid[nextx][nexty] = 0;
++num;
que.push(pair<int, int>(nextx, nexty));
}
}
}
return num;
}
public:
int numEnclaves(vector<vector<int>>& grid) {
int res = 0;
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) res += max(0, bfs(grid, i, j));
}
}
return res;
}
};
无。
无。
题目链接
代码随想录文章讲解链接
用时:17m13s
遍历位于临界的元素,若是’O’则将连通的’O’变成’H’,最后再将board中的所有’O’替换成’X’,‘H’替换成’O’,因为此时剩下的’O’是位于board内部的无法连接到外部的元素,直接将其变成’X’,而’H’是可以连通到外部的’O’,将其变回’O’。
class Solution {
private:
int m;
int n;
void dfs(vector<vector<char>>& board, int x, int y) {
board[x][y] = 'H';
if (x - 1 >= 0 && board[x - 1][y] == 'O') dfs(board, x - 1, y);
if (x + 1 < m && board[x + 1][y] == 'O') dfs(board, x + 1, y);
if (y - 1 >= 0 && board[x][y - 1] == 'O') dfs(board, x, y - 1);
if (y + 1 < n && board[x][y + 1] == 'O') dfs(board, x, y + 1);
}
public:
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') dfs(board, i, 0);
if (board[i][n - 1] == 'O') dfs(board, i, n - 1);
}
for (int i = 0; i < n; ++i) {
if (board[0][i] == 'O') dfs(board, 0, i);
if (board[m - 1][i] == 'O') dfs(board, m - 1, i);
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'H') board[i][j] = 'O';
else if (board[i][j] == 'O') board[i][j] = 'X';
}
}
}
};
就是遍历的方法从DFS换成BFS,懒得写了。
无。
无。
就快要一刷完代码随想录,冲!