Submission #675332

# Submission time Handle Problem Language Result Execution time Memory
675332 2022-12-27T04:18:52 Z vjudge1 Portals (BOI14_portals) C++14
0 / 100
3 ms 4380 KB
#include <bits/stdc++.h>
#define ii pair<int,int>
#define fi first
#define se second

using namespace std;

int R, C, i, j;
char a[1005][1005];
int wL[1005][1005][2], wR[1005][1005][2];
int t[1005][1005];
int cx, cy;
queue< pair<int, int> > q;

void bfs(int x, int y)
{
    if (x >= 0 && y >= 0 && x < C && y < R && t[y][x] == -1 && a[y][x] != '#')
    {
        t[y][x] = t[j][i] + 1;
        q.push(make_pair(x, y));
    }
}

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

    memset(t,-1,sizeof t);

    cin >> R >> C;

    for (int j = 0; j < R; j++)
    {
        for (int i = 0; i < C; i++) cin >> a[j][i];

        for (int i = 0; i < C; i++)
        {
            t[j][i] = -1;
            if(a[j][i] == 'S')
            {
                t[j][i] = 0;
                q.push({i,j});
            }
            if(a[j][i] == 'C')
            {
                cx = i;
                cy = j;
            }
        }
    }

    for (int j = 0; j < R; j++)
        for (int i = 0; i < C; i++)
        {
            wR[j][i][0] = wR[j-1][i][0];
            if(a[j-1][i] == '#') wR[j][i][0] = j;

            wL[j][i][0] = wL[j][i-1][0];
            if(a[j][i-1] == '#') wL[j][i][0] = i;
        }

    for (int j = R-1; j >= 0; j--)
    for (int i = C-1; i >= 0; i--)
    {
        wR[j][i][1] = wR[j+1][i][1];
        if(a[j+1][i] == '#') wR[j][i][1] = j;

        wL[j][i][1] = wL[j][i+1][1];
        if(a[j][i] == '#') wL[i][j][1] = i;
    }

    while (q.size() && t[cy][cx] == -1)
    {
        i = q.front().first;
        j = q.front().second;
        q.pop();
        bfs(i, j-1);
        bfs(i, j+1);
        bfs(i-1, j);
        bfs(i+1, j);
        if (wL[j][i][0] == i || wL[j][i][1] == i || wR[j][i][0] == j || wR[j][i][1] == j)
        {
            bfs(wL[j][i][0], j);
            bfs(wL[j][i][1], j);
            bfs(i, wR[j][i][0]);
            bfs(i, wR[j][i][1]);
        }
    }

    cout << t[cy][cx];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Incorrect 2 ms 4308 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 3 ms 4308 KB Output is correct
3 Incorrect 2 ms 4308 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4380 KB Output is correct
3 Incorrect 2 ms 4308 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Incorrect 2 ms 4308 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Incorrect 2 ms 4308 KB Output isn't correct
4 Halted 0 ms 0 KB -