# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
330075 | phathnv | Patkice (COCI20_patkice) | C++11 | 326 ms | 524292 KiB |
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>
#define mp make_pair
#define X first
#define Y second
#define taskname "Patkice"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 102;
const int INF = 1e9;
int r, c, d[N][N];
char s[N][N];
map <char, int> dx, dy;
void readInput(){
cin >> r >> c;
for(int i = 1; i <= r; i++)
cin >> (s[i] + 1);
}
bool inside(int x, int y){
return (1 <= x && x <= r && 1 <= y && y <= c);
}
int dfs(int x, int y){
if (!inside(x, y))
return INF;
if (d[x][y] != -1)
return d[x][y];
if (s[x][y] == 'x'){
d[x][y] = 0;
return 0;
}
if (s[x][y] == '.'){
d[x][y] = INF;
return INF;
}
int nxtX = x + dx[s[x][y]];
int nxtY = y + dy[s[x][y]];
d[x][y] = dfs(nxtX, nxtY) + 1;
return d[x][y];
}
void solve(){
int sX = -1, sY = -1;
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++)
if (s[i][j] == 'o'){
sX = i;
sY = j;
}
dx['^'] = -1;
dx['v'] = 1;
dy['<'] = -1;
dy['>'] = 1;
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++)
d[i][j] = -1;
int bestDist = INF;
char bestDir = '@';
if (bestDist > dfs(sX - 1, sY)){
bestDist = dfs(sX - 1, sY);
bestDir = 'N';
}
if (bestDist > dfs(sX + 1, sY)){
bestDist = dfs(sX + 1, sY);
bestDir = 'S';
}
if (bestDist > dfs(sX, sY - 1)){
bestDist = dfs(sX, sY - 1);
bestDir = 'W';
}
if (bestDist > dfs(sX, sY + 1)){
bestDist = dfs(sX, sY + 1);
bestDir = 'E';
}
if (bestDist >= INF){
cout << ":(";
return;
}
cout << ":)\n" << bestDir;
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
readInput();
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |