Advertisement
Hamoudi30

marcus uva

Jul 30th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/detail/standard_policies.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. template<typename T>
  7. using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  8. #define all(v)          ((v).begin()), ((v).end())
  9. #define SZ(v)           ((int)((v).size()))
  10. #define clr(v, d)       memset(v, d, sizeof(v))
  11. #define has(c,i)        ((c).find(i) != end(c))
  12. #define UNIQUE(v)       sort(all(v));v.erase(unique(all(v)),v.end());
  13. #define has_str(s, c)   ((s).find(c) != string::npos)
  14. typedef long long ll;
  15. constexpr int N = 2e5+9;
  16. string target = "IEHOVA#";
  17. char a[9][9];
  18. int n, m;
  19. pair<int, int> st;
  20. vector<string> ans;
  21. bool valid (int row, int col) {
  22.     return (row < n and row >= 0 and col >= 0 and col < m);
  23. }
  24. void search (int row, int col, int idx) {
  25.     if(idx > SZ(target) || !valid(row, col))
  26.         return;
  27.     if(valid(row - 1, col) && target[idx] == a[row - 1][col]) {
  28.         ans.emplace_back("forth");
  29.         search(row - 1, col, idx + 1);
  30.     }
  31.     if(valid(row, col + 1) && target[idx] == a[row][col + 1]) {
  32.         ans.emplace_back("right");
  33.         search(row, col + 1, idx + 1);
  34.     }
  35.     if(valid(row, col - 1) && target[idx] == a[row][col - 1]) {
  36.         ans.emplace_back("left");
  37.         search(row, col - 1, idx + 1);
  38.     }
  39. }
  40. int main() {
  41.     ios_base::sync_with_stdio(false);
  42.     cin.tie(nullptr);
  43.     cout.tie(nullptr);
  44.     int test_cases;
  45.     cin >> test_cases;
  46.     while (test_cases--) {
  47.         cin >> n >> m;
  48.         for (int i = 0; i < n; ++i) {
  49.             for (int j = 0; j < m; ++j) {
  50.                 cin >> a[i][j];
  51.                 if(a[i][j] == '@') {
  52.                     st = {i, j};
  53.                 }
  54.             }
  55.         }
  56.         search(st.first, st.second, 0);
  57.         cout << ans[0];
  58.         for(int i = 1; i < SZ(ans); ++i)
  59.             cout << " " << ans[i];
  60.         cout << endl;
  61.         ans.clear();
  62.     }
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement