This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class F> struct RecLambda : private F {
explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int main() {
int H, W;
std::cin >> H >> W;
vector<char> grid(H * W);
for (auto& x : grid) {
std::cin >> x;
}
int src = 0, dst = 0;
while (grid[src] != 'o') src += 1;
while (grid[dst] != 'x') dst += 1;
vector<int> dist(H * W, -1);
dist[dst] = 0;
const auto dfs = RecLambda([&](auto&& dfs, const int u) -> int {
if (dist[u] >= 0) return dist[u];
if (grid[u] == '<') return dist[u] = dfs(u - 1) + 1;
if (grid[u] == '>') return dist[u] = dfs(u + 1) + 1;
if (grid[u] == '^') return dist[u] = dfs(u - W) + 1;
if (grid[u] == 'v') return dist[u] = dfs(u + W) + 1;
return H * W;
});
pair<int, char> ans(H * W, 'Z');
ans = std::min(ans, std::make_pair(dfs(src - 1), 'W'));
ans = std::min(ans, std::make_pair(dfs(src + 1), 'E'));
ans = std::min(ans, std::make_pair(dfs(src - W), 'N'));
ans = std::min(ans, std::make_pair(dfs(src + W), 'S'));
if (ans.first == H * W) {
std::cout << ":(\n";
} else {
std::cout << ":)\n";
std::cout << ans.second << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |