Submission #1312545

#TimeUsernameProblemLanguageResultExecution timeMemory
1312545mantaggezPatkice (COCI20_patkice)C++20
50 / 50
2 ms584 KiB
#include <bits/stdc++.h>

using namespace std;

#define pii pair<int, int>
#define tup tuple<int, int, char, int>

const int nx = 105;

int n, m, dist[nx][nx];
int di[] = {0, 0, -1, 1};
int dj[] = {-1, 1, 0, 0};
pii src, ed;
char sea[nx][nx];
char d[] = {'W', 'E', 'N', 'S'};
vector<pair<int, char>> res;
map<char, int> dir;
queue<tup> q;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            cin >> sea[i][j];
            if(sea[i][j] == 'o') src = {i, j};
            if(sea[i][j] == 'x') ed = {i, j};
            dist[i][j] = INT_MAX;
        }
    }

    dir['<'] = 0;
    dir['>'] = 1;
    dir['^'] = 2;
    dir['v'] = 3;

    for(int i=0;i<4;i++) {
        auto [x, y] = src;
        q.push({x + di[i], y + dj[i], d[i], 1});
    }

    while(!q.empty())
    {
        auto [i, j, ans, cur] = q.front();
        q.pop();

        if(ed == make_pair(i, j)) {
            res.push_back({cur, ans});
            continue;
        }

        if(sea[i][j] == '.' || dist[i][j] < cur) continue;

        if(dist[i][j] > cur) dist[i][j] = cur;
        int idx = dir[sea[i][j]];
        q.push({i + di[idx], j + dj[idx], ans, cur + 1});
    }

    if(res.empty()) cout << ":(" ;
    else
    {
        int mn = INT_MAX;
        char ans;
        for(auto [dist, c] : res)
        {
            if(dist < mn) {
                mn = dist;
                ans = c;
            }
            else if(dist == mn && c < ans) {
                ans = c;
            }
        }
        cout << ":)\n" << ans;
    }

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