#670. Pac-Takahashi
Pac-Takahashi
戳这里,享受全体数据提交服务
Description
We have a grid with rows and columns. Let denote the square at the -th row from the top and -th column from the left. Each square in the grid is one of the following: the start square, the goal square, an empty square, a wall square, and a candy square. is represented by a character , and is the start square if = G
, the goal square if G
, an empty square if .
, a wall square if #
, and a candy square if o
. Here, it is guaranteed that there are exactly one start, exactly one goal, and at most candy squares .
Takahashi is now at the start square. He can repeat moving to a vertically or horizontally adjacent non-wall square. He wants to reach the goal square in at most moves. Determine whether it is possible. If it is possible, find the maximum number of candy squares he can visit on the way to the goal square, where he must finish. Each candy square counts only once, even if it is visited multiple times.
Constraints
- , , and are integers.
- is one of
S
,G
,.
,#
, ando
. - Exactly one pair satisfies
S
. - Exactly one pair satisfies
G
. - At most pairs satisfy
o
.
Input
H W T
a[1][1] a[1][2] a[1][3] ... a[1][W]
a[2][2] a[2][2] a[2][3] ... a[2][W]
a[3][1] a[3][2] a[3][3] ... a[3][W]
.
.
.
a[H][1] a[H][2] a[H][3] ... a[H][W]
Output
If it is impossible to reach the goal square in at most moves, print -1
. Otherwise, print the maximum number of candy squares that can be visited on the way to the goal square, where Takahashi must finish.
3 3 5
S.G
o#o
.#.
1
3 3 1
S.G
.#o
o#.
-1
5 10 2000000
S.o..ooo..
..o..o.o..
..o..ooo..
..o..o.o..
..o..ooo.G
18