#670. Pac-Takahashi

Pac-Takahashi

戳这里,享受全体数据提交服务

Description

We have a grid with HH rows and WW columns. Let (i,j)(i,j) denote the square at the ii-th row from the top and jj-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. (i,j)(i,j) is represented by a character ai,ja_{i,j}, and is the start square if ai,ja_{i,j} = G, the goal square if ai,j=a_{i,j}= G, an empty square if ai,j=a_{i,j}= ., a wall square if ai,j=a_{i,j}= #, and a candy square if ai,j=a_{i,j}= o. Here, it is guaranteed that there are exactly one start, exactly one goal, and at most 1818 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 TT 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

  • 1H,W3001\le H,W\le 300
  • 1T2×1061\le T \le 2\times 10^6
  • HH, WW, and TT are integers.
  • ai,ja_{i,j} is one of S, G, ., #, and o.
  • Exactly one pair (i,j)(i,j) satisfies ai,j=a_{i,j}=S.
  • Exactly one pair (i,j)(i,j) satisfies ai,j=a_{i,j}=G.
  • At most 1818 pairs (i,j)(i,j) satisfy ai,j=a_{i,j}=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 TT 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