답안 #288226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
288226 2020-09-01T10:33:22 Z Alexa2001 Dangerous Skating (JOI16_skating) C++17
100 / 100
332 ms 110200 KB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 1005, Nodes = 3 * Nmax * Nmax, inf = 1e9;

int dx[] = {0, 0, +1, -1};
int dy[] = {-1, +1, 0, 0};


int n, m, nr;
char A[Nmax][Nmax];
int startX, startY, finishX, finishY;
int dist[Nodes];
bool used[Nodes];
int hor[Nmax][Nmax], ver[Nmax][Nmax];

queue<int> q[3];
vector<int> to[Nodes];


int code(int x, int y)
{
    return (x-1) * m + y;
}




void prepare()
{
    int i, j;

    nr = n * m;

    for(i=1; i<=n; ++i)
        for(j=1; j<=m; ++j)
            if(A[i][j] == '.')
            {
                if(A[i][j-1] == '.') hor[i][j] = hor[i][j-1];
                    else hor[i][j] = ++nr;

                if(A[i-1][j] == '.') ver[i][j] = ver[i-1][j];
                    else ver[i][j] = ++nr;
            }
    for(i=1; i<=n; ++i)
        for(j=1; j<=m; ++j)
            if(A[i][j] == '#')
            {
                if(A[i-1][j] == '.') to[ver[i-1][j]].push_back(code(i-1, j));
                if(A[i+1][j] == '.') to[ver[i+1][j]].push_back(code(i+1, j));

                if(A[i][j-1] == '.') to[hor[i][j-1]].push_back(code(i, j-1));
                if(A[i][j+1] == '.') to[hor[i][j+1]].push_back(code(i, j+1));
            }
}

void add1(int node)
{
    int x, y;
    if(node % m == 0) x = node / m, y = m;
        else x = node / m + 1, y = node % m;

    for(int i=0; i<4; ++i)
    {
        int nx = x + dx[i], ny = y + dy[i];
        if(A[nx][ny] != '.') continue;

        int c = code(nx, ny);

        if(dist[c] > dist[node] + 2)
        {
            dist[c] = dist[node] + 2;
            q[2].push(c);
        }
    }

    if(dist[hor[x][y]] > dist[node])
    {
        dist[hor[x][y]] = dist[node];
        q[0].push(hor[x][y]);
    }

    if(dist[ver[x][y]] > dist[node])
    {
        dist[ver[x][y]] = dist[node];
        q[0].push(ver[x][y]);
    }
}

void add2(int node)
{
    for(auto it : to[node])
        if(dist[it] > dist[node] + 1)
        {
            dist[it] = dist[node] + 1;
            q[1].push(it);
        }
}

int find_next()
{
    int i;
    for(i=0; i<=2; ++i)
        while(q[i].size() && used[q[i].front()]) q[i].pop();

    int mn = inf, x = -1, id;
    for(i=0; i<=2; ++i)
        if(q[i].size() && dist[q[i].front()] < mn)
            mn = dist[q[i].front()], x = q[i].front(), id = i;

    if(mn == inf) return -1;
    q[id].pop();
    return x;
}

int solve()
{
    int i;
    for(i=1; i<=nr; ++i) dist[i] = inf;

    q[0].push(code(startX, startY));
    dist[code(startX, startY)] = 0;

    while(1)
    {
        int node = find_next();
        if(node == -1) break;
        used[node] = 1;

        if(node <= n * m) add1(node);
            else add2(node);
    }

    return (dist[code(finishX, finishY)] == inf ? -1 : dist[code(finishX, finishY)]);
}

