
- 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 sides that a polygon has inside a grid
Suppose, we are given a grid of dimensions h x w. There are two types of cells in the grid, white and black cells. White cells are represented by '.', whereas black cells are represented by '#'. Now the grid has multiple black cells in it that form a polygon. We have to find out the number of sides that the polygon has. It is to be noted, that the outermost cells of the grid are always white.
So, if the input is like h = 4, w = 4, grid = {"....", ".##.", ".##.", "...."}, then the output will be 4.
The black cells form a square, and a square has 4 sides.
Steps
To solve this, we will follow these steps −
sides := 0 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: bl := 0 if grid[i - 1, j - 1] is same as '#', then: (increase bl by 1) if grid[i - 1, j] is same as '#', then: (increase bl by 1) if grid[i, j - 1] is same as '#', then: (increase bl by 1) if grid[i, j] is same as '#', then: (increase bl by 1) if bl is same as 1 or 3, then: (increase sides by 1) return sides
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int h, int w, vector<string> grid){ int sides = 0; for(int i = 1; i < h; i++) { for(int j = 1; j < w; j++) { int bl = 0; if(grid.at(i - 1).at(j - 1) == '#') { bl++; } if(grid.at(i - 1).at(j) == '#') { bl++; } if(grid.at(i).at(j - 1) == '#') { bl++; } if(grid.at(i).at(j) == '#') { bl++; } if(bl == 1 or bl == 3) { sides++; } } } cout << sides; } int main() { int h = 4, w = 4; vector<string> grid = {"....", ".##.", ".##.", "...."}; solve(h, w, grid); return 0; }
Input
4, 4, {"....", ".##.", ".##.", "...."}
Output
4
- 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
- The interior angle of a regular polygon is $156$ . Find the number of sides of the polygon.
- C++ Program to find out the number of cells to block in a grid to create a path
- C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid
- C++ program to find out the number of ways a grid with boards can be colored
- 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 jumps needed for a robot to reach a particular cell in a grid
- Each interior angle of a regular polygon is four times the exterior angle. Find the number of sides in the polygon.
- C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- C++ Program to find out if there is a pattern in a grid
- C++ program to find out number of changes required to get from one end to other end in a grid
- How to find the number of diagonals of a polygon?
- What is a regular polygon?State the name of a regular polygon of(i) 3 sides(ii) 4 sides(iii) 6 sides.

Advertisements