There is a legend about a magical park with N × N trees. The trees are positioned in a square grid with N rows (numbered from 1 to N from north to south) and N columns (numbered from 1 to N from west to east). The height (in metres) of each tree is an integer between 1 and N × N, inclusive. Magically, the height of all trees is unique. Bunga is standing on the northmost point of the park and wants to count the number of visible trees for each column. Similarly, Lestari is standing on the westmost point of the park and wants to count the number of visible trees for each row. A tree X is visible if all other trees in front of the tree X are shorter than the tree X. For example, let N = 3 and the height (in metres) of the trees are as follows. 6 1 8 7 5 3 2 9 4 • On the first column, Bunga can see two trees, as the tree on the third row is obstructed by the other trees. • On the second column, Bunga can see all three trees. • On the third column, Bunga can see only the tree on the first row, as the other trees are obstructed by the tree on the first row. • On the first row, Lestari can see two trees, as the tree on the second column is obstructed by the tree on the first column. • On the second row, Lestari can see only the tree on the first column, as the other trees are obstructed by the tree on the first column. • On the third row, Lestari can see two trees, as the tree on the third column is obstructed by the tree on the second column. Your task is to determine the numbers reported by Bunga and Lestari. Input Input begins with a line containing an integer: N (1 ≤ N ≤ 10) representing the number of trees on each row and column. The next N lines each contains N integers representing the height (in metres) of the trees. The j th integer on the i th line is the height of the tree on the i th row and j th column between 1 to N × N, inclusive. It is guaranteed that all trees have different heights. Output Output begins with a line containing N integers representing the number of trees visible by Bunga. The j th integer is the number of visible trees on the j th column. The next N lines each contains an integer representing the number of trees visible by Lestari. The integer on the i th line is the number of visible trees on the i th row.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

There is a legend about a magical park with N × N trees. The trees are positioned in a square grid with N
rows (numbered from 1 to N from north to south) and N columns (numbered from 1 to N from west to east).
The height (in metres) of each tree is an integer between 1 and N × N, inclusive. Magically, the height of
all trees is unique.
Bunga is standing on the northmost point of the park and wants to count the number of visible trees for each
column. Similarly, Lestari is standing on the westmost point of the park and wants to count the number of
visible trees for each row. A tree X is visible if all other trees in front of the tree X are shorter than the tree
X.
For example, let N = 3 and the height (in metres) of the trees are as follows.
6 1 8
7 5 3
2 9 4
• On the first column, Bunga can see two trees, as the tree on the third row is obstructed by the other
trees.
• On the second column, Bunga can see all three trees.
• On the third column, Bunga can see only the tree on the first row, as the other trees are obstructed by
the tree on the first row.
• On the first row, Lestari can see two trees, as the tree on the second column is obstructed by the tree
on the first column.
• On the second row, Lestari can see only the tree on the first column, as the other trees are obstructed
by the tree on the first column.
• On the third row, Lestari can see two trees, as the tree on the third column is obstructed by the tree on
the second column.
Your task is to determine the numbers reported by Bunga and Lestari.
Input
Input begins with a line containing an integer: N (1 ≤ N ≤ 10) representing the number of trees on each
row and column. The next N lines each contains N integers representing the height (in metres) of the trees.
The j
th integer on the i
th line is the height of the tree on the i
th row and j
th column between 1 to N × N,
inclusive. It is guaranteed that all trees have different heights.

Output
Output begins with a line containing N integers representing the number of trees visible by Bunga. The
j
th integer is the number of visible trees on the j
th column. The next N lines each contains an integer
representing the number of trees visible by Lestari. The integer on the i
th line is the number of visible trees
on the i
th row.

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Topological Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education