ABC293C - Make Takahashi Happy

考え方

全通りの経路を再帰でみていけば良い.

回答例

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]

def f(x, y, seen):
    global ans
    if x == H - 1 and y == W - 1:
        ans += 1
        return
    if x + 1 < H:
        if A[x + 1][y] not in seen:
            seen.add(A[x + 1][y])
            f(x + 1, y, seen)
            seen.remove(A[x + 1][y])
    if y + 1 < W:
        if A[x][y + 1] not in seen:
            seen.add(A[x][y + 1])
            f(x, y + 1, seen)
            seen.remove(A[x][y + 1])
    
ans = 0
f(0, 0, set([A[0][0]]))
print(ans)