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

Updated on: 08-Apr-2022

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements