
- 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 the number of ways a grid with boards can be colored
Suppose, we are given a grid that has 2 rows and n columns. The grid has to be covered by n boards without one board getting over another. Now, the boards have to be colored by any one color between red, blue, and green. Two boards that are adjacent to each other cannot be colored by the same color and if not necessary, all colors do not have to be used. The configuration of the grid is given in the array 'grid', where a particular board in the grid is represented using the same English letter and different boards are represented using different English letters. We have to find out the number of ways the boards can be colored.
So, if the input is like n = 4, grid = {"abbd", "accd"}, then the output will be 6.
There are 6 different ways to color the boards satisfying the given criterion.
Steps
To solve this, we will follow these steps −
MODVAL := 10^9 + 7 Define an array s for initialize i := 0, when i < n, do: if grid[0, i] is same as grid[1, i], then: insert 1 at the end of s (increase i by 1) Otherwise, insert 2 at the end of s i := i + 2 Define an array tvec if s[0] is same as 1, then: tvec[0] := 3 Otherwise, tvec[0] := 6 for initialize i := 1, when i < size of s, update (increase i by 1), do: if s[i - 1] is same as 2 and s[i] is same as 2, then: tvec[i] := tvec[i - 1] * 3 mod MODVAL if s[i - 1] is same as 2 and s[i] is same as 1, then: tvec[i] := tvec[i - 1] if s[i - 1] is same as 1 and s[i] is same as 2, then: tvec[i] := tvec[i - 1] * 2 mod MODVAL if s[i - 1] is same as 1 and s[i] is same as 1, then: tvec[i] := tvec[i - 1] * 2 mod MODVAL return tvec[size of s - 1]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n, vector<string> grid){ int MODVAL = 1e9 + 7; vector<int> s; for (int i = 0; i < n;) { if (grid[0][i] == grid[1][i]) { s.push_back(1); i++; } else { s.push_back(2); i += 2; } } vector<int> tvec(s.size()); if (s[0] == 1) tvec[0] = 3; else tvec[0] = 6; for (int i = 1; i < (int)s.size(); i++) { if (s[i - 1] == 2 && s[i] == 2) tvec[i] = tvec[i - 1] * 3 % MODVAL; if (s[i - 1] == 2 && s[i] == 1) tvec[i] = tvec[i - 1]; if (s[i - 1] == 1 && s[i] == 2) tvec[i] = tvec[i - 1] * 2 % MODVAL; if (s[i - 1] == 1 && s[i] == 1) tvec[i] = tvec[i - 1] * 2 % MODVAL; } return tvec[s.size() - 1]; } int main() { int n = 4; vector <string> grid = {"abbd", "accd"}; cout<< solve(n, grid); return 0; }
Input
4, {"abbd", "accd"}
Output
6
- Related Articles
- 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
- C++ program to find out the maximum number of cells a cleaning robot can clean 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 maximum number of cells that can be illuminated
- C++ program to find out the number of coordinate pairs that can be made
- Python Program to find out the number of rooms in which a prize can be hidden
- C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid
- Program to find out number of blocks that can be covered in Python
- C++ Program to find out the number of cells to block in a grid to create a path
- Number of Ways to Paint N × 3 Grid in C++ program
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
