Log inSign up
Problems / B070

Cutting down mail collection boxes

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

Statement

A village is a grid divided into N rows and N columns. Each square is empty land, or a house, or a mail collection box. Row numbers are counted from the top and column numbers from the left, starting at 1. The distance between the square in row r column c and the square in row R column C is worked out as the difference between the two row numbers plus the difference between the two column numbers.

The people of the village put their mail in the collection box closest to their house. So the box distance of a house is the smallest of the distances from that house to the collection boxes left in the village. And the box distance of the village is the sum of the box distances of every house in the village.

To cut its running costs the post office wants to keep only M of the collection boxes in the village and clear away all the rest. The box distance of the village changes with which boxes are kept. Write a program that, given the village and the number of collection boxes M to keep, works out the smallest box distance of the village over every way of keeping M boxes.

For example, suppose a village with N=5 and M=3 is as in the table below. In the table 0 is empty land, 1 is a house and 2 is a mail collection box.

SquareColumn 1Column 2Column 3Column 4Column 5
Row 100100
Row 200201
Row 301200
Row 400100
Row 500002

In this example there are only three collection boxes, in row 2 column 3, row 3 column 3 and row 5 column 5, so all three have to be kept. The box distances of the four houses are then as follows.

Position of the houseRow 1 column 3Row 2 column 5Row 3 column 2Row 4 column 3
Closest collection boxRow 2 column 3Row 2 column 3Row 3 column 3Row 3 column 3
Box distance1211

So the box distance of the village is 1 + 2 + 1 + 1 = 5, and the answer in this example is 5.

Input and Output Examples

Example 1

Input

5 3
0 0 1 0 0
0 0 2 0 1
0 1 2 0 0
0 0 1 0 0
0 0 0 0 2

Output

5

Example 2

Input

5 2
2 2 2 2 0
0 0 2 2 0
2 0 0 2 0
1 0 2 0 0
1 2 0 0 0

Output

2

Example 3

Input

2 1
1 2
2 1

Output

2

Input

  • First the number of squares N along one side of the village and the number of collection boxes M to keep are given on the first line, separated by a space. N is a natural number from 2 to 50, and M is a natural number from 1 to 13.

  • Next the information on each row of the village is given over N lines, from the top row downward. Each line holds N numbers separated by spaces, where 0 means empty land, 1 means a house and 2 means a mail collection box.

  • The village has at least 1 and at most 100 houses, and at least 1 and at most 13 collection boxes. Also M is not greater than the number of collection boxes in the village.

Output

  • Print the smallest box distance of the village over every way of keeping M collection boxes on the first line. The value printed is a sum of distances, so it is an integer of 0 or more.

Requirements

  • The distance between two squares is the difference between the row numbers plus the difference between the column numbers
  • The box distance of a house is the smallest of the distances to the collection boxes that are left
  • The box distance of the village is the sum of the box distances of every house
  • Exactly M collection boxes are kept
  • Houses are never cleared away, and every house in the village goes into the calculation
  • 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
2 1
1 2
2 1
2
5 2
2 2 2 2 0
0 0 2 2 0
2 0 0 2 0
1 0 2 0 0
1 2 0 0 0
2
5 3
0 0 1 0 0
0 0 2 0 1
0 1 2 0 0
0 0 1 0 0
0 0 0 0 2
5

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

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.

1bampal2145ms652 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

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