답안 #675277

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
675277 2022-12-27T03:45:51 Z vjudge1 포탈들 (BOI14_portals) C++17
100 / 100
312 ms 9856 KB
#include<bits/stdc++.h>
#define task "A"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
using namespace std;
const int maxN = 1e3 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int m, n;
string a[maxN];
pair<int, int> s, t;
int d[maxN][maxN];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<int> hang[maxN], cot[maxN];

struct TPQRect
{
    int x, y, dlab;
    bool operator < (const TPQRect& other) const
    {
        return other.dlab < dlab;
    }
    bool Valid() const
    {
        return d[x][y] == dlab;
    }
};

void Input()
{
    cin >> m >> n;
    for(int i = 1; i <= m; i++)
    {
        hang[i].pb(0);
        hang[i].pb(n + 1);
    }
    for(int i = 1; i <= n; i++)
    {
        cot[i].pb(0);
        cot[i].pb(m + 1);
    }
    for(int i = 1; i <= m; i++)
    {
        cin >> a[i];
        a[i] = ' ' + a[i];
        for(int j = 1; j <= n; j++)
        {
            if(a[i][j] == 'S')
                s = {i, j};
            if(a[i][j] == 'C')
                t = {i, j};
            if(a[i][j] == '#')
            {
                hang[i].pb(j);
                cot[j].pb(i);
            }
        }
    }
    for(int i = 1; i <= m; i++)
        sort(hang[i].begin(), hang[i].end());
    for(int i = 1; i <= n; i++)
        sort(cot[i].begin(), cot[i].end());
}

void Dijkstra()
{
    priority_queue<TPQRect> pq;
    memset(d, 3, sizeof d);
    d[s.fi][s.se] = 0;
    pq.push({s.fi, s.se, 0});
    while(!pq.empty())
    {
        TPQRect R = pq.top();
        pq.pop();
        if(!R.Valid())
            continue;
        int x = R.x, y = R.y;
        for(int i=0; i<4; i++)
        {
            int u = x + dx[i], v = y + dy[i];
            if(u < 1 || u > m || v < 1 || v > n || a[u][v] == '#')
                continue;
            if(d[u][v] > d[x][y] + 1)
            {
                d[u][v] = d[x][y] + 1;
                pq.push({u, v, d[u][v]});
            }
        }
        vector<pii> vc;
        int id = lower_bound(hang[x].begin(), hang[x].end(), y) - hang[x].begin();
        vc.pb({x, hang[x][id] - 1});
        id--;
        vc.pb({x, hang[x][id] + 1});
        id = lower_bound(cot[y].begin(), cot[y].end(), x) - cot[y].begin();
        vc.pb({cot[y][id] - 1, y});
        id--;
        vc.pb({cot[y][id] + 1, y});
        for(int i=0; i<4; i++)
            for(int j=0; j<4; j++)
            {
                int w = abs(x - vc[j].fi) + abs(y - vc[j].se) + 1;
                if(d[vc[i].fi][vc[i].se] > d[x][y] + w)
                {
                    d[vc[i].fi][vc[i].se] = d[x][y] + w;
                    pq.push({vc[i].fi, vc[i].se, d[vc[i].fi][vc[i].se]});
                }
            }
    }
}

void Solve()
{
    Dijkstra();
    cout << d[t.fi][t.se];
}

signed 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

portals.cpp: In function 'int main()':
portals.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
portals.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         freopen(task".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 3 ms 4308 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 2 ms 4308 KB Output is correct
10 Correct 2 ms 4308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 2 ms 4308 KB Output is correct
8 Correct 2 ms 4376 KB Output is correct
9 Correct 4 ms 4308 KB Output is correct
10 Correct 3 ms 4308 KB Output is correct
11 Correct 2 ms 4308 KB Output is correct
12 Correct 3 ms 4380 KB Output is correct
13 Correct 3 ms 4372 KB Output is correct
14 Correct 2 ms 4248 KB Output is correct
15 Correct 2 ms 4308 KB Output is correct
16 Correct 2 ms 4308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 2 ms 4308 KB Output is correct
5 Correct 9 ms 4532 KB Output is correct
6 Correct 12 ms 4564 KB Output is correct
7 Correct 12 ms 4656 KB Output is correct
8 Correct 10 ms 4564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4248 KB Output is correct
4 Correct 2 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 2 ms 4360 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 3 ms 4372 KB Output is correct
10 Correct 4 ms 4308 KB Output is correct
11 Correct 3 ms 4376 KB Output is correct
12 Correct 3 ms 4308 KB Output is correct
13 Correct 3 ms 4308 KB Output is correct
14 Correct 9 ms 4564 KB Output is correct
15 Correct 11 ms 4676 KB Output is correct
16 Correct 11 ms 4644 KB Output is correct
17 Correct 13 ms 4668 KB Output is correct
18 Correct 13 ms 4564 KB Output is correct
19 Correct 11 ms 4436 KB Output is correct
20 Correct 12 ms 4532 KB Output is correct
21 Correct 9 ms 4644 KB Output is correct
22 Correct 10 ms 4632 KB Output is correct
23 Correct 11 ms 4640 KB Output is correct
24 Correct 11 ms 4436 KB Output is correct
25 Correct 2 ms 4308 KB Output is correct
26 Correct 2 ms 4308 KB Output is correct
27 Correct 2 ms 4308 KB Output is correct
28 Correct 10 ms 4644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4252 KB Output is correct
2 Correct 2 ms 4252 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 3 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 2 ms 4308 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 3 ms 4308 KB Output is correct
10 Correct 3 ms 4308 KB Output is correct
11 Correct 4 ms 4308 KB Output is correct
12 Correct 2 ms 4308 KB Output is correct
13 Correct 4 ms 4308 KB Output is correct
14 Correct 10 ms 4564 KB Output is correct
15 Correct 11 ms 4636 KB Output is correct
16 Correct 12 ms 4692 KB Output is correct
17 Correct 11 ms 4600 KB Output is correct
18 Correct 13 ms 4608 KB Output is correct
19 Correct 13 ms 4440 KB Output is correct
20 Correct 12 ms 4532 KB Output is correct
21 Correct 9 ms 4564 KB Output is correct
22 Correct 10 ms 4680 KB Output is correct
23 Correct 11 ms 4564 KB Output is correct
24 Correct 312 ms 9424 KB Output is correct
25 Correct 261 ms 5944 KB Output is correct
26 Correct 233 ms 5884 KB Output is correct
27 Correct 267 ms 6056 KB Output is correct
28 Correct 188 ms 9828 KB Output is correct
29 Correct 228 ms 9812 KB Output is correct
30 Correct 276 ms 9856 KB Output is correct
31 Correct 11 ms 4392 KB Output is correct
32 Correct 236 ms 5740 KB Output is correct
33 Correct 2 ms 4244 KB Output is correct
34 Correct 3 ms 4308 KB Output is correct
35 Correct 209 ms 7800 KB Output is correct
36 Correct 2 ms 4308 KB Output is correct
37 Correct 8 ms 4692 KB Output is correct
38 Correct 155 ms 9700 KB Output is correct
39 Correct 165 ms 9748 KB Output is correct