
- 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 matrix with marked asterisks region
Suppose we have a n x n grid of characters with dots (.) and asterisks (*). All cells are marked as dot except two cells. We have to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. If there are multiple solutions available, return any one of them.
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
. | . | * | . |
. | . | . | . |
* | . | . | . |
. | . | . | . |
then the output will be
* | . | * | . |
. | . | . | . |
* | . | * | . |
. | . | . | . |
Steps
To solve this, we will follow these steps −
n := size of M x1 := -1, x2 := -1, y1 = -1, y2 = -1 for initialize i := 0, when i < n, update (increase i by 1), do: for initialize j := 0, when j < n, update (increase j by 1), do: if M[i, j] is same as '*' and x1 is same as -1, then: x1 := i, y1 := j if M[i, j] is same as '*' and x1 is not equal to -1, then: x2 := i, y2 := j if x1 is same as x2, then: if x1 is not equal to n - 1, then: M[x1 + 1, y1] = M[x2 + 1, y2] = '*' Otherwise M[x1 - 1, y1] = M[x2 - 1, y2] = '*' otherwise when y1 is same as y2, then: if y1 is not equal to n - 1, then: M[x1, y1 + 1] = M[x2, y2 + 1] = '*' Otherwise M[x1, y1 - 1] = M[x2, y2 - 1] = '*' Otherwise M[x1, y2] := M[x2, y1] = '*' for initialize i := 0, when i < n, update (increase i by 1), do: for initialize j := 0, when j < n, update (increase j by 1), do: print M[i, j] 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(vector<vector<char>> M){ int i, j, x1, y1, x2, y2; int n = M.size(); x1 = x2 = y1 = y2 = -1; for (i = 0; i < n; i++){ for (j = 0; j < n; j++){ if (M[i][j] == '*' && x1 == -1) x1 = i, y1 = j; if (M[i][j] == '*' && x1 != -1) x2 = i, y2 = j; } } if (x1 == x2){ if (x1 != n - 1) M[x1 + 1][y1] = M[x2 + 1][y2] = '*'; else M[x1 - 1][y1] = M[x2 - 1][y2] = '*'; } else if (y1 == y2){ if (y1 != n - 1) M[x1][y1 + 1] = M[x2][y2 + 1] = '*'; else M[x1][y1 - 1] = M[x2][y2 - 1] = '*'; } else M[x1][y2] = M[x2][y1] = '*'; for (i = 0; i < n; i++){ for (j = 0; j < n; j++) printf("%c", M[i][j]); printf("\n"); } } int main(){ vector<vector<char>> matrix = { { '.', '.', '*', '.' }, { '.', '.', '.', '.' }, { '*', '.', '.', '.' }, { '.', '.', '.', '.' } }; solve(matrix); }
Input
{ { '.', '.', '*', '.' }, { '.', '.', '.', '.' }, { '*', '.', '.', '.' }, { '.', '.', '.', '.' } }
Output
*.*. .... *.*. ....
- Related Articles
- C# Program to replace a character with asterisks in a sentence
- Python Program to replace a word with asterisks in a sentence
- Java Program to replace a word with asterisks in a sentence
- Replacing words with asterisks in C++
- Find length of the largest region in Boolean Matrix in C++
- C++ program to find last value of matrix with given constraints
- C++ Program to Find Transpose of a Matrix
- Java Program to Find Transpose of a Matrix
- C++ Program to Find Inverse of a Graph Matrix
- Python Program to find the transpose of a matrix
- C++ Program to Find Fibonacci Numbers using Matrix Exponentiation
- C++ Program to Find Transpose of a Graph Matrix
- Golang Program To Find The Transpose Of A Matrix
- Usage of Asterisks in Python
- Python program to remove rows with duplicate element in Matrix
