
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find out number of changes required to get from one end to other end in a grid
Suppose, we are given a grid of dimensions x * y 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 the cell (0, 0) to cell (x, y). We can perform only two moves, we can either go right of a cell or go down from a cell. We have to keep in mind that, we can only go in unblocked cells, and (0, 0) and (x, y) are both unblocked cells. If we cannot reach (x, y) from (0, 0), we can make a blocked cell an unblocked cell. We have to find the minimum number of changing operations to perform to reach our destination from source.
So, if the input is like x = 4, y = 4, grid = {"..#.", "#.#.", "#.##", "###."}, then the output will be 1.
Only one change operation has to be performed. If we change the cell (2, 2) to unblocked from blocked, then we can reach (3, 3) from (0, 0).
Steps
To solve this, we will follow these steps −
Define one 2D array mat if grid[0, 0] is same as '#', then: mat[0, 0] := 1 Otherwise mat[0, 0] := 0 for initialize i := 0, when i < x, update (increase i by 1), do: for initialize j := 0, when j < y, update (increase j by 1), do: if i + 1 < x, then: mat[i + 1, j] = minimum of (mat[i + 1, j], mat[i, j] + (1 if grid[i + 1, j] is same as '#' AND grid[i, j] is same as '.')) if j + 1 < y, then: mat[i, j + 1] = minimum of (mat[i, j + 1], mat[i, j]+(1 if grid[i, j + 1] is same as '#' AND grid[i, j] is same as '.')) return mat[x - 1, y - 1]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int x, int y, vector<string> grid){ vector<vector<int>> mat(x, vector<int>(y, 100)); if(grid[0][0] == '#') mat[0][0] = 1; else mat[0][0] = 0; for(int i = 0; i < x; i++){ for(int j = 0; j < y; j++){ if(i + 1 < x){ mat[i + 1][j] = min(mat[i + 1][j], mat[i][j] + (grid[i + 1][j] == '#' && grid[i][j] == '.')); } if(j + 1 < y){ mat[i][j + 1] = min(mat[i][j + 1],mat[i][j]+(grid[i][j + 1] == '#' && grid[i][j] == '.')); } } } return mat[x - 1][y - 1]; } int main() { int x = 4, y = 4; vector<string> grid = {"..#.", "#.#.", "#.##", "###."}; cout<< solve(x, y, grid); return 0; }
Input
4, 4, {"..#.", "#.#.", "#.##", "###."}
Output
1
- Related Articles
- Program to find minimum number of hops required to reach end position in Python
- What is the mode of transfer of heat from one end of a metallic spoon to the other end ?
- C++ program to find minimum number of steps needed to move from start to end
- Find the other end point of a line with given one end and mid using C++
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to Find Out the Number of Squares in a Grid 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
- Find minimum steps required to reach the end of a matrix in C++
- C++ Program to find out the total cost required for a robot to make a trip in a grid
- Set where to end the CSS grid item
- How to find the minimum number of jumps required to reach the end of the array using C#?
- In a synapse, chemical signal is transmitted from:(a) axon to cell body of the same neuron(b) cell body to axon end of the same neuron(c) dendrite end of one neuron to axon end of adjacent neuron(d) axon end of one neuron to dendrite end of adjacent neuron
- C++ Program to find out the number of cells to block in a grid to create a path
- Program to count number of paths with cost k from start to end point in Python
