
- 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 how many pawns can reach the first row
Suppose we have two binary strings S and T of size n. Consider there is a chessboard of size n by n.
Currently, there are some pawns in the n-th row. There are also enemy pawns in the 1-st row. On one move, we can move one of our pawns. A pawn can move one square up (from (i,j) to (i−1,j)) if there is no pawn in the destination square. And a pawn can move one square diagonally up (from (i,j) to either (i−1,j−1) or (i−1,j+1)) if and only if there is an enemy pawn in that square. That enemy pawn is also removed. We have to find the maximum number of his pawns that can reach row 1? Only we can move pawns and the enemy pawns never move. Also, when our pawn reaches row 1, it is stuck and cannot make any further moves. The S and T represents enemy pawn series and our pawn series respectively. If S[i] or T[i] is 1, there is a pawn, and if it is 0, that cell is empty.
Problem Category
The above-mentioned problem can be solved by applying Greedy problem-solving techniques. The greedy algorithm techniques are types of algorithms where the current best solution is chosen instead of going through all possible solutions. Greedy algorithm techniques are also used to solve optimization problems, like its bigger brother dynamic programming. In dynamic programming, it is necessary to go through all possible subproblems to find out the optimal solution, but there is a drawback of it; that it takes more time and space. So, in various scenarios greedy technique is used to find out an optimal solution to a problem. Though it does not gives an optimal solution in all cases, if designed carefully it can yield a solution faster than a dynamic programming problem. Greedy techniques provide a locally optimal solution to an optimization problem. Examples of this technique include Kruskal's and Prim's Minimal Spanning Tree (MST) algorithm, Huffman Tree coding, Dijkstra's Single Source Shortest Path problem, etc.
https://www.tutorialspoint.com/data_structures_algorithms/greedy_algorithms.htm
https://www.tutorialspoint.com/data_structures_algorithms/dynamic_programming.htm
So, if the input of our problem is like S = "000"; T = "111", then the output will be 3, because we can simply advance all 3 of the pawns forward. Thus, the answer is 3.
Steps
To solve this, we will follow these steps −
ans := 0 n := size of S for initialize i := 0, when i < n, update (increase i by 1), do: if T[i] is same as '1', then: if S[i] is same as '0', then: (increase ans by 1) otherwise when S[i - 1] is same as '1', then: S[i - 1] = '0', increase ans by 1 otherwise when S[i + 1] is same as '1', then: S[i + 1] = '0', increase ans by 1 return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(string S, string T){ int ans = 0; int n = S.size(); for (int i = 0; i < n; i++){ if (T[i] == '1'){ if (S[i] == '0') ans++; else if (S[i - 1] == '1') S[i - 1] = '0', ans++; else if (S[i + 1] == '1') S[i + 1] = '0', ans++; } } return ans; } int main(){ string S = "000"; string T = "111"; cout << solve(S, T) << endl; }
Input
"000", "111"
Output
3
- Related Articles
- Program to find how many years it will take to reach t amount in Python
- C++ program to check how many students have greater score than first one
- C++ program to find in how many bases we can represent the number not greater than M
- Program to find minimum number of roads we have to make to reach any city from first one in C++
- Write a Python program to find the average of first row in a Panel
- Program to find out the farthest building a parkour artist can reach in Python
- Program to find how many ways we can climb stairs in Python
- C++ program to find maximum how many chocolates we can buy with at most k rupees
- How to write the first C++ program?
- C++ Program to find out how many movies an attendee can watch entirely at a Film festival
- Program to find maximum how many water bottles we can drink in Python
- Program to find out how many transfer requests can be satisfied in Python
- How to find the first empty row of an excel file in Python?
- Program to find out how many boxes can be put into the godown in Python
- Program to find number of ways we can reach to the next floor using stairs in Python
