Submission #1236150

#TimeUsernameProblemLanguageResultExecution timeMemory
1236150ghassanhasanPatkice II (COCI21_patkice2)C++20
110 / 110
180 ms40900 KiB
#include <bits/stdc++.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));
int n, m, si, sj, ei, ej;
bool valid(int i, int j)
{
    return i >= 0 && j >= 0 && i < n && j < m && par[i][j] == -1;
}
int bfs()
{
    deque<array<int, 4>> q;
    par[si][sj] = -5;
    for (int k = 0; k < 4; k++)
    {
        int ni = si + di[k], nj = sj + dj[k];
        if (valid(si + di[k], sj + dj[k]))
        {
            q.push_back({0, ni, nj, k});
        }
    }
    while (!q.empty())
    {
        auto [c, i, j, p] = q.front();
        q.pop_front();
        if (par[i][j] != -1)
            continue;
        par[i][j] = p;
        if (i == ei && j == ej)
            return c;
        for (int k = 0; k < 4; k++)
        {
            int ni = i + di[k], nj = j + dj[k];
            if (!valid(ni, nj))
                continue;
            // q.push({c + (grid[i][j] != C[k]), ni, nj, k});
            if (grid[i][j] != C[k])
            {
                q.push_back({c + 1, ni, nj, k});
            }
            else
            {
                q.push_front({c, ni, nj, k});
            }
        }
    }
    return 0;
}
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() << 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:93:11: warning: ignoring return value of 'int system(const char*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |     system("pause");
      |     ~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...