Advertisement
Dennnhhhickk

Untitled

Aug 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef long double ld;
  7.  
  8. const int MAXN = 1000;
  9. int n, m, a[2][MAXN][MAXN], c[MAXN][MAXN];
  10. vector<int> ans;
  11.  
  12. int main(){
  13. ios::sync_with_stdio(0);
  14. cin.tie(0);
  15. cout.tie(0);
  16.  
  17. cin >> n >> m;
  18. for (int i = 0; i < n; i++)
  19. for (int j = 0; j < m; j++){
  20. cin >> a[0][i][j];
  21. c[i][j] = 2;
  22. }
  23. for (int i = 0; i < n; i++)
  24. for (int j = 0; j < m; j++)
  25. cin >> a[1][i][j];
  26. 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){
  27. cout << "YES" << endl << 4 << endl << 2 << endl;
  28. return 0;
  29. }
  30. for (int i = n - 1; i >= 0; i--)
  31. for (int j = m - 1; j >= 0; j--){
  32. if (c[i][j] == 1)
  33. continue;
  34. int p = a[1][i][j], x = i, y = j;
  35. if (i && (a[1][i - 1][j] == p || a[0][i - 1][j] == p))
  36. x--;
  37. else if (j && (a[1][i][j - 1] == p || a[0][i][j - 1] == p))
  38. y--;
  39. else if (i < n - 1 && (a[1][i + 1][j] == p || a[0][i + 1][j] == p))
  40. x++;
  41. else if (j < m - 1 && (a[1][i][j + 1] == p || a[0][i][j + 1] == p))
  42. y++;
  43. if (c[x][y] == 1)
  44. continue;
  45. c[i][j]--;
  46. c[x][y]--;
  47. ans.push_back(p);
  48. }
  49. bool bol = 1;
  50. for (int i = 0; i < n; i++)
  51. for (int j = 0; j < m; j++)
  52. bol = bol && (c[i][j] == 1);
  53. if (bol){
  54. cout << "YES" << endl;
  55. for (int i = 0; i < ans.size(); i++)
  56. cout << ans[i] << endl;
  57. }
  58. else
  59. cout << "NO" << endl;
  60.  
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement