主要是dir的应用,以及一层循环代表每一种路径往外走了一截,最先抵达了一种路径就不用算另外的了。
class Solution {
public:
vector<vector<int>>dir={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
if(grid[0][0]==1)return -1;
int n=grid.size();
queue<pair<int,int>>neighbor;
int length=1;
neighbor.push(make_pair(0,0));
grid[0][0]=2;
while(!neighbor.empty()){
int l=neighbor.size();
for(int i=0;i<l;i++){
auto x=neighbor.front().first;
auto y=neighbor.front().second;
neighbor.pop();
if(x==n-1&&y==n-1)return length;
for(int j=0;j<8;j++){
int x1=x+dir[j][0];
int y1=y+dir[j][1];
if(x1<0||y1<0||x1>n-1||y1>n-1||grid[x1][y1])continue;
grid[x1][y1]=2;//标记已经走过的地方,防止超时。。
neighbor.push(make_pair(x1,y1));
}
}
length++;
}
return -1;
}
};
运用新添的flag数组,从外围用dfs突破,标记上不被化为‘X’的数组,最后统统化为‘X’
这就是企业级理解!
class Solution {
private:
void dfs(vector<vector<char>>& board,vector<vector<int>>& flag,int i,int j){
int m=board.size(),n=board[0].size();
flag[i][j]=1;
if(i>0&&board[i-1][j]=='O'&&flag[i-1][j]==0)dfs(board,flag,i-1,j);
if(i<(m-1)&&board[i+1][j]=='O'&&flag[i+1][j]==0)dfs(board,flag,i+1,j);
if(j>0&&board[i][j-1]=='O'&&flag[i][j-1]==0)dfs(board,flag,i,j-1);
if(j<(n-1)&&board[i][j+1]=='O'&&flag[i][j+1]==0)dfs(board,flag,i,j+1);
}
public:
void solve(vector<vector<char>>& board) {
int m=board.size(),n=board[0].size();
vector<vector<int>>flag(m,vector<int>(n,0));
for(int i=0;i<m;i++){
if(board[i][0]=='O'&&flag[i][0]==0)dfs(board,flag,i,0);
if(board[i][n-1]=='O'&&flag[i][n-1]==0)dfs(board,flag,i,n-1);
}
for(int j=1;j<n-1;j++){
if(board[0][j]=='O'&&flag[0][j]==0)dfs(board,flag,0,j);
if(board[m-1][j]=='O'&&flag[m-1][j]==0)dfs(board,flag,m-1,j);
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(flag[i][j]==0)board[i][j]='X';
}
}
}
};
关键是回溯这一步,以及for(int i:graph[j])这样的遍历很方便
新加了两个数组来存放数组,形成双推进。
class Solution {
private:
// i表示当前的点
// n表示点的数量,也是结束标志
// res是结果
// curr是当前的路径
void dfs(int i, int n, vector<vector<int>>& res, vector<int>& curr, vector<vector<int>>& graph)
{
if (i < n-1)
{
for (int next : graph[i])
{
curr.push_back(next);
dfs(next, n, res, curr, graph);
// 记得回溯
curr.pop_back();
}
}
else
{
// 递归结束了
res.push_back(curr);
}
}
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> res;
// 初始从0开始
vector<int> curr{0};
int n = graph.size();
dfs(0, n, res, curr, graph);
return res;
}
};