# 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

*.*.
....
*.*.
....