summaryrefslogtreecommitdiffstats
path: root/Main/Python/2020/S2.py
blob: 83c852dafecd23777baec0a122e9c2da1e184959 (plain)
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
49
50
51
from queue import Queue
from math import sqrt
# from functools import lru_cache


# @lru_cache()
def get_factors(n):
    output = []
    for i in range(1, 1 + int(sqrt(n))):
        if n % i == 0:
            output.append(i)
            # output.append(n / i)
    return output


M = int(input())
N = int(input())

room = []
for i in range(M):
    room.append(tuple(map(int, input().split())))

q = Queue()

q.put((0, 0))

seen = set()

while not q.empty():
    r, c = q.get()

    if (r, c) in seen:
        continue
    else:
        seen.add((r, c))

    if r == M - 1 and c == N - 1:
        print("yes")
        quit()

    n = room[r][c]
    for factor in get_factors(n):
        row, col = int(factor - 1), int(n / factor - 1)
        if 0 <= row < M and 0 <= col < N:
            q.put((row, col))

        if 0 <= col < M and 0 <= row < N:
            q.put((col, row))


print("no")