Treasure Hunt
Time limit1sMemory limit256 MB
Starting at (1, 1), follow the letters of S through adjacent grid tiles, and report the maximum number of full repetitions K and the final tile, or -1 if none exists.
- Level
Medium4 of 10
- Topics
- Graph, DFS, Simulation
- Solved
- No attempts yet
Problem
Geonggus found a treasure map with rows and columns. On the back of the map is a string of uppercase letters and a note: "If you follow the written string times, the treasure is buried on the tile with the last character of the string."
Geonggus lives by the saying "effort never betrays," so he believes the treasure is buried where the value of is maximal. Follow the given string and find the place where Geonggus thinks the treasure is buried. Geonggus starts at , and he can move only to the adjacent tiles above, below, left, or right.
Input
The first line contains , , and , the length of the string , separated by spaces. The top-left coordinate is , and the bottom-right coordinate is .
The second line contains the string . consists of uppercase letters, and no letter appears more than once in .
The next lines give the map. Each line consists of uppercase letters. The tile where Geonggus stands has the same letter as the first character of .
Output
If Geonggus believes a treasure location exists, print the number of times he follows the string, followed by the coordinates of the treasure.
If Geonggus decides there is no treasure, or the treasure can never be found, print only .
Print the maximum value of and the treasure location that corresponds to it. There are never two or more places where is maximal.
Constraints