Log inSign up
Problems / B075

Protecting the dig area

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

Statement

A dig site is managed as a grid of N rows and M columns. Each cell is in one of three states. A cell with value 0 is bare ground with nothing on it yet. A cell with value 1 is solid bedrock, and nothing passes through it. A cell with value 2 is a sand inlet, and sand flows out of it.

Sand starts from a cell with an inlet and flows into bare ground that touches it up, down, left, or right. A cell the sand has entered spreads it again up, down, left, and right, so the sand reaches any place linked to an inlet through bare ground alone, however far away it is. Sand cannot flow into a cell with bedrock. A cell the sand has entered can no longer be dug.

Before the digging starts, the site manager can install concrete blocks. A block can be placed only on a bare ground cell, and a cell with a block becomes the same as bedrock, so sand cannot pass through it. A cell with a block cannot be dug either. Exactly three blocks have to be placed, no more and no fewer.

Suppose the three blocks are placed and then the sand flows until it has nowhere left to go. The number of bare ground cells the sand did not enter is called the number of diggable cells. Write a program that computes the largest number of diggable cells that the best placement of the blocks can leave.

For example, suppose the site with N=3 and M=4 is the one in the table below. Each position is written as (row, column), and row and column numbers start at 1.

2021
0000
0010

There are eight bare ground cells, two bedrock cells, and two sand inlets. Suppose blocks are placed on the three cells (1, 2), (2, 1), and (2, 3). Writing the cells with blocks as 1 gives the site below.

2121
1010
0010

Up, down, left, and right of the inlet at (1, 1) there are two blocks and the outside of the grid, so the sand has nowhere to go. The inlet at (1, 3) is the same, with two blocks and the bedrock that was already there around it. The sand therefore cannot flow into any cell.

The bare ground left is (2, 2), (2, 4), (3, 1), (3, 2), and (3, 4), five cells in all, and every one of them can be dug. No other placement of the blocks leaves more than five cells. The answer for this example is therefore 5.

Input and Output Examples

Example 1

Input

3 4
2 0 2 1
0 0 0 0
0 0 1 0

Output

5

Example 2

Input

3 3
0 2 1
2 0 2
0 0 0

Output

1

Example 3

Input

4 4
1 2 0 0
0 0 0 1
0 1 0 0
1 0 0 0

Output

8

Input

  • First the number of rows N and the number of columns M of the site are given on one line, separated by a space. N and M are each a natural number from 3 to 8.

  • Then N lines follow, giving the rows of the site. Each line holds M numbers separated by spaces, and each number is one of 0, 1, and 2. 0 means bare ground, 1 means bedrock, and 2 means a sand inlet.

  • There are always 3 or more bare ground cells, and the number of sand inlets is from 1 to 3.

Output

  • Print the number of diggable cells for the best placement of the blocks as a single number on one line. The three cells with blocks do not count as diggable cells, so this value can never be larger than the number of bare ground cells given at the start minus 3.

Requirements

  • Place blocks only on bare ground cells, and place exactly three of them
  • Sand starts from an inlet and spreads along the bare ground touching it up, down, left, or right until it has nowhere left to go
  • Bedrock and blocks stop the sand, and neither kind of cell counts as a diggable cell
  • Print only the single value for the case where the number of diggable cells is largest
  • 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 2 1
2 0 2
0 0 0
1
3 4
2 0 2 1
0 0 0 0
0 0 1 0
5
4 4
1 2 0 0
0 0 0 1
0 1 0 0
1 0 0 0
8

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 399ms

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.

1bampal24.10s1037 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

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