Log inSign up
Problems / A009

Hitting the targets

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

Statement

There are K targets on an N×N board.

A position with a target on the board is marked with one of the characters "U", "R", "D", "L". In that order these mean the target faces up, right, down and left. A position with nothing on it is marked "B", and a part blocked by a wall is marked "X".

When you shoot, you may shoot from any of the 4N positions along the border. For example, consider the case N=5. Here is one example written out as a table. It has 20 positions you can shoot from in all.

XXBXX
BBBBB
XLUXX
BUBXB
XLXBX

The shooter wants to take 3 shots in order. Taking all 3 from the same position is allowed. When a shot is fired, the bullet flies one cell at a time from the position it was fired until it reaches the border exactly on the opposite side, and it disappears on the spot when it meets a wall "X" or a target. When it does hit a target, it is handled by the following rules.

1) Hitting the front of a target: you gain 3 points, and the target disappears and turns into empty space "B". 2) Hitting the side of a target: you gain 2 points, and the target turns 90 degrees clockwise. 3) Hitting the back of a target: you gain 1 point, and the target stays as it is without changing.

Write a program that computes the maximum score you can get when exactly three bullets are fired. In the current example there are 4N=20 positions to shoot from. The positions a bullet can start from are shown with shading.

(0)(1)(2)(3)(4)
(19)XXBXX(5)
(18)BBBBB(6)
(17)XLUXX(7)
(16)BUBXB(8)
(15)XLXBX(9)
(14)(13)(12)(11)(10)

In the current example, hitting targets in the order (2) - (13) - (16) gets you 7 points (3+2+2 points).

Input and Output Examples

Example 1

Input

5, 3, [["X", "X", "B", "X", "X"], ["B", "B", "B", "B", "B"], ["X", "L", "U", "X", "X"], ["B", "U", "B", "X", "B"], ["X", "L", "X", "B", "X"]]

Output

7

Example 2

Input

5, 3, [["X", "X", "X", "X", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "U", "B", "X"], ["X", "X", "X", "X", "X"]]

Output

0

Example 3

Input

5, 3, [["X", "X", "B", "X", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "L", "B", "X"], ["X", "X", "X", "X", "X"]]

Output

8

Example 4

Input

5, 3, [["U", "B", "B", "B", "B"], ["B", "B", "X", "X", "X"], ["B", "B", "X", "U", "X"], ["B", "B", "X", "U", "X"], ["B", "B", "X", "X", "X"]]

Output

7

Input

  • First the board size N is given as a natural number. N is a natural number from 2 to 8.

  • Next the number of targets K is given as a natural number. K is an integer of 0 or more.

  • After that an array board is given, holding the information of the N×N board. A position with a target on the board is marked with one of the characters "U", "R", "D", "L". In that order these mean the target faces up, right, down and left. A position with nothing on it is marked "B", and a part blocked by a wall is marked "X".

Output

  • Return, as a natural number, the maximum score you can get when three bullets are fired.

Requirements

  • You must write a solution(N, K, 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, 3, [["X", "X", "B", "X", "X"], ["B", "B", "B", "B", "B"], ["X", "L", "U", "X", "X"], ["B", "U", "B", "X", "B"], ["X", "L", "X", "B", "X"]]
7
5, 3, [["X", "X", "X", "X", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "U", "B", "X"], ["X", "X", "X", "X", "X"]]
0
5, 3, [["X", "X", "B", "X", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "U", "B", "X"], ["X", "B", "L", "B", "X"], ["X", "X", "X", "X", "X"]]
8
5, 3, [["U", "B", "B", "B", "B"], ["B", "B", "X", "X", "X"], ["B", "B", "X", "U", "X"], ["B", "B", "X", "U", "X"], ["B", "B", "X", "X", "X"]]
7
5, 2, [["B", "B", "B", "B", "X"], ["B", "X", "B", "B", "X"], ["B", "B", "B", "B", "B"], ["X", "X", "B", "B", "R"], ["U", "B", "B", "B", "B"]]
8
7, 2, [["B", "X", "R", "B", "B", "B", "B"], ["X", "B", "B", "X", "B", "B", "B"], ["B", "B", "B", "X", "B", "X", "B"], ["B", "B", "X", "B", "B", "B", "X"], ["D", "X", "B", "B", "B", "B", "B"], ["X", "B", "X", "B", "B", "B", "B"], ["B", "B", "X", "B", "B", "B", "X"]]
8
4, 5, [["U", "B", "D", "B"], ["X", "U", "R", "X"], ["X", "D", "X", "X"], ["B", "B", "B", "B"]]
9

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 3 in the original.

The body says exactly three bullets are fired, yet the number of targets K comes in with the input as well. The reference solution in the original does not use K, and the test cases include ones where K is 2.

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

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.

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

Input read time 5ms (excluded from the ranking)

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