# leetcode 79单词搜索与小道面积 leetcode695 岛屿的最大面积 总结

79 单词搜索

### 代码

class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
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)
ret = max(ret,dfs(grid,i,j));
}
return ret;
}
int dfs(vector<vector<int>>& grid,int i,int j)
{
//边界条件
if(i<0||j<0||i>=grid.size()||j>=grid[0].size())
return 0;
//海水剪枝
if(grid[i][j]==0)
return 0;
grid[i][j]=0; //表示已访问
int all=1; //表示访问一次
all += dfs(grid,i+1,j);
all += dfs(grid,i-1,j);
all += dfs(grid,i,j+1);
all += dfs(grid,i,j-1);
return all;
}
};

class Solution {
public:
bool dfs(vector<vector<char>>& board,vector<vector<int>>& visit,const string& word,int size,int x,int y)
{
if(size==word.size())
{
return true;
}
//边界条件
if(x<0||y<0||x>=board.size()||y>=board[0].size()||board[x][y]!=word[size])
return false;
if(visit[x][y]==0)
{
visit[x][y] = 1;
//其中一个搜索成功返回true
if(dfs(board,visit,word,size+1,x+1,y)||
dfs(board,visit,word,size+1,x-1,y)||
dfs(board,visit,word,size+1,x,y+1)||
dfs(board,visit,word,size+1,x,y-1))
return true;
visit[x][y] = 0;
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
if (board.empty() || word.empty()){
return false;
}
//visit初始化
vector<vector<int>> visit(board.size(), vector<int>(board[0].size(), 0));
for(int i=0;i<board.size();i++)
for(int j=0;j<board[0].size();j++)
{
if(dfs(board,visit,word,0,i,j))
return true;
}
return false;

}
};

### 重难点分析

vector<vector<int>> visi(row,vector<int>(col,0));

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客