Where the ink has spread
Statement
Drops of ink fall on blotting paper divided into a grid of N rows and N columns. There are K kinds of ink, numbered from 1 to K. Each cell of the grid holds one ink drop or nothing at all. Drops of the same number may sit on several cells, and a number that is not placed at all may exist.
The ink spreads once every second. From a cell that holds ink, ink of the same number spreads into the cells touching it up, down, left, or right that hold nothing yet. No other ink can enter a cell that already holds ink, and the number of the ink on a cell never changes once it is there.
Within each second the ink with the lower number spreads first. That is, ink 1 takes every cell it spreads into, then ink 2 spreads, and then ink 3 spreads. Among drops of the same number the result is the same whichever drop spreads first.
Write a program that computes the number of the ink on the cell in row X and column Y after exactly S seconds. Row and column numbers start at 1.
For example, suppose that on blotting paper with N=3 and K=3 the ink drops sit as in the table below. A cell with nothing on it is written as 0.
| 1 | 0 | 2 |
|---|---|---|
| 0 | 0 | 0 |
| 3 | 0 | 0 |
After 1 second the paper looks like this. Ink 1 spreads first and takes (1, 2) and (2, 1). When ink 2 spreads next, (1, 2) is already taken, so it takes only (2, 3). When ink 3 spreads last, (2, 1) is already taken, so it takes only (3, 2).
| 1 | 1 | 2 |
|---|---|---|
| 1 | 0 | 2 |
| 3 | 3 | 0 |
After 2 seconds the paper looks like this. Ink 1 takes (2, 2) and ink 2 takes (3, 3). No empty cell is left for ink 3 to spread into.
| 1 | 1 | 2 |
|---|---|---|
| 1 | 1 | 2 |
| 3 | 3 | 2 |
After S=2 seconds the number of the ink on the cell in row X=3 and column Y=2 is therefore 3.
Input and Output Examples
Example 1
Input
3 3 1 0 2 0 0 0 3 0 0 2 3 2
Output
3
Example 2
Input
2 1 0 0 0 1 1 1 2
Output
1
Example 3
Input
8 3 3 0 0 0 1 0 0 0 0 3 3 0 3 0 0 0 0 0 0 0 0 0 0 0 2 0 3 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 2 0 0 0 1 0 0 0 0 0 0 1 3 7
Output
0
Input
First the side length N of the blotting paper and the number of kinds of ink K are given on one line, separated by a space. N is a natural number from 1 to 200, and K is a natural number from 1 to 20.
Then N lines follow, giving the rows of the paper from top to bottom. Each line holds N numbers separated by spaces. Each number is an integer from 0 to K, where 0 means a cell with nothing on it and any other number means the number of the ink drop sitting on that cell.
Then the elapsed time S and the row X and the column Y of the cell to look at are given on one line, separated by spaces. S is a natural number from 1 to 400, and X and Y are each a natural number from 1 to N.
Output
Print the number of the ink on the cell in row X and column Y after exactly S seconds as a single number on one line. If no ink is on that cell, print 0.
Requirements
- The ink spreads into the empty cells touching it up, down, left, or right once every second
- No other ink can enter a cell that already holds ink
- Within each second the inks spread in order, starting with the one that has the lowest number
- Read the value at exactly S seconds, and print 0 if that cell is empty
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 1 0 0 0 1 1 1 2
1
8 3 3 0 0 0 1 0 0 0 0 3 3 0 3 0 0 0 0 0 0 0 0 0 0 0 2 0 3 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 2 0 0 0 1 0 0 0 0 0 0 1 3 7
0
3 3 1 0 2 0 0 0 3 0 0 2 3 2
3
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.