
- 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 table plate ordering maintaining given conditions
Suppose we have two numbers h and w. The height and width of a table. The table is divided into h × w cells. Into each cell of the table, we can either put a plate or keep it empty. As each guest has to be seated next to their plate, we can only put plates on the edge of the table — into the first or the last row of the rectangle, or into the first or the last column. To make the guests comfortable, no two plates must be put into cells that have a common side or corner. In other words, if the cell (i,j) contains a plate, we can't put plates into cells (i−1,j), (i,j−1), (i+1,j), (i,j+1), (i−1,j−1), (i−1,j+1), (i+1,j−1), (i+1,j+1). Put as many plates as possible with maintaining the above rules. We have to print the table current status.
Problem Category
An array in the data structure is a finite collection of elements of a specific type. Arrays are used to store elements of the same type in consecutive memory locations. An array is assigned a particular name and it is referenced through that name in various programming languages. To access the elements of an array, indexing is required. We use the terminology 'name[i]' to access a particular element residing in position 'i' in the array 'name'. Various data structures such as stacks, queues, heaps, priority queues can be implemented using arrays. Operations on arrays include insertion, deletion, updating, traversal, searching, and sorting operations. Visit the link below for further reading.
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm
So, if the input of our problem is like h = 3; w = 5, then the output will be
1 | 0 | 1 | 0 | 1 |
0 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 0 | 1 |
Steps
To solve this, we will follow these steps −
for initialize i := 1, when i <= h, update (increase i by 1), do: for initialize j := 1, when j <= w, update (increase j by 1), do: if i is same as 1 or i is same as h, then: print 1 if (j AND 1) is 1, otherwise 0 otherwise when i is not equal to 2 and i is not equal to h - 1 and i is odd and (j is same as 1 or j is same as w), then: print 1 Otherwise print 0 move cursor to the next line
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int h, int w){ for (int i = 1; i <= h; ++i){ for (int j = 1; j <= w; ++j){ if (i == 1 || i == h) cout << ((j & 1)) << " "; else if (i != 2 && i != h - 1 && (i & 1) && (j == 1 || j == w)) cout << 1 << " "; else cout << 0 << " "; } cout << endl; } } int main(){ int h = 3; int w = 5; solve(h, w); }
Input
3, 5
Output
1 0 1 0 1 0 0 0 0 0 1 0 1 0 1
- Related Articles
- Program to check whether given words are maintaining given pattern or not in C++
- Find the ordering of tasks from given dependencies in C++
- Program to find number of tasks can be finished with given conditions in Python
- Forests are not responsible for(a) providing medicinal plants(b) maintaining the flow of water into the streams(c) creating flood conditions(d) absorbing rainwater and maintaining water table
- C++ code to find array from given array with conditions
- Program to find number of elements in all permutation which are following given conditions in Python
- Program find number of subsets of colorful vertices that meets given conditions in Python
- C++ program to count at least how many operations needed for given conditions
- How ANALYZE TABLE statement helps in maintaining the MySQL tables?
- Program to check number of requests that will be processed with given conditions in python
- How to replace rows in a MySQL table with conditions?
- Python program to sort table based on given attribute index
- Program to find hoe many children will get candies while distributing them maintaining the rules in Python
- Program to find number of ways we can merge two lists such that ordering does not change in Python
- How to create conditions in a MySQL table with multiple columns?
