Log inSign up
Problems / A012

Wolves and chickens

300 ptsCases 20Set by dongbinna
Added October 30.

Statement

An N×N map holds K wolves and K chickens. At the start each cell is empty, holds a wall, holds one wolf, or holds one chicken. Each wolf is assigned one chicken to catch.

Each chicken stays still at the position it was given at the start. Each wolf can move up, down, left, or right once per second. A wolf cannot move into a cell that holds a wall, and while moving it may pass through a cell with a wolf or a chicken. For example, several wolves may be on the same cell while they are moving.

When a wolf reaches the position of the chicken assigned to it, the chicken disappears and the wolf takes that position. Write a program that assigns one chicken to each wolf so that the time until every wolf has caught its assigned chicken is as small as possible. If even one wolf can never catch the chicken assigned to it, assume the time taken is "infinite".

For example, suppose N=5 and K=3. A position with a wolf is marked "W", and a position with a chicken is marked "C". A position with a wall is marked "X", and an empty space is marked "B".

BWBCB
XXXXX
BCCBB
BBXBB
WBXWB

Suppose each position is written as (row, column). With row and column indices starting at 0, the best choice in this example is for the wolf at (0, 1) to catch the chicken at (0, 3), the wolf at (4, 0) to catch the chicken at (2, 1), and the wolf at (4, 3) to catch the chicken at (2, 2). So in this example the smallest time until every wolf has caught its assigned chicken is 3 seconds.

Input and Output Examples

Example 1

Input

5, 3, [["B", "W", "B", "C", "B"], ["X", "X", "X", "X", "X"], ["B", "C", "C", "B", "B"], ["B", "B", "X", "B", "B"], ["W", "B", "X", "W", "B"]]

Output

3

Example 2

Input

5, 3, [["C", "X", "B", "W", "B"], ["X", "X", "X", "X", "B"], ["B", "C", "C", "B", "B"], ["B", "B", "X", "B", "B"], ["W", "B", "X", "W", "B"]]

Output

0

Example 3

Input

5, 4, [["C", "C", "C", "C", "B"], ["X", "X", "X", "X", "B"], ["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["B", "W", "W", "W", "W"]]

Output

8

Example 4

Input

5, 6, [["C", "C", "C", "C", "B"], ["X", "B", "X", "X", "W"], ["B", "B", "B", "W", "B"], ["B", "C", "B", "B", "C"], ["B", "W", "W", "W", "W"]]

Output

5

Input

  • First the size N of the whole map is given. N is a natural number between 5 and 200.

  • Next the number of wolves and chickens K is given. K is a natural number between 1 and 9.

  • Next a two-dimensional array arr holding the whole N×N map is given. A position with a wolf is marked "W", and a position with a chicken is marked "C". A position with a wall is marked "X", and an empty space is marked "B".

Output

  • Return the time taken when one chicken is assigned to each wolf so that the time until every wolf has caught its assigned chicken is as small as possible. If a chicken cannot be caught no matter how the wolves are assigned (that is, the smallest time until every wolf has caught its assigned chicken is "infinite"), return 0.

Requirements

  • You must write a solution(N, K, arr) function and return the answer
  • 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 20 cases, and 7 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 20 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

7 public cases. Judging also uses hidden cases. You have to pass all 20 cases to solve this.

Input
Expected output
5, 3, [["B", "W", "B", "C", "B"], ["X", "X", "X", "X", "X"], ["B", "C", "C", "B", "B"], ["B", "B", "X", "B", "B"], ["W", "B", "X", "W", "B"]]
3
5, 3, [["C", "X", "B", "W", "B"], ["X", "X", "X", "X", "B"], ["B", "C", "C", "B", "B"], ["B", "B", "X", "B", "B"], ["W", "B", "X", "W", "B"]]
0
5, 4, [["C", "C", "C", "C", "B"], ["X", "X", "X", "X", "B"], ["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["B", "W", "W", "W", "W"]]
8
5, 6, [["C", "C", "C", "C", "B"], ["X", "B", "X", "X", "W"], ["B", "B", "B", "W", "B"], ["B", "C", "B", "B", "C"], ["B", "W", "W", "W", "W"]]
5
75, 9, [["B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "X", "X", "B", "X", "X", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "X", "X", "B", "X", "B", "B", "X", "X", "X", "X", "B"], ["B", "X", "B", "X", "X", "B", "B", "X", "X", "B", "B", "B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "X", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "X", "B", "X", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X", "B", "X", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "B", "B", "X", "X", "B", "X", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", 

... cut off here (28,282 chars in total)
40
30, 7, [["X", "B", "B", "B", "X", "X", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "X", "B", "B", "B", "B", "B", "B"], ["B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "C", "B", "B", "B", "B", "X", "B", "B", "X", "B", "X"], ["B", "X", "B", "B", "B", "X", "B", "X", "B", "B", "B", "X", "X", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B"], ["B", "X", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B"], ["X", "B", "X", "B", "B", "X", "B", "B", "X", "B", "X", "X", "B", "B", "X", "B", "B", "X", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "X"], ["B", "B", "B", "B", "W", "X", "X", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "C", "B", "B", "B", "B", "B", "B", "B", "B

... cut off here (4,567 chars in total)
19
93, 4, [["B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "X", "X", "B", "B", "X", "B", "X", "X", "X", "B", "B", "X", "B", "X", "B", "X", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X"], ["X", "B", "B", "B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "X", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "X", "B", "X", "B", "X", "B", "X", "B", "B", "X", "B", "B", "B", "B", "B", "X", "X", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "X", "B", "B", "B", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "X

... cut off here (43,438 chars in total)
90

Notes

Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.

This is problem 3 of practice test 4 in the original.

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

Submitting requires you to log in. Running against the public cases works without one.

You can test with 7 public cases. Submitting runs the hidden cases too.

People who solved this problem

Nobody yet. You could be the first.