summaryrefslogtreecommitdiffstats
path: root/Main/C++/2020/S2.cpp
blob: 42873ddbe0165e8a339ebf2dda772beeab05d642 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
using namespace std;


vector<int>  get_factors(int n)
{
    vector<int> output;
    for (int i = 1; i < 1 + sqrt(n); i++)
    {
        if (n % i == 0)
        {
            output.push_back(i);
        }
    }
    return output;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int M, N;
    cin >> M;
    cin >> N;

    vector<vector<int>> room;
    int temp;

    for (int i = 0; i < M;i++)
    {
        vector<int> v;
        for (int j = 0; j < N; j++)
        {
            cin >> temp;
            v.push_back(temp);
        }
        room.push_back(v);
    }

    queue<vector<int>> q;
    q.push({0,0});
    vector<vector<int>> seen;

    while (!q.empty())
    {
        vector<int> current = q.front();
        q.pop();

        if (find(seen.begin(), seen.end(), current) != seen.end())
            {
                continue;
            }
        else
            {
                seen.push_back(current);
            }

        int r = current[0];
        int c = current[1];

        if (r == M -1 && c == N-1)
        {
            cout << "yes" << endl;
            return 0;
        }

        int n = room[r][c];
        vector<int> factors = get_factors(n);


        for (int i = 0; i < factors.size(); i++)
        {
            int row = factors[i] - 1;
            int col = n / factors[i] - 1;
            if ( 0 <= row && row < M && 0 <= col && col < N)
            {
                q.push({row, col});
            }
            if ( 0 <= col && col < M && 0 <= row && row < N)
            {
                q.push({col, row});
            }

        }


    }
    cout << "no" << endl;
    return 0;
}