int main()
{
  //  freopen("input", "r", stdin);
    cin.tie(0); cin.sync_with_stdio(false);

    cin >> n >> m;

    int i;
    for(i=1; i<=n; ++i) cin >> (A[i] + 1);

    cin >> startX >> startY;
    cin >> finishX >> finishY;

    prepare();
    cout << solve() << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 71544 KB Output is correct
2 Correct 48 ms 71544 KB Output is correct
3 Correct 49 ms 71544 KB Output is correct
4 Correct 49 ms 71544 KB Output is correct
5 Correct 48 ms 71672 KB Output is correct
6 Correct 49 ms 71544 KB Output is correct
7 Correct 59 ms 71544 KB Output is correct
8 Correct 49 ms 71544 KB Output is correct
9 Correct 49 ms 71672 KB Output is correct
10 Correct 49 ms 71544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 71544 KB Output is correct
2 Correct 48 ms 71544 KB Output is correct
3 Correct 49 ms 71544 KB Output is correct
4 Correct 49 ms 71544 KB Output is correct
5 Correct 48 ms 71672 KB Output is correct
6 Correct 49 ms 71544 KB Output is correct
7 Correct 59 ms 71544 KB Output is correct
8 Correct 49 ms 71544 KB Output is correct
9 Correct 49 ms 71672 KB Output is correct
10 Correct 49 ms 71544 KB Output is correct
11 Correct 51 ms 73464 KB Output is correct
12 Correct 52 ms 73600 KB Output is correct
13 Correct 52 ms 73592 KB Output is correct
14 Correct 57 ms 74044 KB Output is correct
15 Correct 54 ms 74232 KB Output is correct
16 Correct 52 ms 73664 KB Output is correct
17 Correct 61 ms 73976 KB Output is correct
18 Correct 54 ms 74156 KB Output is correct
19 Correct 55 ms 73848 KB Output is correct
20 Correct 51 ms 73472 KB Output is correct
21 Correct 56 ms 74408 KB Output is correct
22 Correct 61 ms 73956 KB Output is correct
23 Correct 66 ms 74104 KB Output is correct
24 Correct 60 ms 73592 KB Output is correct
25 Correct 55 ms 74232 KB Output is correct
26 Correct 64 ms 74232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 71544 KB Output is correct
2 Correct 48 ms 71544 KB Output is correct
3 Correct 49 ms 71544 KB Output is correct
4 Correct 49 ms 71544 KB Output is correct
5 Correct 48 ms 71672 KB Output is correct
6 Correct 49 ms 71544 KB Output is correct
7 Correct 59 ms 71544 KB Output is correct
8 Correct 49 ms 71544 KB Output is correct
9 Correct 49 ms 71672 KB Output is correct
10 Correct 49 ms 71544 KB Output is correct
11 Correct 51 ms 73464 KB Output is correct
12 Correct 52 ms 73600 KB Output is correct
13 Correct 52 ms 73592 KB Output is correct
14 Correct 57 ms 74044 KB Output is correct
15 Correct 54 ms 74232 KB Output is correct
16 Correct 52 ms 73664 KB Output is correct
17 Correct 61 ms 73976 KB Output is correct
18 Correct 54 ms 74156 KB Output is correct
19 Correct 55 ms 73848 KB Output is correct
20 Correct 51 ms 73472 KB Output is correct
21 Correct 56 ms 74408 KB Output is correct
22 Correct 61 ms 73956 KB Output is correct
23 Correct 66 ms 74104 KB Output is correct
24 Correct 60 ms 73592 KB Output is correct
25 Correct 55 ms 74232 KB Output is correct
26 Correct 64 ms 74232 KB Output is correct
27 Correct 166 ms 86392 KB Output is correct
28 Correct 213 ms 86520 KB Output is correct
29 Correct 253 ms 87544 KB Output is correct
30 Correct 244 ms 93032 KB Output is correct
31 Correct 146 ms 102904 KB Output is correct
32 Correct 128 ms 86312 KB Output is correct
33 Correct 102 ms 79608 KB Output is correct
34 Correct 128 ms 93176 KB Output is correct
35 Correct 145 ms 87288 KB Output is correct
36 Correct 185 ms 93176 KB Output is correct
37 Correct 178 ms 101240 KB Output is correct
38 Correct 256 ms 87032 KB Output is correct
39 Correct 332 ms 110200 KB Output is correct
40 Correct 128 ms 98300 KB Output is correct