Queen Bee
Time limit2sMemory limit256 MB
Simulate N days of growth on an M by M grid where each inner cell copies the largest daily growth among its left, upper-left, and upper neighbors.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
A beehive is an grid. One larva that will become a queen bee grows in each cell.
Coordinates are set like this. The top left cell is . Each step down increases the first number by 1, giving , , and so on. Each step right from cell increases the second number by 1, giving , , and so on.
Every larva grows once a day, at noon. The growing takes so little time that you can ignore it. On the morning of the first day every larva has size 1, and the process repeats for days.
A larva grows by 0, 1, or 2 in a day. The rules that fix the amount are these.
- The larvae in the leftmost column and in the top row decide their own growth. Those amounts are given in the input. Read them starting at the bottom left cell, moving up to the top left cell, then moving right to the end of the top row. In every input the sequence read this way is non-decreasing.
- Every other larva waits until the larvae to its left (L), to its upper left (D), and above it (U) have grown, then grows by as much as whichever of those three grew the most that day.
Here is an example with and . The sizes on the morning of the first day are:
Suppose that the growth of the 7 larvae in the leftmost column and the top row, read in the order described above, is:
- Day 1: 0, 0, 1, 1, 1, 2, 2
- Day 2: 1, 1, 1, 1, 1, 1, 2
On the evening of the first day the sizes are as follows. For example, the larva at has a left neighbour that grew by 1, an upper left neighbour that grew by 1, and an upper neighbour that grew by 1, so it grows by 1 too. The larva at grows by 2 under the same rule.
After the second day the same process gives:
Write a program that reads the grid size, the number of days, and the daily growth of the larvae in the leftmost column and the top row, then prints the size of every larva on the evening of the last day.
Input
The first line contains the side length of the grid () and the number of days (), separated by a space. The sizes on the first morning are all 1, so they are not given.
Each of the next lines describes one day, in order from the first day, and gives the growth of the larvae in the leftmost column and the top row on that day. The values read in the order described in the statement are non-decreasing, so each line gives the number of 0s, the number of 1s, and the number of 2s in that sequence, in that order. The three numbers always add up to , and any of them can be 0.
Output
Print lines with natural numbers each, separated by spaces. The -th number on the -th line is the size of the larva at coordinate on the evening of the last day.