ABCPATH - ABC Path

You will be given a 2-dimensional grid of letters. Find the length of the longest path of consecutive letters, starting at 'A'. Paths can step from one letter in the grid to any adjacent letter (horizontally, vertically, or diagonally).

For example, in the following grid, there are several paths from 'A' to 'D', but none from 'A' to 'E':

ABC

One such path is:

path

Input

Each test case will start with a line contains two integers H, W the height and width of the grid respectively 1 <= H, W <= 50. Then H lines follow each of W uppercase letters only. Input terminates with H = 0 and W = 0.

Output

For each test case print “Case C: X” without quotes where C is the case number starting with 1 and X is the solution.

Example

Sample Input:
4 3
ABE
CFG
BDH
ABC
0 0

Sample Output:
Case 1: 4

Added by:Ali Arous
Date:2011-11-05
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:FCIS Local Contest 2012

hide comments
2022-02-26 10:28:28
<p>AC in one shot!!!!!</p>
2022-02-08 19:37:49
AC in one go :)

2022-01-17 13:45:16
zoom AC in one go :)

2022-01-16 09:27:19
Do check case in which 'A' is not present. I got 2 WA's due to ths case and wasted my 30 min in this.
2021-11-28 19:23:46
At first got TLE. Then used memoization. AC
2021-11-24 12:12:10
Got 8 WA's because while calculating max I was iterating n rows and n columns :(
2021-11-22 21:46:26
AC after 4 WA, my fault was , i wasn't checking previously visited condition.
2021-09-26 13:15:33
You have to start with A only and find max for that path only.
2021-05-01 14:06:24
What is the input format? I'm confused
2021-04-01 15:58:55
memorized recursive. AC. :3
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.