C++ program to check we can place dominos on colored cells in correct order or not

C++Server Side ProgrammingProgramming

Suppose we have five numbers n, k1, k2, w and b. There is a board with 2 x n cells and first k1 cells in the first row, first k2 cells in second row are colored in white. All other cells are black. We have w white dominos and b black dominos (2 x 1 size). We can place a white domino on the board if both board's cells are white and not occupied by any other domino. In the same way, a black domino can be placed if both cells are black and not occupied by any other domino. We have to check whether we can place all w + b dominoes on the board if they are placed both horizontally and vertically?

So, if the input is like n = 5; k1 = 4; k2 = 3; w = 3; b = 1, then the output will be True.

Steps

To solve this, we will follow these steps −

if 2 * w <= (k1 + k2) and 2 * b <= (n - k1 + n - k2), then:
   return true
Otherwise
   return false

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

bool solve(int n, int k1, int k2, int w, int b) {
   if (2 * w <= (k1 + k2) && 2 * b <= (n - k1 + n - k2)) {
      return true;
   }
   else {
      return false;
   }
}
int main() {
   int n = 5;
   int k1 = 4;
   int k2 = 3;
   int w = 3;
   int b = 1;
   cout << solve(n, k1, k2, w, b) << endl;
}

Input

5, 4, 3, 3, 1

Output

1
raja
Updated on 03-Mar-2022 06:17:10

Advertisements