递归寻路,为了避免走重复的路,建立标记数组进行标记
class Solution {
List<List<Integer>> l = new ArrayList<>();
int[][] j;
public List<List<Integer>> pathWithObstacles(int[][] obstacleGrid) {
j = new int[obstacleGrid.length][obstacleGrid[0].length];
find(obstacleGrid, 0, 0);
return l;
}
boolean find(int[][] obstacleGrid, int r, int c){
if(r == obstacleGrid.length || c == obstacleGrid[0].length || obstacleGrid[r][c] == 1 || j[r][c] == 1)
return false;
j[r][c] = 1;
List<Integer> t = new ArrayList<>();
t.add(r);
t.add(c);
l.add(t);
if(r == obstacleGrid.length - 1 && c == obstacleGrid[0].length - 1)
return true;
if(find(obstacleGrid, r + 1 , c) || find(obstacleGrid, r, c + 1)){
return true;
}else{
l.remove(l.size() - 1);
return false;
}
}
}