MAY99_1 - Tom and Jerry

Tom and Jerry is a favourite cartoon of many of us. One day Manku was sitting watching an episode of Tom and Jerry where he found that Tom and Jerry both entered a rectangular maze and Tom was after Jerry, but Jerry being the hero, returned safely.

Manku then start making different scenarios and wondering that if Jerry moves optimally and Tom knows entire path that Jerry is expected to take, then will Jerry be able to escape out of the maze or not?

Moreover Manku added 1 rule to this that if Jerry moves from position A to position B, then at any cost he is not allowed to return from position B to position A.

Jerry, if is at the escape positions of the maze, in the beginning, then he can't exit from that same position.

Moreover he can't escape if he is caught by Tom at any position.

Jerry and Tom can move up, down, left, right or wait at their current position.

If it is guaranteed that either Jerry would escape or Tom would catch him.

All characters at row = 0 or row = m-1 or column = 0 or column = n-1, which are '.' or 'J' or 'T' are escape positions.

Input

Each input file consist of only 1 test case.

First line of input contains 2 numbers m and n, both integers are less than or equal to 100, the size of the rectangular maze.

Then m lines follows containing n characters each:

  • . means an open space so that Tom or Jerry can move there.
  • # means a closed place.
  • T means starting position of Tom.
  • J means starting position of Jerry.

Output

Output single line containing a character W and integer D, where W is 'J' if Jerry can escape or else 'T' and D is the minimum time taken by Jerry to escape (if W is 'J') or maximum time for which Jerry is alive (if W is 'T')

Example

Input 1:
4 4
#..J
#...
#.T.
####

Output 1:
J 1
Input 2:
6 3
###
#J#
#.T
###
###
#.#

Output 2:
T 2
Input 3:
7 7
......#
.......
.......
...J...
...T...
.......
#......

Output 3:
J 3
Input 4:
7 7
#.....#
#......
#......
J......
#..T...
#......
#......

Output 4:
J 4

Explanation

In the first test case Jerry will move 1 step to his left and would escape.

In the second test case Jerry can't escape so he will remain at his position and will be caught after 2 moves.

In the third test case Jerry will move 3 steps to his left and will escape.

In the fourth test case Jerry will move 1 step to his right and then 3 steps up to escape.


Added by:Mayank Tuteja
Date:2013-01-05
Time limit:0.100s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:self problem

hide comments
2015-01-20 11:21:19 Verma Rahul Omprakash
If a shortest path planned by Jerry is interceptable by Tom, then will he take a longer one which is not interceptable?
2013-05-18 11:27:44 Shashwat
Please provide me more test cases
2013-05-08 17:42:50 vishal chaudhary
is jerry always making the first move? also..is jerry allowed to step on tom's already visited path and on his own visited path.?? wat will be the output of this case..? thanks
########
# . . . . . J#
# .#### .#
# .#### .#
.T . . . . . #
########


@vishal
Jerry is not allowed to retrace its path

Last edit: 2013-05-16 16:56:57
2013-01-28 15:33:12 (Tjandra Satria Gunawan)(曾毅昆)
NZEC, bad input data detected again!
EDIT: Finally AC, but you must fix the input data, it's not easy to 'guess' the size of matrix.

Last edit: 2013-01-28 15:57:00
2013-01-21 18:41:12 mehmetin
What is the output for:
3 3
##J
##.
##T

@Mehmet Its T 2

Last edit: 2013-01-22 10:34:17
2013-01-21 08:46:03 Mayank Tuteja
@Tjandra Your code got WA after rejudging solutions
2013-01-19 07:53:00 (Tjandra Satria Gunawan)(曾毅昆)
@||zone: Yes, jerry planning shortest safe path... and Tom move is depend on jerry move... if jerry can't escape, jerry will choose the path such that tom will catch jerry as long as possible (T is maximized)...
2013-01-19 07:33:59 ||zone
Is jerry planning shortest path?
And
Is Tom planning shortest intercept to this path.
Otherwise cant tom move one step left to catch in one step only.
2013-01-18 20:46:27 Mayank Tuteja
If Jerry moves only 1 step to right ,
then since Tom knows Jerry movement plan so he will just move up and catch him in 3 steps
2013-01-18 19:42:53 Jose Sanchez
Why is it not T 5?
jerry could move to right, and after stay in that position waiting for tom.
Thanks for answering

Last edit: 2013-01-18 19:49:24
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.