|
| 1 | +import sys |
| 2 | +from collections import deque |
| 3 | + |
| 4 | +read = lambda: sys.stdin.readline().rstrip() |
| 5 | + |
| 6 | + |
| 7 | +class Problem: |
| 8 | + def __init__(self): |
| 9 | + self.r, self.c = map(int, read().split()) |
| 10 | + self.matrix = [list(read()) for _ in range(self.r)] |
| 11 | + |
| 12 | + def solve(self) -> None: |
| 13 | + print(self.dfs()) |
| 14 | + |
| 15 | + def dfs(self) -> int: |
| 16 | + stack, cache, max_depth = ( |
| 17 | + deque([((0, 0), 1, 1 << ord(self.matrix[0][0]) - ord("A"))]), |
| 18 | + {row: {col: set() for col in range(self.c)} for row in range(self.r)}, |
| 19 | + 1, |
| 20 | + ) |
| 21 | + cache[0][0].add(1 << ord(self.matrix[0][0]) - ord("A")) |
| 22 | + |
| 23 | + while stack: |
| 24 | + (x, y), depth, visited = stack.pop() |
| 25 | + if depth > max_depth: |
| 26 | + max_depth = depth |
| 27 | + |
| 28 | + if max_depth == min(self.r * self.c, 26): |
| 29 | + return max_depth |
| 30 | + |
| 31 | + for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]: |
| 32 | + nx, ny = x + dx, y + dy |
| 33 | + |
| 34 | + if 0 <= nx < self.c and 0 <= ny < self.r: |
| 35 | + next_token_code = ord(self.matrix[ny][nx]) - ord("A") |
| 36 | + |
| 37 | + if visited & (1 << next_token_code) == 0: |
| 38 | + mask = visited | (1 << next_token_code) |
| 39 | + |
| 40 | + if mask not in cache[ny][nx]: |
| 41 | + cache[ny][nx].add(mask) |
| 42 | + stack.append(((nx, ny), depth + 1, mask)) |
| 43 | + |
| 44 | + return max_depth |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + Problem().solve() |
0 commit comments