#P8169. [eJOI 2021] Dungeons

[eJOI 2021] Dungeons

Problem Description

You need to play a game on an N×MN \times M map. Each cell on the map is one of the following types:

  • .: empty ground.
  • #: wall.
  • o: coin.
  • X: bomb.
  • S: starting position.

Each map contains one or more S. When the game starts, your starting point will be chosen as any one of the S. Since the map is very dark, you can only see the cells within a 3×33 \times 3 square centered at yourself. Also, X and S will be treated as empty ground (.) in your view. Note that you cannot enter a wall (#).

In each step, you may move one cell in one of four directions: up, down, left, or right. When you enter a cell with o, you gain 11 coin, and the o in that cell disappears. When you enter a cell with X, you lose all your coins and the game ends.

Before the game starts, you already have the full map (although you do not know which S you will start from). If you play with an optimal strategy, find the number of coins you can guarantee to obtain in the worst case.

Input Format

The first line contains two positive integers N,MN, M.

The next NN lines describe the map. The testdata guarantees that the border of the map (i.e., the first row, last row, first column, and last column) consists entirely of the # character.

Output Format

Output one integer, the number of coins you can guarantee to obtain in the worst case.

3 7
#######
#Soooo#
#######
4
3 8
########
#SoXooS#
########
1
7 18
##################
#................#
#.o...SX.......o.#
#.o...X..X.....o.#
#.o.....XS.....o.#
#................#
##################
0
7 18
##################
#....#...........#
#.o...SX.......o.#
#.o...X..X.....o.#
#.o.....XS.....o.#
#.........#......#
##################
6
7 18
##################
#......X..S....oo#
##################
#..o..S.X......o.#
##########X#######
#o.....S...X.....#
##################
1

Hint

Explanation for Sample 1

Since there is only one S, the starting position is fixed, and you can obtain all coins.

Explanation for Sample 2

The initial view when starting from the left and right S respectively (@ is the starting point):

$$\texttt{\#\#\#\,\,\,\,\,\,\,\,\#\#\#} \\ \texttt{\#@o\,\,\,\,\,\,\,\,o@\#} \\ \texttt{\#\#\#\,\,\,\,\,\,\,\,\#\#\#}$$

Starting from the left S, you can obtain 11 coin; starting from the right one, you can obtain 22 coins. Therefore, in the worst case you can obtain at most min(1,2)=1\min(1,2)=1 coin.

Explanation for Sample 3

No matter which S you start from, the initial view is:

....@....\texttt{...} \\ \texttt{.@.} \\ \texttt{...}

Since you do not know where exactly you are on the map, the worst case is stepping onto the X next to the starting point (which appears as . in the view). In this case you cannot obtain any coins, so the answer is 00.

Explanation for Sample 4

The initial view when starting from the top-left and bottom-right S respectively:

$$\texttt{\#..\,\,\,\,\,\,\,\,...} \\ \texttt{.@.\,\,\,\,\,\,\,\,.@.} \\ \texttt{...\,\,\,\,\,\,\,\,..\#}$$

Since the initial views are different, you can determine your exact position on the map by the differences in the # cells. Therefore, you can obtain all coins, and the answer is 66.

Explanation for Sample 5

First, you may move left for 22 steps. If you can see o, then you can determine that you are on the fourth row, and also pick up the coin on the left.

Otherwise, you still cannot determine whether you are on the second row or the sixth row. Then, based on moving left 22 steps, you can move right 44 steps (that is, to the cell 22 steps to the right of the starting point). If the upper-right cell in your view is . (which is X on the map), then you can determine that you are on the sixth row. In this case you can safely keep moving left to collect the coin(s) in the second column. If the upper-right cell in your view is not ., then you only need to keep moving right to collect the coin(s) in the 16th and 17th columns.

The answer obtained by this plan is optimal, which is min{1,1,2}=1\min\{1,1,2\}=1.

It is not hard to see that if you move right directly, you might enter X immediately. This plan would give an answer of 00.

Constraints

This problem uses bundled tests.

Let the number of S on the map be SS.

  • Subtask 1 (3 pts): S=1S=1, there is no X, and only the border of the map has #.
  • Subtask 2 (7 pts): N=3N=3.
  • Subtask 3 (12 pts): S=1S=1.
  • Subtask 4 (23 pts): S=2S=2.
  • Subtask 5 (41 pts): 1N,M2501 \le N,M \le 250, 1S121 \le S \le 12.
  • Subtask 6 (14 pts): no special constraints.

For all testdata, 1N,M4001 \le N,M \le 400, 1S601 \le S \le 60.

Notes

This problem is translated from eJOI2021 Day 2 B Dungeons.

Translated by ChatGPT 5