Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/detail/standard_policies.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- template<typename T>
- using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- #define all(v) ((v).begin()), ((v).end())
- #define SZ(v) ((int)((v).size()))
- #define clr(v, d) memset(v, d, sizeof(v))
- #define has(c,i) ((c).find(i) != end(c))
- #define UNIQUE(v) sort(all(v));v.erase(unique(all(v)),v.end());
- #define has_str(s, c) ((s).find(c) != string::npos)
- typedef long long ll;
- constexpr int N = 2e5+9;
- string target = "IEHOVA#";
- char a[9][9];
- int n, m;
- pair<int, int> st;
- vector<string> ans;
- bool valid (int row, int col) {
- return (row < n and row >= 0 and col >= 0 and col < m);
- }
- void search (int row, int col, int idx) {
- if(idx > SZ(target) || !valid(row, col))
- return;
- if(valid(row - 1, col) && target[idx] == a[row - 1][col]) {
- ans.emplace_back("forth");
- search(row - 1, col, idx + 1);
- }
- if(valid(row, col + 1) && target[idx] == a[row][col + 1]) {
- ans.emplace_back("right");
- search(row, col + 1, idx + 1);
- }
- if(valid(row, col - 1) && target[idx] == a[row][col - 1]) {
- ans.emplace_back("left");
- search(row, col - 1, idx + 1);
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int test_cases;
- cin >> test_cases;
- while (test_cases--) {
- cin >> n >> m;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> a[i][j];
- if(a[i][j] == '@') {
- st = {i, j};
- }
- }
- }
- search(st.first, st.second, 0);
- cout << ans[0];
- for(int i = 1; i < SZ(ans); ++i)
- cout << " " << ans[i];
- cout << endl;
- ans.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement