Rotating defense
Statement
There is one hero character. The hero faces up at the start. Then, each second from second 1 to second N, enemies appear at the up, down, left and right positions. Each second the hero can keep its direction (stay), turn 90 degrees to the left (left turn), or turn 90 degrees to the right (right turn). The hero's position is fixed and cannot be moved.
Each second, ① the hero first decides which way to turn, and then ② the enemies at the up, down, left and right positions come one cell closer and attack.
When the hero is attacked, points are deducted according to the attack power of the enemy. An enemy in front of the hero deducts its attack power multiplied by 1, an enemy to the side multiplied by 2, and an enemy behind multiplied by 3.
As said above, one enemy appears at each of the up, down, left and right positions every second over the N seconds. The input is therefore given as a 2D array of size 4×N, and this array holds the attack power of each enemy that appears over the N seconds. Taking the hero's position as the center, the first row means up, the second row right, the third row down, and the fourth row left.
Write a program that makes the hero act well over the N seconds so that it takes the least damage, meaning the fewest points are deducted. For example, suppose N=4 and the attack power of the enemies is given as follows.
| Time | 1s | 2s | 3s | 4s |
|---|---|---|---|---|
| Up | 2 | 2 | 3 | 7 |
| Right | 1 | 3 | 8 | 6 |
| Down | 3 | 5 | 6 | 5 |
| Left | 4 | 4 | 9 | 3 |
If the hero acts in the order (1) stay, (2) left turn, (3) right turn, (4) right turn, a total of (21 + 27 + 55 + 39) = 142 points is deducted.
| Time | 1s | 2s | 3s | 4s |
|---|---|---|---|---|
| Action | Stay | Left turn | Right turn | Right turn |
| Facing | Up | Left | Up | Right |
| Points deducted | 21 | 27 | 55 | 39 |
But if the hero acts in the order (1) left turn, (2) left turn, (3) stay, (4) left turn, a total of (17 + 25 + 49 + 39) = 130 points is deducted. This is the total deducted when the hero acts in the best way, and it is the minimum.
| Time | 1s | 2s | 3s | 4s |
|---|---|---|---|---|
| Action | Left turn | Left turn | Stay | Left turn |
| Facing | Left | Down | Down | Right |
| Points deducted | 17 | 25 | 49 | 39 |
Write a program that returns the smallest total of deducted points the hero can reach by acting in the best way over the N seconds.
Input and Output Examples
Example 1
Input
4, [[2, 2, 3, 7], [1, 3, 8, 6], [3, 5, 6, 5], [4, 4, 9, 3]]
Output
130
Example 2
Input
4, [[1, 2, 3, 4], [6, 2, 5, 3], [4, 7, 1, 2], [8, 6, 3, 2]]
Output
107
Input
First comes N, the number of seconds over which the enemies appear, given as a natural number. N is a natural number between 2 and 9.
Next comes enemies, a 2D array of size 4×N, and this array holds the attack power of each enemy that appears over the N seconds. Every attack power is a natural number between 1 and 100.
Output
Return, as a natural number, the smallest total of deducted points the hero can reach by acting in the best way over the N seconds.
Requirements
- You must write a solution(N, enemies) 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 15 cases, and 5 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.
Examples
5 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
4, [[2, 2, 3, 7], [1, 3, 8, 6], [3, 5, 6, 5], [4, 4, 9, 3]]
130
4, [[1, 2, 3, 4], [6, 2, 5, 3], [4, 7, 1, 2], [8, 6, 3, 2]]
107
8, [[64, 10, 61, 28, 28, 98, 37, 60], [33, 15, 47, 79, 49, 44, 80, 10], [68, 12, 46, 6, 4, 18, 67, 67], [74, 67, 83, 90, 85, 53, 74, 15]]
2864
7, [[4, 97, 70, 59, 72, 32, 27], [56, 49, 50, 37, 84, 85, 30], [73, 28, 40, 83, 72, 34, 43], [35, 42, 75, 48, 5, 71, 1]]
2543
2, [[42, 44], [91, 42], [67, 36], [33, 41]]
726
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 6 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.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 5 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.
Input read time 4ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.