MAY99_1 - Tom and Jerry

no tags 

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.


hide comments
Verma Rahul Omprakash: 2015-01-20 11:21:19

If a shortest path planned by Jerry is interceptable by Tom, then will he take a longer one which is not interceptable?

Shashwat: 2013-05-18 11:27:44

Please provide me more test cases

vishal chaudhary: 2013-05-08 17:42:50

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
(Tjandra Satria Gunawan)(曾毅昆): 2013-01-28 15:33:12

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
mehmetin: 2013-01-21 18:41:12

What is the output for:
3 3
##J
##.
##T

@Mehmet Its T 2

Last edit: 2013-01-22 10:34:17
Mayank Tuteja: 2013-01-21 08:46:03

@Tjandra Your code got WA after rejudging solutions

(Tjandra Satria Gunawan)(曾毅昆): 2013-01-19 07:53:00

@||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)...

||zone: 2013-01-19 07:33:59

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.

Mayank Tuteja: 2013-01-18 20:46:27

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

Jose Sanchez: 2013-01-18 19:42:53

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

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