Submission #675249

#TimeUsernameProblemLanguageResultExecution timeMemory
675249vjudge1Portals (BOI14_portals)C++17
100 / 100
230 ms37148 KiB
#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 1e3 + 5;
const ll INF = 1e18 + 5;

int m, n;
string s[MAXN];

void Input()
{
    cin >> m >> n;
    for (int i = 1; i <= m; i++)
    {
        cin >> s[i];
        s[i] = ',' + s[i];
    }
}

pair<int, int> jump[MAXN][MAXN][4];
int d[MAXN][MAXN];

struct TState
{
    int x, y;
    int dlab;

    bool operator < (const TState& other) const
    {
        return dlab > other.dlab;
    }

    bool Valid() const
    {
        return d[x][y] == dlab;
    }
};

bool Chk(pair<int, int> p)
{
    return p.fi > 0 && p.fi <= m && p.se > 0 && p.se <= n;
}

int Ox[4] = {0, -1, 0, 1};
int Oy[4] = {-1, 0, 1, 0};

void Solve()
{
    pair<int, int> S, C;
    for (int i = 1; i <= m; i++)
    {
        jump[i][0][0] = {i, 1};
        jump[i][n + 1][2] = {i, n};
    }
    for (int i = 1; i <= n; i++)
    {
        jump[0][i][1] = {1, i};
        jump[m + 1][i][3] = {m, i};
    }
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (s[i][j] == '#')
            {
                jump[i][j][0] = {i, j + 1};
                jump[i][j][1] = {i + 1, j};
            } else
            {
                if (s[i][j] == 'S')
                    S = {i, j};
                if (s[i][j] == 'C')
                    C = {i, j};
                jump[i][j][0] = jump[i][j - 1][0];
                jump[i][j][1] = jump[i - 1][j][1];
            }
        }
    }
    for (int i = m; i >= 1; i--)
    {
        for (int j = n; j >= 1; j--)
        {
            if (s[i][j] == '#')
            {
                jump[i][j][2] = {i, j - 1};
                jump[i][j][3] = {i - 1, j};
            } else
            {
                jump[i][j][2] = jump[i][j + 1][2];
                jump[i][j][3] = jump[i + 1][j][3];
            }
        }
    }

    priority_queue<TState> PQ;
    memset(d, 0x3f, sizeof(d));
    d[S.fi][S.se] = 0;
    PQ.push({S.fi, S.se, 0});
    while (!PQ.empty())
    {
        auto u = PQ.top();
        PQ.pop();
        if (!u.Valid())
            continue;
        //cout << u.x << ' ' << u.y << ' ' << u.dlab << '\n';
        if (u.x == C.fi && u.y == C.se)
        {
            cout << u.dlab;
            return;
        }
        for (int c = 0; c < 4; c++)
        {
            pair<int, int> v = {u.x + Ox[c], u.y + Oy[c]};
            if (Chk(v) && d[v.fi][v.se] > u.dlab + 1 && s[v.fi][v.se] != '#')
            {
                d[v.fi][v.se] = u.dlab + 1;
                PQ.push({v.fi, v.se, d[v.fi][v.se]});
            }
        }
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (i == j)
                    continue;
                pair<int, int> v = jump[u.x][u.y][i], z = jump[u.x][u.y][j];
                int w = abs(u.x - z.fi) + abs(u.y - z.se) + 1;
                if (Chk(v) && Chk(z) && d[v.fi][v.se] > u.dlab + w)
                {
                    d[v.fi][v.se] = u.dlab + w;
                    PQ.push({v.fi, v.se, d[v.fi][v.se]});
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(task".INP","r"))
    {
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    Input();
    Solve();
}

Compilation message (stderr)

portals.cpp: In function 'int main()':
portals.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...