This page is still under construction.

Parts of this page are still being built. What you see may change.

Gahui's Sweet Potato Eating

Interview

Time limit2sMemory limit512 MB

Summary
Given a grid with obstacles, a start cell, and sweet potatoes, find the most potatoes Gahui can eat while walking or waiting for exactly T seconds, where T is at most 10.
Level

Medium7 of 10

Topics
BFS, Dynamic programming, Bit manipulation, Graph
Solved
No attempts yet

Problem

Gahui really likes sweet potatoes.

This time too, the smell of sweet potatoes is unmistakable, yet no sweet potato is in sight. Her older brother has hidden sweet potatoes in the room.

Her brother proposes a game to Gahui and explains the rules. The rules are as follows.

  • Every second, Gahui may move one cell in one of the four directions (up, down, left, right), or stay in place without moving.
  • If Gahui moves to a cell containing a sweet potato, she eats it. Eating a sweet potato is assumed to take no time.
  • Once Gahui eats a sweet potato, it does not reappear in that cell.

Starting from her current position, Gahui wants to eat as many sweet potatoes as possible over T seconds. Tell her the maximum number of sweet potatoes she can eat.

Input

The first line gives the map's height R, width C, and the time T Gahui moves.

From the second line to the R+1-th line, a string of length C is given.

Each character in the given strings is one of 'G' for Gahui, 'S' for a sweet potato, '.' for an empty cell, or '#' for an obstacle.

Output

Print the answer to the problem.

Constraints

  • 2 ≤ R ≤ 100
  • 2 ≤ C ≤ 100
  • 1 ≤ T ≤ 10
  • The character 'G', representing Gahui, appears exactly once in the map. The position of 'G' is Gahui's current position.
  • Each position with 'S' contains one sweet potato.
  • There is at least one sweet potato and at least one obstacle.
  • Gahui cannot jump over or pass through obstacles.
  • Gahui cannot leave the map.

Examples2

  1. Example 1

    Input
    11 11 5
    ........G..
    ......S.#S.
    ........#.S
    ...........
    ...........
    .##########
    .##########
    ...........
    ...........
    ##########.
    ...........
    
    Expected output
    2
    
  2. Example 2

    Input
    11 11 5
    G....S.....
    ...........
    ...........
    ...........
    ...........
    ...........
    .....#.....
    ...........
    ...........
    ...........
    ...........
    
    Expected output
    1