-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020_S2.cpp
53 lines (48 loc) · 1.25 KB
/
2020_S2.cpp
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
//BFS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
int arr[maxn][maxn];
queue<pair<int, int>> q;
bool vis[maxn][maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> arr[i][j];
q.push({1, 1});
vis[1][1] = true;
while (!q.empty()){
pair<int, int> vt = q.front(); q.pop();
int x = vt.first, y = vt.second;
if (x > n || y > m) continue;
int num = arr[x][y];
for (int i = 1; i <= sqrt(num); i++){
if (num % i == 0){
if (i <= n && (num/i) <= m && vis[i][num/i]) continue;
else if (i <= n && (num/i) <= m){
q.push({i, num/i});
vis[i][num/i] = true;
if (vis[n][m]){
cout <<"yes" << endl; return 0;
}
}
if (i <= m && (num/i) <= n && vis[num/i][i]) continue;
else if (i <= m && (num/i) <= n){
q.push({num/i, i});
vis[num/i][i] = true;
if (vis[n][m]){
cout << "yes" << endl; return 0;
}
}
}
}
}
if (vis[n][m]) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}