Count number of ways to reach destination in a Maze in C++

C++Server Side ProgrammingProgramming

Given a Maze represented as a row X col matrix in which the obstacle is represented as -1 and a clear cell has value other than -1. The goal is to start from the first cell arr[0][0] and reach the last cell arr[row][col] such that only two moves are allowed:

  • Right move arr[i][j] to arr[i][j+1] and 
  • Down move arr[i][j] to arr[i+1][j].

Let us understand with examples.

Input -  arr[row][col] =  {{0, 0, 0}, {-1, -1, 0}, {0, 0, 0}}

Output - Count of number of ways to reach destination in a Maze are: 1

Explanation

      0    1   2   

0    0    0   0

1   -1   -1   0

2    0    0   0

The ways will be :

  • cell (0,0) → cell (0,1) → cell (0,2) → cell(1,2) → cell(2,0)

Input -  arr[row][col] =  { {0, 0, 0, 0}, {-1, 0, -1, 0}, {-1, 0, -1, 0}, {0, 0, 0, 0}}

Output - Count of number of ways to reach destination in a Maze are: 2

Explanation

      0    1   2   3 

0    0    0   0   0

1   -1    0  -1   0

2   -1    0  -1   0

3    0    0   0   0

The ways will be :

  • cell (0,0) → cell (0,1) → cell (1,1) → cell(2,1) → cell(3,1) → cell(3,2) → cell(3,3)
  • cell (0,0) → cell (0,1) → cell (0,2) → cell(0,3) → cell(1,3) → cell(2,3) → cell(3,3) 

Approach used in the below program is as follows

In this approach we will first set all zeros to 1. Traverse the maze matrix again and now for each cell, if it is blockage (-1) then ignore it. If not, then check the upper cell( i-1,j) and left cell (i,j-1). If it is more than zero then add its value to the current cell (i,j). In this way we will get sum at cell (row-1,col-1) as total ways to reach it.

  • Take the input array arr[row][col] as Maze.
  • Function destination_maze(int arr[row][col]) takes arr and returns the count of number of ways to reach destination in a Maze.
  • If the first cell is blocked then return 0 as ways to reach the end.
  • Now traverse the leftmost column and set all 0s to 1. At first the blockage breaks the loop as cells below it cannot be reached. Start from index i=0 to i<row and set arr[i][0] to 1 if it is 0.
  • Similarly do for the first row from j=0 to j<col and set arr[j][0] to 1 if it is 0.
  • Traverse the arr again from cell (1,1) and if arr[i][j] is -1 then do nothing.
  • If arr[i-1][j] or arr[i][j-1] is more than 0 then arr[i][j] is reachable from them. So add their value to it.
  • At the end we will have arr[row-1][col-1] as total ways to reach it.
  • If it is >0 then return it else return 0.

Example

Live Demo

#include<bits/stdc++.h>

using namespace std;
#define row 3
#define col 3

int destination_maze(int arr[row][col]) {
   if (arr[0][0] == -1) {
      return 0;
   }
   for (int i = 0; i < row; i++) {
      if (arr[i][0] == 0) {
         arr[i][0] = 1;
      } else {
         break;
      }
   }
   for (int i = 1; i < col; i++) {
      if (arr[0][i] == 0) {
         arr[0][i] = 1;
      } else {
         break;
      }
   }
   for (int i = 1; i < row; i++) {
      for (int j = 1; j < col; j++) {
         if (arr[i][j] == -1) {
            continue;
         }
         if (arr[i - 1][j] > 0) {
            arr[i][j] = (arr[i][j] + arr[i - 1][j]);
         }
         if (arr[i][j - 1] > 0) {
            arr[i][j] = (arr[i][j] + arr[i][j - 1]);
         }
      }
   }
   if (arr[row - 1][col - 1] > 0) {
      return arr[row - 1][col - 1];
   } else {
      return 0;
   }
}
int main() {
   int arr[row][col] = {
      {
         0,
         0,
         0
      },
      {
         -1,
         -1,
         0
      },
      {
         0,
         0,
         0
      }
   };
   cout << "Count of number of ways to reach destination in a Maze are: " << destination_maze(arr);
   return 0;
}

If we run the above code it will generate the following output −

Output

Count of number of ways to reach destination in a Maze are: 1
raja
Published on 29-Jan-2021 07:47:22
Advertisements