class Solution {
public:
vector>res;
vectormid={0};
void backtrace(vector>&graph,int starti,int n){
if(mid.back()==n-1){//遍历到一条路径时
res.push_back(mid);
return;
}
if(starti==n) return;//超出二维下标
for(int i=0;i> allPathsSourceTarget(vector>& graph) {
int n=graph.size();
backtrace(graph,0,n);
return res;
}
};
class Solution {
public:
int direct[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
void dfs(vector>&grid,vector>&visted,int x,int y){
for(int i=0;i<4;i++){//从四个方向进行深度优先搜索
int nextx = x+direct[i][0];
int nexty = y+direct[i][1];
if(nextx=0 && nexty=0){//当未越界时
if(grid[nextx][nexty]=='1' && !visted[nextx][nexty]){//当前陆地没有被遍历过时
visted[nextx][nexty]=true;
dfs(grid,visted,nextx,nexty);
}
}
}
}
int numIslands(vector>& grid) {
int n=grid.size(),m=grid[0].size();
int res=0;
vector>visted(n,vector(m,false));
for(int i=0;i
class Solution {
public:
int direct[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
void bfs(vector>&grid,vector>&visted,int x,int y){
queue>que;
que.push(make_pair(x,y));
visted[x][y]=true;//加入栈就是已经遍历
while(!que.empty()){
pair cur=que.front();//获取栈顶组合
que.pop();
int curx=cur.first;
int cury=cur.second;
for(int i=0;i<4;i++){//从四个方向的第一层进行遍历
int nextx=curx+direct[i][0];
int nexty=cury+direct[i][1];
//防止越界
if(nextx>=0 && nextx=0 && nexty>& grid) {
int n=grid.size(),m=grid[0].size();
int res=0;
vector>visted(n,vector(m,false));
for(int i=0;i
class Solution {
public:
int direct[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int bfs(vector>&grid,vector>&visted,int x,int y){
queue>que;
que.push(make_pair(x,y));
visted[x][y]=true;
int mid=1;//初始只有一个岛屿
while(!que.empty()){
paircur=que.front();
que.pop();
int curx=cur.first;
int cury=cur.second;
for(int i=0;i<4;i++){
int nextx=curx+direct[i][0];
int nexty=cury+direct[i][1];
if(nextx>=0 && nextx=0 && nexty>& grid) {
int n=grid.size(),m=grid[0].size();
vector>visted(n,vector(m,false));
int res=0;
for(int i=0;ires) res=mid;//更新最大面积
}
}
}
return res;
}
};
class Solution {
public:
int direct[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int res=0;
int mid=1;
void dfs(vector>&grid,vector>&visted,int x,int y){
for(int i=0;i<4;i++){
int nextx=x+direct[i][0];
int nexty=y+direct[i][1];
//越界控制
if(nextx>=0 && nextx=0 && nexty>& grid) {
int n=grid.size(),m=grid[0].size();
vector>visted(n,vector(m,false));
for(int i=0;ires) res=mid;
mid=1;//记录面积值重新赋值
}
}
}
return res;
}
};