Shortest Path in Binary Matrix in C++


Suppose we have an N by N square grid, there each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that −

  • Adjacent cells C_i and C_{i+1} are connected 8-directionally (So they are different and share an edge or corner)

  • C_1 is at location (0, 0)

  • C_k is at location (N-1, N-1)

  • If C_i is located at (r, c), then grid[r, c] is empty or contains 0

We have to find the length of the shortest such clear path from top-left to bottom-right. If there is no such path, then return -1.

For example, if the grid is like −

000
110
110

The orange cells will be the path. The length is 4

To solve this, we will follow these steps −

  • Define a direction array, this will hold 8 pairs to move 8 different directions. So this array is like [[1,1], [1,-1], [-1,1], [1,0], [0,1], [-1,-1], [0,-1], [-1,0]]

  • The main section will take the grid as input, this will act like below −

  • define a queue of points, q, n:= number of rows

  • if grid[0, 0] is 0, then make a new point p(0, 0, 1), insert p into q, and make grid[0, 0] := 1

  • while q is not empty

    • curr := front point from q, delete front point from q

    • x := x value of curr, y := y value of curr, c := c value of curr

    • if x = n – 1 and y = n – 1, then return c

    • increase c by 1

    • for i in range 0 to 7

      • X := x + d[i, 0], Y := y + d[i, 1]

      • if X in range 0 and n and y in range 0 and n, and grid[X, Y] is 0, then

        • grid[X, Y] := 1

        • insert a new point p (X, Y, c) into q

  • return -1

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int d[8][2] = {{1, 1}, {1, -1}, {-1, 1}, {1, 0}, {0, 1}, {-1, -1},
{0, -1}, {-1, 0}};
struct point{
   int x, y, c;
   point(int a, int b, int z){
      x = a;
      y = b;
      c = z;
   }
};
class Solution {
   public:
   int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
      queue <point> q;
      int n = grid.size();
      if(!grid[0][0]){
         q.push(point(0, 0, 1));
         grid[0][0] = 1;
      }
      while(!q.empty()){
         point curr = q.front();
         q.pop();
         int x = curr.x;
         int y = curr.y;
         int c = curr.c;
         if(x == n-1 && y == n-1)return c;
            c++;
         for(int i = 0; i < 8; i++){
            int X = x + d[i][0];
            int Y = y + d[i][1];
            if(X >= 0 && X < n && Y >= 0 && Y < n &&
            !grid[X][Y]){
               grid[X][Y] = 1;
               q.push(point(X, Y, c));
            }
         }
      }
      return -1;
   }
};
main(){
   vector<vector<int>> v = {{0,0,0},{1,1,0},{1,1,0}};
   Solution ob;
   cout << (ob.shortestPathBinaryMatrix(v));
}

Input

[[0,0,0],[1,1,0],[1,1,0]]

Output

4

Updated on: 02-May-2020

619 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements