#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,O3,unroll-loops")
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl "\n"
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < n; i++)
#define pb push_back
#define fi first
#define se second
int inf = LLONG_MAX >> 3;
int n, m;
string grid[100];
int solved;
vector<string> ans;
int mn = inf;
void floodfill(int y, int x, int d = 0) {
if(x < 0 || y < 0 || x >= m || y >= n || grid[y][x] == '.') return;
if(grid[y][x] == 'x') {
solved = d;
return;
}
if(grid[y][x] == '^') floodfill(y-1, x, d+1);
if(grid[y][x] == 'v') floodfill(y+1, x, d+1);
if(grid[y][x] == '<') floodfill(y, x-1, d+1);
if(grid[y][x] == '>') floodfill(y, x+1, d+1);
}
void solve() {
cin >> n >> m;
F(i, 0, n) cin >> grid[i];
int y, x;
F(i, 0, n) {
F(j, 0, m) {
if(grid[i][j] == 'o') {
y = i; x = j;
break;
}
}
}
solved = 0; floodfill(y-1, x);
if(solved && solved < mn) ans = {"N"}, mn = solved;
else if(solved && solved == mn) ans.pb("N");
solved = 0; floodfill(y+1, x);
if(solved && solved < mn) ans = {"S"}, mn = solved;
else if(solved && solved == mn) ans.pb("S");
solved = 0; floodfill(y, x-1);
if(solved && solved < mn) ans = {"W"}, mn = solved;
else if(solved && solved == mn) ans.pb("W");
solved = 0; floodfill(y, x+1);
if(solved && solved < mn) ans = {"E"}, mn = solved;
else if(solved && solved == mn) ans.pb("E");
if(ans.size() == 0) cout << ":(" << endl;
else if(ans.size() == 1) cout << ":)\n" << ans[0] << endl;
else {
sort(ans.begin(), ans.end());
cout << ":)\n" << ans[0] << endl;
}
}
void setIO() {
ios::sync_with_stdio(0); cin.tie(0);
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
signed main() {
setIO();
solve();
}
Compilation message
patkice.cpp: In function 'void solve()':
patkice.cpp:60:26: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | solved = 0; floodfill(y, x-1);
| ~~~~~~~~~^~~~~~~~
patkice.cpp:52:26: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | solved = 0; floodfill(y-1, x);
| ~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
356 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
436 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
360 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |