Submission #330079

# Submission time Handle Problem Language Result Execution time Memory
330079 2020-11-23T17:33:18 Z phathnv Patkice (COCI20_patkice) C++11
50 / 50
1 ms 512 KB
#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;
    }
    if (s[x][y] == 'o'){
        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;
                break;
            }
    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, sY + 1)){
        bestDist = dfs(sX, sY + 1);
        bestDir = 'E';
    }
    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 >= 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

patkice.cpp: In function 'int main()':
patkice.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   97 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
patkice.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   98 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 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 384 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 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 384 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 0 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 384 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