답안 #236461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
236461 2020-06-02T10:28:50 Z Vimmer Portal (COCI17_portal) C++14
120 / 120
36 ms 6144 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 50001
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9

using namespace std;
//using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 2> a2;

//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;



bool mk[501][501];

queue <a2> qr;

int dst[501][501], l[501][501], r[501][501], u[501][501], d[501][501];

int a[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int rst(int x, int y, int sx, int sy) {return abs(sx - x) + abs(sy - y);}

int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, m;

    cin >> n >> m;

    string s[n];

    for (int i = 0; i < n; i++) cin >> s[i];

    int sx, sy, fx, fy;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
          if (s[i][j] == 'C') {sx = i; sy = j;}
            else if (s[i][j] == 'F') {fx = i; fy = j;}

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (j != 0) l[i][j] = l[i][j - 1];

            if (s[i][j] == '#') l[i][j] = j;
        }

        for (int j = m - 1; j >= 0; j--)
        {
            if (j + 1 != m) r[i][j] = r[i][j + 1];

            if (s[i][j] == '#') r[i][j] = j;
        }
    }

    for (int j = 0; j < m; j++)
    {
        for (int i = 0; i < n; i++)
        {
            if (i != 0) u[i][j] = u[i - 1][j];

            if (s[i][j] == '#') u[i][j] = i;
        }

        for (int i = n - 1; i >= 0; i--)
        {
            if (i + 1 != n) d[i][j] = d[i + 1][j];

            if (s[i][j] == '#') d[i][j] = i;
        }
    }

    qr.push({sx, sy});

    mk[sx][sy] = 1;

    dst[sx][sy] = 0;

    while (sz(qr) > 0)
    {
        int x = qr.front()[0], y = qr.front()[1];

        qr.pop();

        for (int i = 0; i < 4; i++)
        {
            int cx = x + a[i][0];

            int cy = y + a[i][1];

            if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + 1 || !mk[cx][cy]) && s[cx][cy] != '#')
            {
                mk[cx][cy] = 1;

                dst[cx][cy] = dst[x][y] + 1;

                qr.push({cx, cy});
            }
        }
            int mn = min(rst(x, y, x, l[x][y]), min(rst(x, y, x, r[x][y]), min(rst(x, y, u[x][y], y), rst(x, y, d[x][y], y))));

            int cx = x, cy = l[x][y] + 1;

            if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
            {
                mk[cx][cy] = 1;

                dst[cx][cy] = dst[x][y] + mn;

                qr.push({cx, cy});
            }

            cy = r[x][y] - 1;

            if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
            {
                mk[cx][cy] = 1;

                dst[cx][cy] = dst[x][y] + mn;

                qr.push({cx, cy});
            }

            cy = y; cx = u[x][y] + 1;

            if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
            {
                mk[cx][cy] = 1;

                dst[cx][cy] = dst[x][y] + mn;

                qr.push({cx, cy});
            }

            cx = d[x][y] - 1;

            if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
            {
                mk[cx][cy] = 1;

                dst[cx][cy] = dst[x][y] + mn;

                qr.push({cx, cy});
            }
    }

    if (!mk[fx][fy]) cout << "nemoguce" << endl;
      else cout << dst[fx][fy];
}

Compilation message

portal.cpp: In function 'int main()':
portal.cpp:100:17: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
     dst[sx][sy] = 0;
     ~~~~~~~~~~~~^~~
portal.cpp:171:30: warning: 'fy' may be used uninitialized in this function [-Wmaybe-uninitialized]
       else cout << dst[fx][fy];
                              ^
portal.cpp:100:17: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
     dst[sx][sy] = 0;
     ~~~~~~~~~~~~^~~
portal.cpp:171:30: warning: 'fx' may be used uninitialized in this function [-Wmaybe-uninitialized]
       else cout << dst[fx][fy];
                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2560 KB Output is correct
2 Correct 5 ms 1280 KB Output is correct
3 Correct 6 ms 1408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 2048 KB Output is correct
2 Correct 6 ms 2048 KB Output is correct
3 Correct 7 ms 1664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 4608 KB Output is correct
2 Correct 15 ms 3200 KB Output is correct
3 Correct 11 ms 3636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 5368 KB Output is correct
2 Correct 12 ms 4736 KB Output is correct
3 Correct 14 ms 3712 KB Output is correct
4 Correct 14 ms 4736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 6144 KB Output is correct
2 Correct 13 ms 4864 KB Output is correct
3 Correct 21 ms 6016 KB Output is correct
4 Correct 26 ms 6016 KB Output is correct