Submission #251434

# Submission time Handle Problem Language Result Execution time Memory
251434 2020-07-21T09:07:32 Z Vladikus004 Portal (COCI17_portal) C++14
120 / 120
77 ms 1912 KB
#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

const int N = 500 + 3;
int n, m, sx, sy, fx, fy, d[N][N];
string a[N];

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            d[i][j] = inf;
            if (a[i][j] == 'C'){
                sx = i; sy = j;
            }
            if (a[i][j] == 'F'){
                fx = i; fy = j;
            }
        }
    }
    set <pii> q;
    d[sx][sy] = 0;
    q.insert({0, sx * m + sy});
    while (!q.empty()){
        int x = q.begin()->second / m, y = q.begin()->second % m;
        q.erase(q.begin());
        for (int i = 0; i < 4; i++){
            int X = x + dx[i], Y = y + dy[i];
            if (a[X][Y] == '#') {
                continue;
            }
            if (d[X][Y] <= d[x][y] + 1) continue;
            q.erase({d[X][Y], X * m + Y});
            d[X][Y] = d[x][y] + 1;
            q.insert({d[X][Y], X * m + Y});
        }

        int X = x, Y = y, mn = inf;
        while (a[X][Y] != '#') X++;
        mn = min(mn, abs(X - x));
        X = x; Y = y;
        while (a[X][Y] != '#') X--;
        mn = min(mn, abs(X - x));
        X = x; Y = y;
        while (a[X][Y] != '#') Y++;
        mn = min(mn, abs(Y - y));
        X = x; Y = y;
        while (a[X][Y] != '#') Y--;
        mn = min(mn, abs(Y - y));

            X = x, Y = y;
            while (a[X][Y] != '#'){
                X++;
            }
            X--;
            if (d[X][Y] > d[x][y] + mn){
                q.erase({d[X][Y], X * m + Y});
                d[X][Y] = d[x][y] + mn;
                q.insert({d[X][Y], X * m + Y});
            }
            while (a[X][Y] != '#'){
                X--;
            }
            X++;
            if (d[X][Y] > d[x][y] + mn){
                q.erase({d[X][Y], X * m + Y});
                d[X][Y] = d[x][y] + mn;
                q.insert({d[X][Y], X * m + Y});
            }
            X = x, Y = y;
            while (a[X][Y] != '#'){
                Y++;
            }
            Y--;
            if (d[X][Y] > d[x][y] + mn){
                q.erase({d[X][Y], X * m + Y});
                d[X][Y] = d[x][y] + mn;
                q.insert({d[X][Y], X * m + Y});
            }
            while (a[X][Y] != '#'){
                Y--;
            }
            Y++;
            if (d[X][Y] > d[x][y] + mn){
                q.erase({d[X][Y], X * m + Y});
                d[X][Y] = d[x][y] + mn;
                q.insert({d[X][Y], X * m + Y});
            }
    }
    if (d[fx][fy] == inf){
        cout << "nemoguce";
    }else{
        cout << d[fx][fy];
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 768 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
3 Correct 9 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 1408 KB Output is correct
2 Correct 27 ms 1024 KB Output is correct
3 Correct 20 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 1716 KB Output is correct
2 Correct 3 ms 1536 KB Output is correct
3 Correct 34 ms 1272 KB Output is correct
4 Correct 29 ms 1280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 1784 KB Output is correct
2 Correct 3 ms 1536 KB Output is correct
3 Correct 59 ms 1912 KB Output is correct
4 Correct 67 ms 1792 KB Output is correct