- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
Suppose, we are given a grid of dimensions h * w that contains two types of cells, blocked and unblocked. Blocked cells mean that the cells aren't accessible and unblocked means that the cells are accessible. We represent the grid in a 2D array where the blocked cells are given as '#' and the unblocked cells are given as '.'. Now, we have to reach from an unblocked cell to another unblocked cell in the grid. We can perform only two moves, we can either go vertical or we can go horizontal. We can't move diagonally. We have to keep in mind that, we can only move to unblocked cells. So, we have to find out the maximum number of moves required to reach an unblocked cell from another unblocked cell in the grid.
So, if the input is like h = 4, w = 4, grid = {"..#.", "#.#.", "..##", "###."}, then the output will be 4.
From cell (0,0), a maximum of 4 moves are required to reach the cell (2, 0).
To solve this, we will follow these steps −
Define an array xdir of size: 4 := {1, 0, - 1, 0} Define an array ydir of size: 4 := {0, 1, 0, - 1} Define one 2D array dist Define one 2D array reset res := 0 for initialize i := 0, when i < h, update (increase i by 1), do: for initialize j := 0, when j < w, update (increase j by 1), do: dist := reset if grid[i, j] is same as '.', then: dist[i, j] := 0 Define one queue q containing integer pairs insert make_pair(i, j) into q while (not q is empty), do: x := first element of the leftmost element in the q y := second element of the leftmost element in the q res := maximum of (dist[x, y] and res) delete leftmost element from q for initialize k := 0, when k < 4, update (increase k by 1), do: px := x + xdir[k] py := y + ydir[k] if px >= 0 and px < h and py >= 0 and py < w, then: if grid[px, py] is same as '.', then: if dist[px, py] is same as -1, then: dist[px, py] := dist[x, y] + 1 insert pair(px, py) into q return res
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int h, int w, vector<string> grid){ int xdir[4] = {1, 0, -1, 0}; int ydir[4] = {0, 1, 0, -1}; vector<vector<int>> dist(h, vector<int>(w, -1)); vector<vector<int>> reset(h, vector<int>(w, -1)); int res = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ dist = reset; if(grid[i][j] == '.'){ dist[i][j] = 0; queue<pair<int,int>> q; q.push(make_pair(i, j)); while(!q.empty()){ int x = q.front().first; int y = q.front().second; res = max(dist[x][y], res); q.pop(); for(int k = 0; k < 4; k++){ int px = x + xdir[k]; int py = y + ydir[k]; if(px >= 0 && px < h && py >= 0 && py < w){ if(grid[px][py] == '.'){ if(dist[px][py] == -1){ dist[px][py] = dist[x][y] + 1; q.push(make_pair(px, py)); } } } } } } } } return res; } int main() { int h = 4, w = 4; vector<string> grid = {"..#.", "#.#.", "..##", "###."}; cout << solve(h, w, grid); return 0; }
Input
4, 4, {"..#.", "#.#.", "..##", "###."}
Output
4