- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- 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 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
- Program to find out the path between two vertices in a graph that has the minimum penalty (Python)
- C++ Program to find out if a person has won lottery
- C++ Program to find out if there is a pattern in a grid
- 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 the number of pairs in an array that satisfy a given condition
- Program to find perimeter of a polygon in Python
- Program to find area of a polygon in Python

Advertisements