Stacking Blocks
InterviewTime limit1sMemory limit128 MB
Given a 0/1 top view plus front and side height maxima, output the tallest cube stack arrangement matching all three views, or -1.
- Level
Medium4 of 10
- Topics
- Greedy, Matrix, Implementation
- Solved
- No attempts yet
Problem
Unit cubes are stacked on an grid. A cell can hold zero or more cubes, and the number of cubes on a cell is the height of that cell.
Three views of the solid are given.
- The top view is rows of values, each 0 or 1. A cell of height 1 or more is 1, and a cell of height 0 is 0.
- The front view is numbers. The -th number is the largest height among the cells in column .
- The right side view is numbers. A viewer standing on the right sees the last row of the top view on the far left, so the -th number is the largest height among the cells in row .
Several height assignments can match all three views. Find the one that uses the most cubes. If such an assignment exists, it is the only one.
Input
The first line has the vertical length and the horizontal length , separated by a space. ()
Each of the next lines has values, 0 or 1, giving the top view.
The next line has the front view as integers. Each number is between 0 and 100.
The last line has the right side view as integers. Each number is between 0 and 100.
Output
Print on lines the arrangement that matches all three views and uses the most cubes. Each line holds the heights of the cells in that row, separated by single spaces.
If no arrangement matches the three views, print -1.