- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- C++ program to find out the maximum number of cells a cleaning robot can clean in a grid
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- C++ Program to find out the number of illuminated cells in a grid
- C++ Program to find out the number of border cells in a grid
- C++ Program to find out the number of cells to block in a grid to create a path
- C++ Program to find out the moves to read a point from another point in a 2D plane
- Program to Find Out the Number of Squares in a Grid in Python
- C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid
- C++ Program to find out the number of sides that a polygon has inside a grid
- C++ program to find out the number of ways a grid with boards can be colored
- C++ Program to check k rupees are enough to reach final cell or not
- C++ program to find winner of cell coloring game
