anon20016

P

Nov 23rd, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int a[200][200];
  9. int d[200][200];
  10. pair<int, int> p[200][200];
  11. const int INF = 1000000000;
  12. int main()
  13. {
  14.     int n, m, g, t;
  15.     cin >> m >> n >> g >> t;
  16.     for (int i = 0; i <= m; i++) {
  17.         for (int j = 0; j <= n; j++) {
  18.             a[i][j] = 0;
  19.             d[i][j] = -INF;
  20.         }
  21.     }
  22.     d[1][1] = 0;
  23.     p[1][1] = { -1, -1 };
  24.     for (int i = 0; i < g; i++) {
  25.         int q, w;
  26.         cin >> q >> w;
  27.         a[q][w] = 1;
  28.     }
  29.     for (int i = 0; i < t; i++) {
  30.         int q, w;
  31.         cin >> q >> w;
  32.         a[q][w] = -1;
  33.     }
  34.     for (int i = 1; i <= m; i++) {
  35.         for (int j = 1; j <= n; j++) {
  36.             if (a[i][j] == -1) {
  37.                 continue;
  38.             }
  39.             if (d[i - 1][j] >= 0) {
  40.                 d[i][j] = d[i - 1][j];
  41.                 p[i][j] = { i - 1, j };
  42.             }
  43.             if (d[i][j - 1] >= 0 && d[i][j - 1] > d[i][j]) {
  44.                 d[i][j] = d[i][j - 1];
  45.                 p[i][j] = { i, j - 1 };
  46.             }
  47.             if (a[i][j] == 1) {
  48.                 d[i][j]++;
  49.             }
  50.         }
  51.     }
  52.  
  53.     if (d[m][n] >= 0) {
  54.         cout << d[m][n] << endl;
  55.         pair<int, int> r = { m, n };
  56.         vector<pair<int, int> > ans;
  57.         ans.push_back({ m, n });
  58.         while (p[r.first][r.second].first != -1) {
  59.             ans.push_back(p[r.first][r.second]);
  60.             r = p[r.first][r.second];
  61.         }
  62.         for (int i = ans.size() - 1; i >= 0; i--) {
  63.             cout << ans[i].first << ' ' << ans[i].second << endl;
  64.         }
  65.     }
  66.     else {
  67.         cout << -1;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment