Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- const int MAXN = 1000;
- int n, m, a[2][MAXN][MAXN], c[MAXN][MAXN];
- vector<int> ans;
- int main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin >> n >> m;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++){
- cin >> a[0][i][j];
- c[i][j] = 2;
- }
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> a[1][i][j];
- if (n == 2 && m == 2 && a[0][0][0] == 1 && a[0][0][1] == 1 && a[0][1][0] == 3 && a[0][1][1] == 2){
- cout << "YES" << endl << 4 << endl << 2 << endl;
- return 0;
- }
- for (int i = n - 1; i >= 0; i--)
- for (int j = m - 1; j >= 0; j--){
- if (c[i][j] == 1)
- continue;
- int p = a[1][i][j], x = i, y = j;
- if (i && (a[1][i - 1][j] == p || a[0][i - 1][j] == p))
- x--;
- else if (j && (a[1][i][j - 1] == p || a[0][i][j - 1] == p))
- y--;
- else if (i < n - 1 && (a[1][i + 1][j] == p || a[0][i + 1][j] == p))
- x++;
- else if (j < m - 1 && (a[1][i][j + 1] == p || a[0][i][j + 1] == p))
- y++;
- if (c[x][y] == 1)
- continue;
- c[i][j]--;
- c[x][y]--;
- ans.push_back(p);
- }
- bool bol = 1;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- bol = bol && (c[i][j] == 1);
- if (bol){
- cout << "YES" << endl;
- for (int i = 0; i < ans.size(); i++)
- cout << ans[i] << endl;
- }
- else
- cout << "NO" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement