#include <bits/stdc++.h>
using namespace std;
int n,m;
vector<string>v;
int mp[101][101];
bool check(int x, int y) {
if(x < 0 || x >= n || y < 0 || y >= m) return false;
return true;
}
int dfs(int i, int j) {
if(!check(i,j)) return -1;
if(v[i][j] == 'x') return 0;
if(v[i][j] == '.' || v[i][j] == 'o') return -1;
if(mp[i][j]) return mp[i][j];
if(v[i][j] == 'v') {
int ans = dfs(i+1,j);
if(ans != -1) {
return mp[i][j] = 1 + ans;
} else return mp[i][j] =-1;
}
else if(v[i][j] == '^') {
int ans = dfs(i-1,j);
if(ans != -1) {
return mp[i][j] = 1 + ans;
} else return mp[i][j] =-1;
}
else if(v[i][j] == '>') {
int ans = dfs(i,j+1);
if(ans != -1) {
return mp[i][j] = 1 + ans;
} else return mp[i][j] =-1;
}
else {
int ans = dfs(i,j-1);
if(ans != -1) {
return mp[i][j] = 1 + ans;
} else return mp[i][j] =-1;
}
}
signed main() {
cin>>n>>m;
v.resize(n);
for(auto &z : v) cin>>z;
pair<int,int>s;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(v[i][j] == 'o') s.first=i,s.second=j;
}
}
char win;
int64_t best = INT_MAX, ans;
ans=dfs(s.first,s.second+1);
if(ans != -1 && ans < best) {
win = 'E',best=ans;
}
ans=dfs(s.first-1,s.second);
if(ans != -1 && ans < best) {
win = 'N',best=ans;
}
ans=dfs(s.first+1,s.second);
if(ans!=-1&&ans < best) {
win = 'S',best=ans;
}
ans=dfs(s.first,s.second-1);
if(ans != -1 && ans < best) {
win = 'W',best=ans;
}
if(best < INT_MAX) cout << ":)\n" << win;
else cout << ":(";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |