Given a grid **grid[][]** with **4** types of blocks:

**1**represents the starting block. There is exactly one starting block.**2**represents the ending block. There is exactly one ending block.**0**represents empty block we can walk over.**-1**represents obstacles that we cannot walk over.

The task is to count the number of paths from the starting block to the ending block such that every non-obstacle block is covered exactly once.

**Examples:**

Input:grid[][] = {

{1, 0, 0, 0},

{0, 0, 0, 0},

{0, 0, 2, -1} }

Output:2

Following are the only paths covering all the non-obstacle blocks:

Input:grid[][] = {

{1, 0, 0, 0},

{0, 0, 0, 0},

{0, 0, 0, 2} }

Output:4

**Approach:** We can use simple DFS here with backtracking. We can check that a particular path has covered all the non-obstacle blocks by counting all the blocks encountered in the way and finally comparing it with the total number of blocks available and if they match, then we add it as a valid solution.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for dfs. ` `// i, j ==> Current cell indexes ` `// vis ==> To mark visited cells ` `// ans ==> Result ` `// z ==> Current count 0s visited ` `// z_count ==> Total 0s present ` `void` `dfs(` `int` `i, ` `int` `j, vector<vector<` `int` `> >& grid, ` ` ` `vector<vector<` `bool` `> >& vis, ` `int` `& ans, ` ` ` `int` `z, ` `int` `z_count) ` `{ ` ` ` `int` `n = grid.size(), m = grid[0].size(); ` ` ` ` ` `// Mark the block as visited ` ` ` `vis[i][j] = 1; ` ` ` `if` `(grid[i][j] == 0) ` ` ` ` ` `// update the count ` ` ` `z++; ` ` ` ` ` `// If end block reached ` ` ` `if` `(grid[i][j] == 2) { ` ` ` ` ` `// If path covered all the non- ` ` ` `// obstacle blocks ` ` ` `if` `(z == z_count) ` ` ` `ans++; ` ` ` `vis[i][j] = 0; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Up ` ` ` `if` `(i >= 1 && !vis[i - 1][j] && grid[i - 1][j] != -1) ` ` ` `dfs(i - 1, j, grid, vis, ans, z, z_count); ` ` ` ` ` `// Down ` ` ` `if` `(i < n - 1 && !vis[i + 1][j] && grid[i + 1][j] != -1) ` ` ` `dfs(i + 1, j, grid, vis, ans, z, z_count); ` ` ` ` ` `// Left ` ` ` `if` `(j >= 1 && !vis[i][j - 1] && grid[i][j - 1] != -1) ` ` ` `dfs(i, j - 1, grid, vis, ans, z, z_count); ` ` ` ` ` `// Right ` ` ` `if` `(j < m - 1 && !vis[i][j + 1] && grid[i][j + 1] != -1) ` ` ` `dfs(i, j + 1, grid, vis, ans, z, z_count); ` ` ` ` ` `// Unmark the block (unvisited) ` ` ` `vis[i][j] = 0; ` `} ` ` ` `// Function to return the count of the unique paths ` `int` `uniquePaths(vector<vector<` `int` `> >& grid) ` `{ ` ` ` `int` `z_count = 0; ` `// Total 0s present ` ` ` `int` `n = grid.size(), m = grid[0].size(); ` ` ` `int` `ans = 0; ` ` ` `vector<vector<` `bool` `> > vis(n, vector<` `bool` `>(m, 0)); ` ` ` `int` `x, y; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `for` `(` `int` `j = 0; j < m; ++j) { ` ` ` ` ` `// Count non-obstacle blocks ` ` ` `if` `(grid[i][j] == 0) ` ` ` `z_count++; ` ` ` `else` `if` `(grid[i][j] == 1) { ` ` ` ` ` `// Starting position ` ` ` `x = i, y = j; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `dfs(x, y, grid, vis, ans, 0, z_count); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<vector<` `int` `> > grid{ { 1, 0, 0, 0 }, ` ` ` `{ 0, 0, 0, 0 }, ` ` ` `{ 0, 0, 2, -1 } }; ` ` ` ` ` `cout << uniquePaths(grid); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Unique paths in a Grid with Obstacles
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Number of unique paths in tree such that every path has a value greater than K
- Find number of segments covering each point in an given array
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Number of paths with exactly k coins
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Count unique paths is a matrix whose product of elements contains odd number of divisors
- Find an N x N grid whose xor of every row and column is equal
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Color all boxes in line such that every M consecutive boxes are unique
- Maximize the binary matrix by filpping submatrix once
- Maximum points collected by two persons allowed to meet once
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Direction at last square block
- Collect maximum points in a grid using two traversals
- Search a Word in a 2D Grid of characters
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.