C++ code to find center of inner box

Suppose we have a matrix of size n x m. Cells are either 'W' as white or 'B' as black. Some square inside the table with odd length was painted black. We have to find the center of this square.

So, if the input is like

W W B B B W
W W B B B W
W W B B B W
W W W W W W
W W W W W W

then the output will be (3, 1).

Steps

To solve this, we will follow these steps −

n := row count of matrix
m := column count of matrix
cnt := 0
X := 0
Y := 0
for initialize i := 0, when i 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
void solve(vector> matrix){
   int n = matrix.size();
   int m = matrix[0].size();
   int cnt = 0, X = 0, Y = 0;
   for (int i = 0; i > matrix = { { 'W', 'W', 'B', 'B', 'B', 'W' },
      { 'W', 'W', 'B', 'B', 'B', 'W' }, { 'W', 'W', 'B', 'B', 'B', 'W' },
      { 'W', 'W', 'W', 'W', 'W', 'W' }, { 'W', 'W', 'W', 'W', 'W', 'W' } };
   solve(matrix);
}

Input

{ { 'W', 'W', 'B', 'B', 'B', 'W' },
   { 'W', 'W', 'B', 'B', 'B', 'W' }, { 'W', 'W', 'B', 'B', 'B', 'W' },
   { 'W', 'W', 'W', 'W', 'W', 'W' }, { 'W', 'W', 'W', 'W', 'W', 'W' } }

Output

3, 1
Updated on: 2022-03-29T11:42:12+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements