Submission #366382

#TimeUsernameProblemLanguageResultExecution timeMemory
366382NONAMEPatkice (COCI20_patkice)C++14
50 / 50
1 ms876 KiB
#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
using namespace std;

const int man = (int)(1e3);
const int steps[4][2] = {{0, +1}, {-1, 0}, {+1, 0}, {0, -1}};

int n, m, res, ps, sx, sy;
int a[man][man];
bool mk[man][man];

void rec(int x, int y, int fx, int cur) {
    if (a[x][y] == -1) {
        return;
    }
    if (mk[x][y] == true) {
        return;
    }
    if (a[x][y] == 4) {
        if ((ps == -1) || (cur < res)) {
            res = cur;
            ps = fx;
        }
        return;
    }

    mk[x][y] = true;
    rec(x + steps[a[x][y]][0], y + steps[a[x][y]][1], fx, cur + 1);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #ifdef _LOCAL
        in("inA.txt");
        out("outA.txt");
    #endif

    string t = "ENSW";

    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char c;
            cin >> c;
            if (c == 'o') {
                sx = i, sy = j;
            }
            if ((c == 'o') || (c == '.')) {
                a[i][j] = -1;
            }
            if (c == 'x') {
                a[i][j] = 4;
            }
            if (c == '<') {
                a[i][j] = 3;
            }
            if (c == '^') {
                a[i][j] = 1;
            }
            if (c == '>') {
                a[i][j] = 0;
            }
            if (c == 'v') {
                a[i][j] = 2;
            }
        }
    }

    ps = -1;
    for (int i = 0; i < 4; ++i) {
        rec(sx + steps[i][0], sy + steps[i][1], i, 0);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                mk[i][j] = 0;
            }
        }
    }

    if (ps != -1) {
        cout << ":)\n" << t[ps] << "\n";
    } else {
        cout << ":(";
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...