답안 #1057790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057790 2024-08-14T06:09:54 Z 12345678 Mecho (IOI09_mecho) C++17
100 / 100
108 ms 6324 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=805;

int n, s, mn[nx][nx], dx[4]={1, 0, 0, -1}, dy[4]={0, 1, -1, 0}, dp[nx][nx], f;
char mp[nx][nx];
pair<int, int> st, tg;
queue<pair<int, int>> q;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>s;
    for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) cin>>mp[i][j], mn[i][j]=1e9;
    for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (mp[i][j]=='M') st={i, j};
    for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (mp[i][j]=='H') q.push({i, j}), mn[i][j]=0;
    for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (mp[i][j]=='D') tg={i, j};
    while (!q.empty())
    {
        auto [x, y]=q.front();
        q.pop();
        for (int i=0; i<4; i++)
        {
            int cx=x+dx[i], cy=y+dy[i];
            if (cx<1||cx>n||cy<1||cy>n||mp[cx][cy]=='T'||mp[cx][cy]=='D') continue;
            if (mn[cx][cy]>mn[x][y]+1) mn[cx][cy]=mn[x][y]+1, q.push({cx, cy});
        }
    }
    //cout<<"debug mn "<<mn[st.first][st.second]<<'\n';
    int l=0, r=n*n;
    while (l<r||r==0)
    {
        int md=(l+r+1)/2;
        for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) dp[i][j]=1e9;
        if (mn[st.first][st.second]<=md)
        {
            r=md-1;
            continue;
        }
        dp[st.first][st.second]=0;
        q.push({st.first, st.second});
        //cout<<"here "<<md<<'\n';
        while (!q.empty())
        {
            auto [x, y]=q.front();
            q.pop();
            for (int i=0; i<4; i++)
            {
                int cx=x+dx[i], cy=y+dy[i];
                if (cx<1||cx>n||cy<1||cy>n||mp[cx][cy]=='T'||mp[cx][cy]=='H') continue;
                if ((dp[cx][cy]>dp[x][y]+1&&(md+(dp[x][y]+1)/s)<mn[cx][cy])||(cx==tg.first&&cy==tg.second)) dp[cx][cy]=dp[x][y]+1, q.push({cx, cy});
            }
        }
        if (r==0)
        {
            if (dp[tg.first][tg.second]!=1e9) f=0;
            else f=1;
            break;
        }
        else
        {
            if (dp[tg.first][tg.second]!=1e9) l=md;
            else r=md-1;
        }
    }
    if (f) cout<<-1;
    else cout<<l;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4452 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 45 ms 6248 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 0 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 0 ms 4444 KB Output is correct
16 Correct 0 ms 4444 KB Output is correct
17 Correct 0 ms 4444 KB Output is correct
18 Correct 0 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 0 ms 4444 KB Output is correct
21 Correct 0 ms 4444 KB Output is correct
22 Correct 0 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4440 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 0 ms 4444 KB Output is correct
29 Correct 0 ms 4444 KB Output is correct
30 Correct 0 ms 4444 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4560 KB Output is correct
33 Correct 3 ms 4700 KB Output is correct
34 Correct 3 ms 4700 KB Output is correct
35 Correct 12 ms 4744 KB Output is correct
36 Correct 6 ms 4700 KB Output is correct
37 Correct 4 ms 4900 KB Output is correct
38 Correct 15 ms 4700 KB Output is correct
39 Correct 7 ms 4956 KB Output is correct
40 Correct 5 ms 4956 KB Output is correct
41 Correct 20 ms 5052 KB Output is correct
42 Correct 7 ms 5212 KB Output is correct
43 Correct 7 ms 5212 KB Output is correct
44 Correct 24 ms 5220 KB Output is correct
45 Correct 8 ms 5212 KB Output is correct
46 Correct 8 ms 5212 KB Output is correct
47 Correct 28 ms 5212 KB Output is correct
48 Correct 9 ms 5468 KB Output is correct
49 Correct 10 ms 5468 KB Output is correct
50 Correct 36 ms 5524 KB Output is correct
51 Correct 11 ms 5468 KB Output is correct
52 Correct 11 ms 5468 KB Output is correct
53 Correct 42 ms 5464 KB Output is correct
54 Correct 12 ms 5720 KB Output is correct
55 Correct 12 ms 5724 KB Output is correct
56 Correct 57 ms 5832 KB Output is correct
57 Correct 13 ms 6000 KB Output is correct
58 Correct 15 ms 5992 KB Output is correct
59 Correct 65 ms 5820 KB Output is correct
60 Correct 15 ms 5980 KB Output is correct
61 Correct 16 ms 5980 KB Output is correct
62 Correct 70 ms 5980 KB Output is correct
63 Correct 75 ms 5980 KB Output is correct
64 Correct 108 ms 6136 KB Output is correct
65 Correct 80 ms 5976 KB Output is correct
66 Correct 69 ms 5976 KB Output is correct
67 Correct 72 ms 6136 KB Output is correct
68 Correct 35 ms 5976 KB Output is correct
69 Correct 29 ms 5980 KB Output is correct
70 Correct 28 ms 5980 KB Output is correct
71 Correct 26 ms 6064 KB Output is correct
72 Correct 22 ms 5980 KB Output is correct
73 Correct 21 ms 6232 KB Output is correct
74 Correct 32 ms 6232 KB Output is correct
75 Correct 39 ms 6196 KB Output is correct
76 Correct 37 ms 6232 KB Output is correct
77 Correct 39 ms 6236 KB Output is correct
78 Correct 45 ms 6236 KB Output is correct
79 Correct 30 ms 6236 KB Output is correct
80 Correct 34 ms 6232 KB Output is correct
81 Correct 39 ms 6236 KB Output is correct
82 Correct 34 ms 6236 KB Output is correct
83 Correct 54 ms 6236 KB Output is correct
84 Correct 41 ms 6232 KB Output is correct
85 Correct 42 ms 6132 KB Output is correct
86 Correct 44 ms 6228 KB Output is correct
87 Correct 41 ms 6236 KB Output is correct
88 Correct 49 ms 6232 KB Output is correct
89 Correct 46 ms 6324 KB Output is correct
90 Correct 49 ms 6160 KB Output is correct
91 Correct 45 ms 6236 KB Output is correct
92 Correct 43 ms 6296 KB Output is correct