dfs要用到回溯
797 dfs经典模版题 跟回溯一样 很好写 x这里是目前达到的节点位置 for loop里面是遍历当前位置可去的节点位置 path要先加入0(所有的路径都是从0开始的)
class Solution {
private:
vector> result;
vector path;
void dfs(vector>& graph, int x) {
if (x==graph.size()-1) {
result.push_back(path);
return;
}
for (int i=0; i> allPathsSourceTarget(vector>& graph) {
path.push_back(0);
dfs(graph, 0);
return result;
}
};
bfs 适合用于解决找两点之间的最短路径
200 bfs function 从某个点开始 将它连续的所有为1的都标记上
main function 从头到尾遍历 遇到为1但没被标记过的 说明遇到新岛屿 count++ 走一遍bfs function
class Solution {
private:
void bfs(vector>& grid, vector>& visited, int x, int y) {
int dir[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}};
queue> que;
que.push({x,y});
visited[x][y]=true;
while (!que.empty()) {
pair curr = que.front(); que.pop();
for (int i=0; i<4; i++) {
int next_x = curr.first+dir[i][0];
int next_y = curr.second+dir[i][1];
if (next_x<0 || next_x>=grid.size() || next_y<0 || next_y>=grid[0].size()) continue;
if (!visited[next_x][next_y] && grid[next_x][next_y]=='1') {
visited[next_x][next_y]=true;
que.push({next_x, next_y});
}
}
}
}
public:
int numIslands(vector>& grid) {
int count=0;
vector> visited(grid.size(), vector(grid[0].size(), false));
for (int i=0; i
这道岛屿问题用dfs也很好写 不用终于条件 因为调用时是有条件的 main function的逻辑和bfs一样
void dfs(vector>& grid, vector>& visited, int x, int y) {
visited[x][y]=true;
for (int i=0; i<4; i++) {
int next_x = x+dir[i][0];
int next_y = y+dir[i][1];
if (next_x<0 || next_x>=grid.size() || next_y<0 || next_y>=grid[0].size()) continue;
if (!visited[next_x][next_y] && grid[next_x][next_y]=='1') {
dfs(grid, visited, next_x, next_y);
}
}
}
695 岛屿问题都是一样的 一个岛一个岛的去深搜/广搜且标记计数 找到最大的岛
class Solution {
private:
int dir[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
int dfs(vector>& grid, vector>& visited, int x, int y) {
if (x<0 || x>=grid.size() || y<0 || y>=grid[0].size()) return 0;
if (grid[x][y]==0||visited[x][y]) return 0;
visited[x][y] = true;
int count=1;
for (int i=0; i<4; i++) {
int next_x = x+dir[i][0];
int next_y = y+dir[i][1];
count+=dfs(grid, visited, next_x, next_y);
}
return count;
}
int bfs(vector>& grid, vector>& visited, int x, int y) {
queue> que;
que.push({x,y});
visited[x][y]=true;
int count=1;
while (!que.empty()) {
int x=que.front().first;
int y=que.front().second;
que.pop();
for (int i=0; i<4; i++) {
int next_x = x+dir[i][0];
int next_y = y+dir[i][1];
if (next_x<0 || next_x>=grid.size() || next_y<0 || next_y>=grid[0].size()) {
continue;
}
if (!visited[next_x][next_y] && grid[next_x][next_y]==1) {
que.push({next_x, next_y});
visited[next_x][next_y]=true;
count++;
}
}
}
return count;
}
public:
int maxAreaOfIsland(vector>& grid) {
vector> visited(grid.size(), vector(grid[0].size(), false));
int result = 0;
for (int i=0; i
1020 先遍历四个边的岛屿 都标记之后 再数没标记的1有几个
class Solution {
private:
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void dfs (vector>& grid, vector>& visited, int x, int y) {
if (x<0 || x>=grid.size() || y<0 || y>=grid[0].size()) return;
if (visited[x][y] || grid[x][y]==0) return;
visited[x][y]=true;
int count = 1;
for (int i=0; i<4; i++) {
int next_x=x+dir[i][0];
int next_y=y+dir[i][1];
dfs(grid, visited, next_x, next_y);
}
}
void bfs (vector>& grid, vector>& visited, int x, int y) {
queue> que;
que.push({x,y});
visited[x][y]=true;
while (!que.empty()) {
int x= que.front().first;
int y= que.front().second;
que.pop();
for (int i=0; i<4; i++) {
int next_x=x+dir[i][0];
int next_y=y+dir[i][1];
if (next_x<0 || next_x>=grid.size() || next_y<0 || next_y>=grid[0].size()) continue;
if (visited[next_x][next_y] || grid[next_x][next_y]==0) continue;
que.push({next_x, next_y});
visited[next_x][next_y]=true;
}
}
}
public:
int numEnclaves(vector>& grid) {
int m=grid.size();
int n=grid[0].size();
vector> visited(grid.size(), vector(grid[0].size(), false));
for (int i=0; i
130 跟上一题一样 把四边的都标记完之后改中间没标记的变成X
class Solution {
private:
int dir[4][2]={{0,1}, {0,-1}, {1,0}, {-1,0}};
void dfs(vector>& board, vector>& visited, int x, int y) {
if (x<0 || x>=board.size() || y<0 || y>=board[0].size()) return;
if (visited[x][y] || board[x][y]=='X') return;
visited[x][y]=true;
for (int i=0; i<4; i++) {
int nx=x+dir[i][0];
int ny=y+dir[i][1];
dfs(board, visited, nx, ny);
}
}
public:
void solve(vector>& board) {
vector> visited(board.size(), vector(board[0].size(), false));
int m=board.size();
int n=board[0].size();
for (int i=0; i