Sanjay and his friends are searching for treasure in an abandoned castle. They have just entered the castle and find themselves faced with a maze of treasure-filled rooms separated by locked doors. Fortunately for them, the old owners left spare keys littered around the castle.
Sanjay finds that they can use any key to help open any door in the castle. However, some doors require multiple keys in order to open them.
The floor plan of the castle can be represented as an grid of the following characters:
.
denotes an empty space.#
denotes a wall.- Characters
1
through9
denote a door, with the number representing how many keys it takes to open the door. K
represents a key.T
represents a treasure.S
represents Sanjay's start point.
Input Specification
The input will contain test cases. Each test case starts with an integer . The next lines each contain characters denoting the floor plan of the castle.
Output Specification
For each test case, your program should output the number of treasures that Sanjay and his friends will be able to retrieve.
Sample Input
3
.SK
#1#
T..
5
S.2.T
.K##3
11##T
....#
..T.K
Sample Output
1
2
Note: Only cases are shown in this sample.
Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org
Comments
How can Sanjay and his friends move through the castle? Is it only North, East, South, and West? Or can they travel NE, NW, etc?