Log inSign up
Problems / A011

Two knights

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

Statement

An N×N board holds 1 hero and 2 knights, and at least 1 enemy. The hero's position is marked "H", a knight's position "K", and an enemy's position "E". Empty space is marked "B" and a wall is marked "X". No two of them ever share a position.

The hero cannot move. A knight may, if it wants, move from its current position to one of 8 positions, the way a chess knight does. The 8 directions a knight can move in on the N×N grid are shown in the figure below.

Suppose each position is written as (row, column). If row and column indices start at 0, then from position (3, 3) the reachable positions are (1, 2), (1, 4), (2, 1), (2, 5), (4, 1), (4, 5), (5, 2), (5, 4). They are the shaded positions in the figure below.

●●
●●
K
●●
●●

The table above is a grid of 7 rows and 7 columns with the knight (K) at (3, 3). In the original, the 8 reachable positions were marked only by shading, so no cell values survived the text extraction. The list of positions written in the body has been copied over unchanged and marked with ●.

The 2 knights may simply stay where they started, and each one may move at most once, into an empty space or onto a position holding an enemy. They cannot move onto a position holding a wall. Moving onto a position holding an enemy removes that enemy, and the knight takes the position. Once a given knight has moved once it cannot move again, and after both knights are done moving the enemies start to move. While the knights and the enemies move, no two of them can share a position.

Each enemy can move up, down, left or right from its current position every second. It cannot move onto a position holding a knight or a wall, and the whole process ends once an enemy reaches the position of the hero.

Look at an example with N=5. In this example you can leave the knight at (1, 1) where it is and move the knight at (0, 1) to (2, 2) to take out an enemy.

BKBBB
BKEEB
BBEXB
BBXXB
HBXBE

The board then changes as follows.

BBBBB
BKEEB
BBKXB
BBXXB
HBXBE

In this case the shortest time for the nearest enemy to reach the hero is 7 seconds.

The goal is to use the 2 knights well and maximize the time it takes the nearest enemy to reach the hero. Write a program that prints the shortest time for the nearest enemy to reach the hero when the 2 knights are used well for that goal. If you can make it impossible for the enemies to reach the hero, print 0. In this problem row and column indices always start at 0.

Input and Output Examples

Example 1

Input

5, [["B", "K", "B", "B", "B"], ["B", "K", "E", "E", "B"], ["B", "B", "E", "X", "B"], ["B", "B", "X", "X", "B"], ["H", "B", "X", "B", "E"]]

Output

7

Example 2

Input

5, [["E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E"], ["K", "E", "E", "E", "E"], ["H", "K", "E", "E", "E"]]

Output

0

Example 3

Input

5, [["B", "B", "B", "B", "E"], ["B", "B", "B", "B", "B"], ["K", "K", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["H", "X", "E", "B", "B"]]

Output

8

Example 4

Input

5, [["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["K", "K", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["H", "X", "E", "B", "B"]]

Output

0

Input

  • First the board size N is given. N is a natural number from 4 to 100.

  • Next, the board information is given as a 2-dimensional array board whose row and column lengths are both N. The hero's position is marked "H", a knight's position "K", and an enemy's position "E". Empty space is marked "B" and a wall is marked "X". The board always holds 1 hero and 2 knights, and at least 1 enemy.

Output

  • Return the shortest time for the nearest enemy to reach the hero when the 2 knights are used well for that goal.

Requirements

  • You must write a solution(N, board) 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, [["B", "K", "B", "B", "B"], ["B", "K", "E", "E", "B"], ["B", "B", "E", "X", "B"], ["B", "B", "X", "X", "B"], ["H", "B", "X", "B", "E"]]
7
5, [["E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E"], ["K", "E", "E", "E", "E"], ["H", "K", "E", "E", "E"]]
0
5, [["B", "B", "B", "B", "E"], ["B", "B", "B", "B", "B"], ["K", "K", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["H", "X", "E", "B", "B"]]
8
5, [["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["K", "K", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["H", "X", "E", "B", "B"]]
0
51, [["B", "B", "B", "E", "B", "E", "X", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "E", "E", "B", "B", "X", "B", "B", "E", "B", "B", "B", "X", "B", "B", "B", "B", "B", "E", "B", "B", "E", "B", "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", "B", "B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X", "B", "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", "X", "B", "X", "B", "X", "B", "X", "B", "B", "B", "E", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "X", "B", "B", "X", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B"], ["X", "B", "X", "X", "B", "B", "B", "B", "X", "B", "B", "E", "B", "B", "X", "E", "B", "B", "B", "X", "B", "X", "X", "B", "B", "B", "B", "X", "X", "B", "B", "B", "B", "

... cut off here (13,111 chars in total)
4
71, [["B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "B", "B", "X", "B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "E", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B"], ["B", "B", "B", "B", "X", "E", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "E", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "E", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "E", "B"], ["B", "B", "B", "B", "B", "X", "E", "E", "B", "E", "B", "X", "B", "E", "B", "E", "B", "X", "B", "X", "B", "X", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "X", "E", "B", "X", "B", "B", "B", "B", "B", "B", "X", "E", "B", "B", "B", "B", "E", "B", "B", "B", "B", "X", "B", "X", "B", "X", "B", "X", "B", "B", "B", "B"], ["B", "B", "B", "E", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "X", "B", "X", "B", "B", "X", "B", "B", "B", "B", "B"

... cut off here (25,351 chars in total)
5
77, [["B", "B", "B", "B", "B", "B", "X", "B", "E", "B", "B", "B", "B", "X", "B", "B", "E", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "X", "B", "X", "B", "B", "B", "X", "X", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "X", "B", "X", "B", "B", "E", "X", "B", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "X", "B"], ["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", "B", "B", "B", "B", "B", "B", "X", "X", "B", "X", "B", "X", "B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "E", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "E", "B", "B", "B", "B", "X", "E", "B", "B", "B", "X", "B", "B", "B", "X", "X", "B", "B", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "E", "B", "B", "B", "E", "B", "B", "B", "B", "X", "B", "X", "B", "B", "E", "B", "X", "B"], ["X", "B", "E", "X", "B", "B", "B"

... cut off here (29,803 chars in total)
6

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 2 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 59ms

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

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.

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

Input read time 8ms (excluded from the ranking)

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