
- 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++ 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 < n, update (increase i by 1), do: for initialize j := 0, when j < m, update (increase j by 1), do: if matrix[i, j] is same as 'B', then: increase cnt by 1 X := X + i Y := Y + j X := X / cnt Y := Y / cnt return (Y, X)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<vector<char>> matrix){ int n = matrix.size(); int m = matrix[0].size(); int cnt = 0, X = 0, Y = 0; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++) if (matrix[i][j] == 'B') cnt++, X += i, Y += j; } X /= cnt; Y /= cnt; printf("%d, %d\n", Y, X); } int main(){ vector<vector<char>> 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
- Related Articles
- C++ code to find total time to pull the box by rabbit
- C++ code to check pattern is center-symmetrical or not
- Code to find the center of an array without using ES6 functions - JavaScript
- How to center the JavaScript alert message box?
- How to Align the modal content box to the center of any screen?
- C++ code to find winner of math contest
- What is the purpose of the Inner Engineering Program by ISHA YOGA Center?
- How to place the text at the center of an Entry box in Tkinter?
- C++ code to find out number of battery combos
- C++ code to find the number of scans needed to find an object
- C++ code to find rank of student from score table
- C++ code to find position of students after coding contest
- C++ code to find reduced direction string of robot movement
- C++ code to find minimum arithmetic mean deviation
- C++ code to find minimal tiredness after meeting

Advertisements