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 namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define nbits(x) __builtin_popcount(x)
#define gcd(x, y) __gcd(x, y)
#define SYNC_OFF ios::sync_with_stdio(0); cin.tie(0);
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
const int MOD = 1e9 + 7; // 998244353;
const int N = 300100;
const int INF = 1e15; // 1e18;
const double PI = acos(-1);
const int dx[] = {-1,0,1,0,-1,1,1,-1};
const int dy[] = {0,1,0,-1,1,1,-1,-1};
int powmod(int a, int b, int m = MOD) {
int res = 1;
a %= m; assert(b >= 0);
for (; b; b>>=1) {
if (b & 1) res = res*a % m;
a = a*a % m;
}
return res;
}
int n, m, steps;
char dir;
char a[101][101];
int vis[101][101];
vector<pair<int, char>> ans;
void dfs(int x, int y) {
steps++;
vis[x][y] = 1;
if (a[x][y] == 'x') {
ans.pb({steps, dir});
} else if (a[x][y] == '^') {
dfs(x - 1, y);
} else if (a[x][y] == '>') {
dfs(x, y + 1);
} else if (a[x][y] == 'v') {
dfs(x + 1, y);
} else if (a[x][y] == '<') {
dfs(x, y - 1);
}
}
signed main() {
SYNC_OFF;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> (a[i] + 1);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 'o') {
memset(vis, 0, sizeof vis);
steps = 0, dir = 'N'; dfs(i - 1, j);
memset(vis, 0, sizeof vis);
steps = 0, dir = 'S'; dfs(i + 1, j);
memset(vis, 0, sizeof vis);
steps = 0, dir = 'W'; dfs(i, j - 1);
memset(vis, 0, sizeof vis);
steps = 0, dir = 'E'; dfs(i, j + 1);
}
}
}
sort(all(ans));
if (ans.size() == 0) {
cout << ":(\n";
} else {
cout << ":)\n" << ans[0].second << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |