
- 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 if there is a pattern in a grid
Suppose, we are given a grid of dimensions n * n. We have to detect if there is a cross pattern in the grid, like below −
#...# .#.#. ..#.. .#.#. #...#
The grid can only contain '#' and '.'. We have to detect the pattern and find out how many such patterns are in the grid. The grid and the dimension are given to us as input.
Problem Category
Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.
So, if the input of our problem is like n = 5, and grid =
#...# .#.#. ..#.. .#.#. #...#,
then the output will be 1.
Steps
To solve this, we will follow these steps −
count := 0 for initialize i := 1, when i < n - 1, update (increase i by 1), do: for initialize j := 1, when j < n - 1, update (increase j by 1), do: if grid[i, j] is same as '#' and grid[i - 1, j - 1] is same as '#' and grid[i - 1, j + 1] is same as '#' and grid[i + 1, j - 1] is same as '#' and grid[i + 1, j + 1] is same as '#', then: (increase count by 1) print(count)
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; void solve(int n, vector<string> grid) { int count = 0; for(int i = 1; i < n - 1; i++){ for(int j = 1; j < n - 1; j++){ if(grid[i][j] == '#' && grid[i - 1][j - 1] == '#' && grid[i - 1][j + 1] == '#' && grid[i + 1][j - 1] == '#' && grid[i + 1][j + 1] == '#') count++; } } cout<< count; } int main() { int n = 5; vector<string> grid = {"#...#", ".#.#.", "..#..", ".#.#.", "#...#"}; solve(n, grid); return 0; }
Input
5, {"#...#", ".#.#.", "..#..", ".#.#.", "#...#"}
Output
1
- Related Articles
- C++ code to find out if a grid is fully accessible
- Program to Find Out if There is a Short Circuit in Input Words in Python
- 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 total cost required for a robot to make a trip 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 a cleaning robot can clean in a grid
- Program to find out if a BST is present in a given binary tree in Python
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
- C++ program to find out the number of ways a grid with boards can be colored
- Program to find out if we win in a game in Python
- Program to find out if a linked list is present in a given binary tree in Python
- C++ Program to find out if a round trip is possible from a particular city
