#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ii pair<int,int>
#define F first
#define S second
#define endl '\n'
const int N = 105;
char a[N][N];
signed main(){
int r, s;
cin >> r >> s;
ii start, target;
for(int i=0; i<r; ++i){
for(int j=0; j<s; ++j){
cin >> a[i][j];
if(a[i][j] == 'o'){
start = {i, j};
}
if(a[i][j] == 'x'){
target = {i, j};
}
}
}
auto dfs = [&](ii cur, auto&& dfs) -> int {
if(cur == target) return 0;
if(a[cur.F][cur.S] == '.') return 4e18;
if(a[cur.F][cur.S] == '<'){
int tmp = dfs({cur.F, cur.S-1}, dfs);
if(tmp != 4e18){
return 1 + tmp;
}
return 4e18;
}
if(a[cur.F][cur.S] == '>'){
int tmp = dfs({cur.F, cur.S+1}, dfs);
if(tmp != 4e18){
return 1 + tmp;
}
return 4e18;
}
if(a[cur.F][cur.S] == '^'){
int tmp = dfs({cur.F-1, cur.S}, dfs);
if(tmp != 4e18){
return 1 + tmp;
}
return 4e18;
}
if(a[cur.F][cur.S] == 'v'){
int tmp = dfs({cur.F+1, cur.S}, dfs);
if(tmp != 4e18){
return 1 + tmp;
}
return 4e18;
}
return 4e18;
};
string aux = "ENSW";
vector<int> ans(4);
ans[0] = dfs(make_pair(start.F, start.S+1), dfs);
ans[1] = dfs(make_pair(start.F-1, start.S), dfs);
ans[2] = dfs(make_pair(start.F+1, start.S), dfs);
ans[3] = dfs(make_pair(start.F, start.S-1), dfs);
int mn = min({ans[0], ans[1], ans[2], ans[3]});
if(mn == 4e18){
cout << ":(";
return 0;
}
for(int i=0; i<4; ++i){
if(ans[i] == mn){
cout << ":)\n";
cout << aux[i];
return 0;
}
}
assert(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
428 KB |
Output is correct |
9 |
Correct |
0 ms |
440 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |