Log inSign up
Problems / B086

Erosion of the island

400 ptsCases 15Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

There is a sea chart shaped as a grid, N cells down and M cells across. Each cell holds 1 for land and 0 for sea. Every cell on the border of the chart is sea.

The land on this chart is worn away by the sea. Each time a year passes, the following happens all at once. If a land cell touches two or more cells of outer sea among the four cells next to it across and down, that land cell erodes and becomes sea a year later. If only one of the four cells is outer sea, or none of them is, that land cell stays through the year. Cells touching at a diagonal are not counted.

Outer sea here means sea that can be reached on foot from the border of the chart by stepping only on sea cells. Sea that is enclosed by land and cut off from the outside is a lake, and a side touching a lake is not counted.

Write a program that takes the starting chart and finds how many years it takes until all the land has eroded away.

For example, suppose N=5 and M=5 and the chart is the one in the table below.

00000
00010
00010
00000
00000

The upper land cell touches outer sea on three sides, above, left, and right, and the lower land cell touches outer sea on three sides, below, left, and right. Both have two or more sides, so they erode away together a year later. The answer in this case is therefore 1.

Now look at a case with a lake. Suppose N=7 and M=7 and the land sits in a ring that surrounds the middle.

0000000
0111110
0100010
0100010
0100010
0111110
0000000

The nine cells in the middle of the ring are sea, but land blocks them off from the outside, so they are a lake. The four corners of the ring touch outer sea on two sides, so they erode in the first year. The cells that are not corners touch outer sea on one side only, while the other side touches the lake, so they stay through the first year. In the second year the spots where the corners disappeared become outer sea, so the cells beside them touch on two sides and erode together. In the third year the four cells that are left all erode. The answer in this case is therefore 3.

Input and Output Examples

Example 1

Input

5 5
0 0 0 0 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 0
0 0 0 0 0

Output

1

Example 2

Input

3 3
0 0 0
0 0 0
0 0 0

Output

0

Example 3

Input

8 9
0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 1 1 0 1 1 0
0 0 1 1 1 1 1 1 0
0 0 1 1 1 1 1 0 0
0 0 1 1 0 1 1 0 0
0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0

Output

4

Input

  • First the number of cells down N and the number of cells across M of the chart are given on one line, separated by a space. N and M are each a natural number less than or equal to 100.

  • Then N lines follow, giving the lines of the chart from top to bottom in order. Each line holds M values separated by spaces, and each value is 1 for land and 0 for sea. The first line and the last line of the chart, and the first cell and the last cell of every line, are all 0.

Output

  • Print the number of years it takes until all the land has eroded away on one line. If there is no land at all from the start, print 0. Only one number is printed, so no space is left at the end of the line.

Requirements

  • One year of erosion happens to every land cell all at once. A cell that eroded in a given year does not erode another cell further in that same year
  • Only a land cell touching outer sea on two or more sides erodes
  • A lake enclosed by land and cut off from the outside is not outer sea, so do not count a side touching a lake
  • Look only at the four directions across and down for touching cells. Do not look at diagonals
  • The border of the chart is always sea, so the outer sea spreads out from the border
  • 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.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
3 3
0 0 0
0 0 0
0 0 0
0
5 5
0 0 0 0 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 0
0 0 0 0 0
1
8 9
0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 1 1 0 1 1 0
0 0 1 1 1 1 1 1 0
0 0 1 1 1 1 1 0 0
0 0 1 1 0 1 1 0 0
0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0
4

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.

0 chars1 lineMemory 256MB (recorded only)Reference 74ms

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.

1bampal21.75s955 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.