1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
import math
def (x, y, target, graph, visit, X, Y, M, N): if x == X and y == Y: return -math.inf if visit[x][y]: return 0 visit[x][y] = True cnt = 0 if graph[x + 1][y] == target: cnt += dfs(x + 1, y, target, graph, visit, X, Y, M, N) if graph[x][(y + 1) % N] == target: cnt += dfs(x, (y + 1) % N, target, graph, visit, X, Y, M, N) if graph[x][y - 1 if y - 1 >= 0 else N - 1] == target: cnt += dfs(x, y - 1 if y - 1 >= 0 else N - 1, target, graph, visit, X, Y, M, N) if graph[x - 1][y] == target: cnt += dfs(x - 1, y, target, graph, visit, X, Y, M, N) return cnt + 1
def main(): while True: try: M, N = map(int, input().split()) except EOFError: break graph = [] graph.append(list('$' * N)) for _ in range(M): graph.append(list(input())) graph.append(list('$' * N)) vis = [[False] * (N) for _ in range(M + 2)] X, Y = map(int, input().split()) X += 1 target = graph[X][Y] Max = 0 for i in range(1, M + 1): for j in range(N): if graph[i][j] == target and not vis[i][j]: Max = max(Max, dfs(i, j, target, graph, vis, X, Y, M, N)) print(Max) input()
if __name__ == "__main__": main()
|
近期评论