#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int dx[] = {0, 1, -1, 0};
int dy[] = {-1, 0, 0, 1};
map<int, char> mp =
{
{0,'W'},
{1,'S'},
{2,'N'},
{3,'E'}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int best = 1e9 + 5, ans = -1;
int n, m, startx, starty;
cin >> n >> m;
char mat[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> mat[i][j];
if (mat[i][j] == 'o') {
startx = i;
starty = j;
}
}
}
for(int i = 0; i < 4; i++) {
int length = 0;
int x = startx + dx[i], y = starty + dy[i];
bool reach = 0;
while(1) {
if (x < 0 || x >= n | y < 0 || y >= m) break;
if (mat[x][y] == '.' || mat[x][y] == 'o') break;
if (mat[x][y] == 'x') {
reach = 1;
break;
}
if (mat[x][y] == '<') {
x += dx[0];
y += dy[0];
} else if (mat[x][y] == 'v') {
x += dx[1];
y += dy[1];
} else if (mat[x][y] == '^') {
x += dx[2];
y += dy[2];
} else if (mat[x][y] == '>') {
x += dx[3];
y += dy[3];
}
length++;
}
if (reach && length <= best) {
best = length;
ans = i;
}
}
if (ans != -1) {
cout << ":)\n";
cout << mp[ans] << "\n";
} else {
cout << ":(";
}
return 0;
}
//~ check for overflows
Compilation message
patkice.cpp: In function 'int main()':
patkice.cpp:35:28: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
35 | if (x < 0 || x >= n | y < 0 || y >= m) break;
| ~~^~~~
patkice.cpp:32:13: warning: 'startx' may be used uninitialized in this function [-Wmaybe-uninitialized]
32 | int x = startx + dx[i], y = starty + dy[i];
| ^
patkice.cpp:32:33: warning: 'starty' may be used uninitialized in this function [-Wmaybe-uninitialized]
32 | int x = startx + dx[i], y = starty + dy[i];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
380 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |