Advertisement
ThegeekKnight16

Railguns

Jun 28th, 2023
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. //Mudar para submeter
  4. const int MAXN = 1e3 + 10;
  5. const int MAXSZ = 10;
  6. const int INF = 0x3f3f3f3f;
  7. array<array<set<int>, MAXN>, MAXN> Chegam;
  8. array<set<int>, MAXN> lin, col;
  9.  
  10. void updateChegam(int start, int x, int y, set<int> &resp)
  11. {
  12.     int limLin = *lin[x].lower_bound(start);
  13.     int limCol = *col[y].lower_bound(start);
  14.     for (int i = start; i <= min(limLin,limCol) && resp.size() <= MAXSZ; i++) resp.insert(i);
  15. }
  16.  
  17. void solve()
  18. {
  19.     int N, M;
  20.     cin >> N >> M;
  21.     int R;
  22.     cin >> R;
  23.     for (int i = 0; i < R; i++)
  24.     {
  25.         int T, dir, X;
  26.         cin >> T >> dir >> X;
  27.         if (dir == 1) lin[X].insert(T);
  28.         else if (dir == 2) col[X].insert(T);
  29.     }
  30.     for (int i = 0; i <= N; i++) lin[i].insert(INF);
  31.     for (int i = 0; i <= M; i++) col[i].insert(INF);
  32.  
  33.     updateChegam(0, 0, 0, Chegam[0][0]);
  34.  
  35.     // cerr << "||" << 0 << " " << 0 << '\n' << "|||";
  36.     // for (auto x : Chegam[0][0]) cerr << x << " ";
  37.     // cerr << '\n';
  38.  
  39.     for (int i = 0; i <= N; i++)
  40.     {
  41.         for (int j = 0; j <= M; j++)
  42.         {
  43.             if (i == 0 && j == 0) continue;
  44.             auto &resp = Chegam[i][j];
  45.             if (i != 0)
  46.             {
  47.                 auto &cheg = Chegam[i-1][j];
  48.                 for (auto x : cheg) resp.insert(x+1);
  49.             }
  50.             if (j != 0)
  51.             {
  52.                 auto &cheg = Chegam[i][j-1];
  53.                 for (auto x : cheg) resp.insert(x+1);
  54.             }
  55.             for (auto x : lin[i]) resp.erase(x);
  56.             for (auto x : col[j]) resp.erase(x);
  57.             if (!resp.empty()) updateChegam(*resp.rbegin(), i, j, resp);
  58.             // cerr << "||" << i << " " << j << '\n' << "||";
  59.             // for (auto x : resp) cerr << x << " ";
  60.             // cerr << '\n';
  61.         }
  62.     }
  63.  
  64.     if (Chegam[N][M].empty()) {cout << "-1" << '\n'; return;}
  65.  
  66.     cout << *Chegam[N][M].begin() << '\n';
  67. }
  68.  
  69. int main()
  70. {
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(NULL);
  73.     int T;
  74.     cin >> T;
  75.     while (T--) solve();
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement