Unique Paths III in C++


Suppose we have one 2-dimensional grid, there are 4 types of squares −

  • In a square 1 is for the starting point. There will be exactly one starting square.

  • In a square 2 is for the ending point. There will be exactly one ending square.

  • In a square 0 is for the empty squares and we can walk over.

  • In a square -1 if for the obstacles that we cannot walk over.

We have to find the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.

So, if the input is like −

1000
0000
102-1

then the output will be 2, as we have these two paths: (0,0), (0,1), (0,2), (0,3), (1,3), (1,2), (1,1),(1,0), (2,0), (2,1), (2,2) and (0,0), (1,0), (2,0), (2,1), (1,1), (0,1), (0,2), (0,3), (1,3), (1,2), (2,2).

To solve this, we will follow these steps −

  • Define a function dfs(), this will take one 2D array grid, i, j, ex, ey, empty,

  • if i,j not in range of grid or grid[i, j] is same as -1, then −

    • return 0

  • if grid[i, j] is same as 2, then

    • return true when empty is -1

  • x := 0

  • (decrease empty by 1)

  • grid[i, j] := -1

  • for initialize k := 0, when k < 4, update (increase k by 1), do −

    • nx := i + dir[k, 0]

    • ny := j + dir[k, 1]

    • x := x + dfs(grid, nx, ny, ex, ey, empty)

  • (increase empty by 1)

  • grid[i, j] := 0

  • return x

  • From the method do the following −

  • empty := 0

  • n := Row count, m := Column count

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • for initialize j := 0, when j < m, update (increase j by 1), do −

      • if grid[i, j] is same as 0, then

        • (increase empty by 1)

      • otherwise when grid[i, j] is same as 1, then −

        • sx := i, sy := j

      • otherwise when grid[i, j] is same as 2, then −

        • ex := i, ey := j

  • return dfs(grid, sx, sy, ex, ey, empty)

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
class Solution {
   public:
   int dfs(vector<vector<int> >& grid, int i, int j, int ex, int ey,
   int empty){
      if (i >= grid.size() || i < 0 || j >= grid[0].size() || j < 0
      || grid[i][j] == -1)
      return 0;
      if (grid[i][j] == 2) {
         return empty == -1;
      }
      int x = 0;
      empty--;
      grid[i][j] = -1;
      for (int k = 0; k < 4; k++) {
         int nx = i + dir[k][0];
         int ny = j + dir[k][1];
         x += dfs(grid, nx, ny, ex, ey, empty);
      }
      empty++;
      grid[i][j] = 0;
      return x;
   }
   int uniquePathsIII(vector<vector<int> >& grid){
      int empty = 0;
      int sx, sy, ex, ey;
      int n = grid.size();
      int m = grid[0].size();
      for (int i = 0; i < n; i++) {
         for (int j = 0; j < m; j++) {
            if (grid[i][j] == 0)
            empty++;
            else if (grid[i][j] == 1) {
               sx = i;
               sy = j;
            }
            else if (grid[i][j] == 2) {
               ex = i;
               ey = j;
            }
         }
      }
      return dfs(grid, sx, sy, ex, ey, empty);
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{1,0,0,0},{0,0,0,0},{0,0,2,-1}};
   cout << (ob.uniquePathsIII(v));
}

Input

{{1,0,0,0},{0,0,0,0},{0,0,2,-1}}

Output

2

Updated on: 08-Jun-2020

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements