- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find out the center coordinates and the height of a building
Suppose, there is a building that has center coordinates xc, yc, and height h. We don't know the center coordinates of the building, but we are provided with n pieces of information that contain x and y coordinates and an altitude value a. The altitude of coordinates (x, y) is the maximum of (h - |x - xc| - |y - yc|, 0). We have to find out the center coordinates and the height of the building. The coordinate xi is given in the array x, yi is given in teg array y, and ai is given in array a.
So, if the input is like n = 3, x = {3, 3, 2}, y = {4, 2, 3}, a = {6, 6, 6}, then the output will be 3 3 7.
The center coordinates are 3,3 and the height of the building is 7.
Steps
To solve this, we will follow these steps −
check := true for initialize xc := 0, when xc <= 100, update (increase xc by 1), do: for initialize yc := 0, when yc <= 100, update (increase yc by 1), do: check := true mh := 2000000000 h := -1 for initialize i := 0, when i < n, update (increase i by 1), do: k := |(x[i] - xc) + |y[i] - yc|| if a[i] is same as 0, then: mh := minimum of mh and k else: if h < 0, then: h := a[i] + k otherwise when h is not equal to a[i] + k, then: check := false Come out from the loop if h > mh, then: check := false Ignore following part, skip to the next iteration if check is non-zero, then: Come out from the loop if check is non-zero, then: Come out from the loop print(xc, yc, h)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int n, vector<int> x, vector<int> y, vector<int> a){ bool check = true; int xc, yc, h; for (xc = 0; xc <= 100; xc++) { for (yc = 0; yc <= 100; yc++) { check = true; int k, mh = 2e9; h = -1; for(int i = 0; i < n; i++) { k = abs(x[i] - xc) + abs(y[i] - yc); if (a[i] == 0) { mh = min(mh, k); } else { if (h < 0) { h = a[i] + k; } else if (h != a[i] + k) { check = false; break; } } } if (h > mh) { check = false; continue; } if (check) { break; } } if (check) { break; } } cout << xc << " " << yc << " " << h; } int main() { int n = 3; vector<int> x = {3, 3, 2}, y = {4, 2, 3}, a = {6, 6, 6}; solve(n, x, y, a); return 0; }
Input
3, {3, 3, 2}, {4, 2, 3}, {6, 6, 6}
Output
3 3 7
- Related Articles
- C++ Program to find out the cost to travel all the given coordinates
- Program to find maximum building height in Python
- How to find the real center coordinates of a Line object using FabricJS?
- Program to find out the farthest building a parkour artist can reach in Python
- FabricJS – How to find the real center coordinates of an Image object?
- C++ Program to find out the health of a character
- Program to find out the number of integral coordinates on a straight line between two points in Python
- C program to find in which quadrant the coordinates lie.
- The angles of depression of the top and bottom of ( 8 mathrm{~m} ) tall building from the top of a multistoried building are ( 30^{circ} ) and ( 45^{circ} ) respectively. Find the height of the multistoried building and the distance between the two buildings.
- A building is in the form of a cylinder surmounted by a hemispherical dome. The base diameter of the dome is equal to ( frac{2}{3} ) of the total height of the building. Find the height of the building, if it contains ( 67 frac{1}{21} mathrm{~m}^{3} ) of air.
- C++ program to find out the maximum value of i
- C++ Program to find out the total price
- C++ Program to find out the number of border cells in a grid
- C++ program to find out the maximum sum of a minimally connected graph
- C++ Program to find out the number of illuminated cells in a grid
