3
int dfs(vector<vector<int>> &grid, int x, int y, int zero) {5
if (x < 0 || y < 0 || x >= grid.size() || y >= grid[0].size() || grid[x][y] == -1) {9
return zero == -1 ? 1 : 0; // Why zero = -1, because in above example we10
// have 9 zero's. So, when we reach the final11
// cell we are covering one cell extra that12
// means if all zero all covered then on13
// reaching final it will make zero count = -114
// If that's the case we find the path and return '1' otherwise return17
grid[x][y] = -1; // mark the visited cells as -1;18
zero--; // and reduce the zero by 120
int totalPaths = dfs(grid, x + 1, y,21
zero) + // calculating all the paths available in 4 directions22
dfs(grid, x - 1, y, zero) +23
dfs(grid, x, y + 1, zero) + dfs(grid, x, y - 1, zero);25
// Let's say if we are not able to count all the paths. Now we use26
// Backtracking over here30
return totalPaths; // if we get all the paths, simply return it.33
int uniquePathsIII(vector<vector<int>> &grid) {34
int start_x, start_y = 0, cntzero = 0;36
for (int i = 0; i < grid.size(); i++) {37
for (int j = 0; j < grid[0].size(); j++) {40
else if (grid[i][j] == 1) {46
return dfs(grid, start_x, start_y, cntzero);