C++ program to find minimum number of punches are needed to make way to reach target


Suppose we have a matrix containing H rows and W columns. The cells either holds '.' or '#'. The dot '.' indicates passable space, and '#' indicates block. Amal will go from his house to a market. His house is in the cell at the top-left corner, and the market is at the bottom-right corner. Amal can move one cell up, down, left, or right to a passable cell. He cannot leave the town. He cannot enter a blocked cell, either. However, his physical strength allows him to destroy all blocks in a square region with 2×2 cells of his choice with one punch, making these cells passable. We have to find the minimum number of punches needed for Amal to reach the market.

So, if the input is like

..#..
#.#.#
##.##
#.#.#
..#..

then the output will be 1, because we can make the grid like −

..#..
#...#
##..#
#.#.#
..#..

by destroying the marked boxes

Steps

To solve this, we will follow these steps −

n := row count of matrix
m := column count of matrix
Define one 2D array dist of order (n + 1) x (m + 1)
Define one deque dq
insert ( 0, 0 ) at the beginning of dq
dist[0, 0] := 0
while (not dq is empty), do:
   v := first element of dq
   delete front element from dq
   for initialize i := 0, when i < 4, update (increase i by 1), do:
      x := dx[i] + v[0]
      y := dy[i] + v[1]
      if x >= 0 and x < n and y >= 0 and y < m, then:
         if matrix[x, y] is same as '.', then:
            if dist[x, y] > dist[v[0], v[1]], then:
               dist[x, y] := dist[v[0], v[1]]
               insert pair { x, y } at the beginning of dq
         Otherwise
            for initialize p := x - 1, when p <= x + 1, update (increase p by 1), do:
               for initialize q := y - 1, when q <= y + 1, update (increase q by 1), do:
                  if p >= 0 and p < n and q >= 0 and q < m, then:
                     if dist[p, q] > dist[v[0], v[1]] + 1, then:
                        dist[p, q] := dist[v[0], v[1]] + 1
                        insert pair { p, q } at the end of dq
return dist[n - 1, m - 1]

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

int dx[4] = { 0, 0, -1, 1 };
int dy[4] = { -1, 1, 0, 0 };

int solve(vector<vector<char>> matrix){
   int n = matrix.size();
   int m = matrix[0].size();
   vector<vector<int>> dist(n + 1, vector<int>(m + 1, 1e9));
   deque<array<int, 2>> dq;
   dq.push_front({ 0, 0 });
   dist[0][0] = 0;
   while (!dq.empty()){
      auto v = dq.front();
      dq.pop_front();
      for (int i = 0; i < 4; i++){
         int x = dx[i] + v[0], y = dy[i] + v[1];
         if (x >= 0 && x < n && y >= 0 && y < m){
            if (matrix[x][y] == '.'){
               if (dist[x][y] > dist[v[0]][v[1]]){
                  dist[x][y] = dist[v[0]][v[1]];
                  dq.push_front({ x, y });
               }
            } else{
               for (int p = x - 1; p <= x + 1; p++){
                  for (int q = y - 1; q <= y + 1; q++){
                     if (p >= 0 && p < n && q >= 0 && q < m){
                        if (dist[p][q] > dist[v[0]][v[1]] + 1){
                           dist[p][q] = dist[v[0]][v[1]] + 1;
                           dq.push_back({ p, q });
                        }
                     }
                  }
               }
            }
         }
      }
   }
   return dist[n - 1][m - 1];
}
int main(){
   vector<vector<char>> matrix = { { '.', '.', '#', '.', '.' }, { '#', '.', '#', '.', '#' }, { '#', '#', '.', '#', '#' }, { '#', '.', '#', '.', '#' }, { '.', '.', '#', '.', '.' } };
   cout << solve(matrix) << endl;
}

Input

{ { '.', '.', '#', '.', '.' }, { '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '#' }, { '#', '.', '#', '.', '#' }, {
'.', '.', '#', '.', '.' } }

Output

1

Updated on: 03-Mar-2022

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements