Alphabet Path
Time limit2sMemory limit256 MB
Find the longest path from the top-left cell of a grid moving to adjacent cells while never revisiting a letter already used, maximizing visited cells.
- Level
Medium5 of 10
- Topics
- Backtracking, DFS, Brute force
- Solved
- No attempts yet
Problem
A board has rows and columns. Each cell contains one uppercase English letter. A piece starts in the upper-left cell, row and column .
The piece may move to one vertically or horizontally adjacent cell. It may enter a cell only if that cell's letter is different from every letter on the cells visited so far. In other words, the path cannot contain the same letter twice.
Find the maximum number of cells the piece can visit, including the starting cell.
Input
The first line contains and , separated by a space. ()
Each of the next lines contains uppercase English letters with no spaces, describing the board.
Output
Print the maximum number of cells the piece can visit.