ABCPATH - ABC Path

no tags 

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

hide comments
aman440: 2022-02-08 19:37:49

AC in one go :)

darksparrow123: 2022-01-17 13:45:16

zoom AC in one go :)

md__talha__007: 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.

mahbubkuet08: 2021-11-28 19:23:46

At first got TLE. Then used memoization. AC

kunal0403: 2021-11-24 12:12:10

Got 8 WA's because while calculating max I was iterating n rows and n columns :(

ankitjosh: 2021-11-22 21:46:26

AC after 4 WA, my fault was , i wasn't checking previously visited condition.

akshitforever: 2021-09-26 13:15:33

You have to start with A only and find max for that path only.

sravant_1727: 2021-05-01 14:06:24

What is the input format? I'm confused

caovanduy15: 2021-04-01 15:58:55

memorized recursive. AC. :3

wiseman_123: 2021-03-07 07:32:05

Be aware of the garbage values. Cost me 3WA... Sad life AF...


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