Submission #1239151

#TimeUsernameProblemLanguageResultExecution timeMemory
1239151ghassanhasanPatkice II (COCI21_patkice2)C++20
110 / 110
248 ms85756 KiB
#include <bits/stdc++.h>
//#include <conio.h>
using namespace std;
#define ll long long

const int N = 2e3 + 5;
const int di[] = {0, 0, 1, -1};
const int dj[] = {-1, 1, 0, 0};
const string C = "<>v^";
vector grid(N, string());
vector par(N, vector<int>(N, -1)), cost(N, vector<int>(N, 1e9));
int n, m, si, sj, ei, ej;
bool valid(int i, int j)
{
    return i >= 0 && j >= 0 && i < n && j < m;
}
void dfs(int i, int j, vector<pair<int, int>> &ret)
{
    ret.emplace_back(i, j);
    for (int k = 0; k < 4; k++)
    {
        int ni = i + di[k], nj = j + dj[k];
        if (valid(ni, nj) && grid[i][j] == C[k] && cost[ni][nj] > cost[i][j])
        {
            par[ni][nj] = k;
            cost[ni][nj] = cost[i][j];
            dfs(ni, nj, ret);
        }
    }
}
int bfs()
{
    vector<array<int, 3>> q;
    q.push_back({si, sj, 0});
    par[si][sj] = -5;
    cost[si][sj] = 0;
    vector<pair<int, int>> cur;
    for (int st = 0; st < q.size(); st++)
    {
        auto [i, j, c] = q[st];
        dfs(i, j, cur);
        for (auto [x, y] : cur)
        {
            for (int k = 0; k < 4; k++)
            {
                int ni = x + di[k], nj = y + dj[k];
                if (valid(ni, nj) && cost[ni][nj] > c + 1)
                {
                    par[ni][nj] = k;
                    cost[ni][nj] = c + 1;
                    q.push_back({ni, nj, c + 1});
                }
            }
        }
        cur.clear();
    }
    return cost[ei][ej];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> grid[i];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (grid[i][j] == 'o')
                si = i, sj = j;
            else if (grid[i][j] == 'x')
                ei = i, ej = j;
        }
    }
    cout << bfs() - 1 << endl;
    while (par[ei][ej] != -5)
    {
        int k = par[ei][ej];
        int i = ei - di[k], j = ej - dj[k];
        grid[i][j] = C[k];
        ei = i;
        ej = j;
    }
    grid[si][sj] = 'o';
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << grid[i][j];
        }
        cout << '\n';
    }
    cout.flush();
    system("pause");
    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:97:11: warning: ignoring return value of 'int system(const char*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     system("pause");
      |     ~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...