close

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
Image pikachuuuuu073: 2025-11-02 08:19:43

Guys dont forget to properly write according to output format! also dont forget a new line, i kust wasted 40 mins because i hadnt used a \n

Image omkar_gaddi: 2025-10-17 04:04:46

Multi-source BFS + Vis matrix == AC

Image clashcoder_5: 2025-06-17 09:26:15

time exceeded, compilation error, every time i use a different approach some new error pops up....

Image lamthientai: 2025-05-13 05:12:37

dfs + backtracking + memo => AC

Image alexocode: 2025-04-04 16:11:16

don't forget to increase testcount

Image krishnakrypton: 2024-08-02 22:55:30

keep grinding and still you wont get it....

Image ahsanul_haque: 2024-03-24 12:35:26

Learning dfs , A/C after few try, Peace ..

Image yash_panasara: 2024-03-06 14:55:24

<snip>
it giving wrong answer
please help
[Simes]: read the footer, don't post code here. Use the forum.

Last edit: 2024-03-06 20:35:01
Image shah_vivek2004: 2023-06-23 14:33:17

so basically, here input format is like you have to write your code in infinite loop and when two inputs 0 0 appear,
then you have to break that loop.

=>spoilers: <snip>
[Simes]: Let people experience the joy of figuring it out for themselves.

Last edit: 2023-06-25 19:49:57
Image pranshu820: 2023-03-01 09:36:21

Wrong answer on submission, but correct on my local system.
Please check out.
<snip>
[Simes]: Read the footer - Don't post source code here, use the forum.
Okay

Last edit: 2023-03-01 10:24:33

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