답안 #844664

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844664 2023-09-05T16:24:35 Z saturina Mecho (IOI09_mecho) C++14
100 / 100
691 ms 7516 KB
//made by Nguyễn Hồng Nam
#include <bits/stdc++.h>
using namespace std;
#define ll int
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pll pair<ll,ll>
#define fi first
#define se second
const ll mod = 1e9 + 7,inf = 1e9,mxn = 805;
string s[mxn];
ll n,step,ans = inf,bear[mxn][mxn],bee[mxn][mxn],row[4] = {-1,0,0,1},col[4] = {0,-1,1,0},inf2;
pll start,en;
bool mark[mxn][mxn];
void minz(ll &a,ll b)
{
    if(a > b ) a = b;
}
bool check_bee(ll a,ll b)
{
    if(a >= 0 && a < n && b >= 0 && b < n && s[a][b] != 'T' && s[a][b] != 'D' && s[a][b] != 'H') return true;
    return 0;
}
bool check_bear(ll a,ll b)
{
    if(a >= 0 && a < n && b >= 0 && b < n && s[a][b] != 'T' && s[a][b] != 'H' && mark[a][b] == 0) return true;
    return 0;
}
void bfs_hive(ll i,ll j)
{
    if(bee[i][j] == 0) return;
    memset(mark,false,sizeof mark);
    bee[i][j] = 0;
    mark[i][j] = 1;
    queue<pll> q;
    q.push({i,j});
    while(!q.empty())
    {
        pll u = q.front();
        q.pop();
        mark[u.fi][u.se] = 1;
        for(ll k = 0; k < 4; k++)
        {
            ll a = u.fi + row[k],b = u.se + col[k];
            if(check_bee(a,b) && bee[u.fi][u.se] + 1 < bee[a][b])
            {
                bee[a][b] = bee[u.fi][u.se] + 1;
                mark[a][b] = 1;
                q.push({a,b});
            }
        }
    }
}
void bfs(ll i,ll j )
{
    memset(mark,0,sizeof mark);
    mark[i][j] = 1;
    queue<pll> q;
    q.push({i,j});
    bear[i][j] = 0;
    while(!q.empty())
    {
        pll u = q.front();
        mark[u.fi][u.se] = 1;
        q.pop();
        for(ll k = 0; k < 4; k++)
        {
            ll a = u.fi + row[k],b = u.se + col[k];
            if(check_bear(a,b))
            {
                mark[a][b] = 1;
                bear[a][b] = bear[u.fi][u.se] + 1;
                q.push({a,b});
                //if(a == en.fi && b == en.se) return;
            }
        }
    }
}
bool f(ll x)
{
    if(x >= bee[start.fi][start.se]) return 0;
    memset(mark,0,sizeof mark);
    mark[start.fi][start.se] = 1;
    queue<pair<pll,ll>>q;
    q.push({start,0});
    while(!q.empty())
    {
        auto u = q.front();
        q.pop();
        mark[u.fi.fi][u.fi.se] = 1;
        for(ll i = 0; i < 4; i++)
        {
            ll a = u.fi.fi + row[i],b = u.fi.se + col[i];
            if(mark[a][b] == 0 && a >= 0 && a < n && b >= 0 && b < n && s[a][b] != 'T' && s[a][b] != 'H' && (x + ((u.se + 1) / step)) < bee[a][b])
            {
                mark[a][b] = 1;
                if(mark[en.fi][en.se] == 1) return 1;
                q.push({{a,b},u.se + 1});
            }
        }
    }
    if(mark[en.fi][en.se] == 1) return 1;
    return 0;
}
int main()
{
#define taskname "mecho"
    if (fopen(taskname".inp", "r"))
    {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> step;
    for(ll i = 0; i < n; i++)
    {
        cin >> s[i];
        for(ll j = 0; j < n; j++)
        {
            if(s[i][j] == 'M') start = {i,j};
            else if(s[i][j] == 'D') en = {i,j};
        }
    }
    memset(bee,60,sizeof bee);
    inf2 = bee[0][0];
    for(ll i = 0; i < n; i++)
    {
        for(ll j = 0; j < n; j++) if(s[i][j] == 'H') bfs_hive(i,j);
    }
    bfs(start.fi,start.se);
    ll lf = 0,rt = inf - 1;
    while(lf <= rt)
    {
        ll mid = (lf + rt)/2;
        if(f(mid))
        {
            ans = mid;
            lf = mid + 1;
        }
        else rt = mid - 1;
    }
//    for(ll i = 0; i < n; i++)
//    {
//        for(ll j = 0; j < n; j++)
//        {
//            if(bee[i][j] != inf2) cout << bee[i][j] << ' ';
//            else cout << "-1 ";
//        }
//        cout << '\n';
//    }
    bee[en.fi][en.se] = inf2;
//    for(ll i = 0; i < n; i++)
//    {
//        for(ll j = 0; j < n; j++)
//        {
//            if(bee[i][j] != inf2) cout << bear[i][j] << ' ';
//            else cout << "-1 ";
//        }
//        cout << '\n';
//    }
    if(ans == inf) cout << "-1\n";
    else cout << ans << '\n';
    return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 146 ms 7448 KB Output is correct
8 Correct 1 ms 4696 KB Output is correct
9 Correct 1 ms 4696 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 3 ms 4700 KB Output is correct
14 Correct 3 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4696 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Correct 1 ms 4700 KB Output is correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 3 ms 4956 KB Output is correct
34 Correct 3 ms 4956 KB Output is correct
35 Correct 14 ms 4960 KB Output is correct
36 Correct 4 ms 5212 KB Output is correct
37 Correct 4 ms 5212 KB Output is correct
38 Correct 18 ms 5212 KB Output is correct
39 Correct 4 ms 5336 KB Output is correct
40 Correct 5 ms 5464 KB Output is correct
41 Correct 23 ms 5472 KB Output is correct
42 Correct 5 ms 5720 KB Output is correct
43 Correct 6 ms 5468 KB Output is correct
44 Correct 31 ms 5740 KB Output is correct
45 Correct 6 ms 5980 KB Output is correct
46 Correct 7 ms 5980 KB Output is correct
47 Correct 36 ms 5980 KB Output is correct
48 Correct 7 ms 6236 KB Output is correct
49 Correct 8 ms 6072 KB Output is correct
50 Correct 43 ms 6272 KB Output is correct
51 Correct 8 ms 6492 KB Output is correct
52 Correct 9 ms 6492 KB Output is correct
53 Correct 57 ms 6532 KB Output is correct
54 Correct 9 ms 6748 KB Output is correct
55 Correct 16 ms 6764 KB Output is correct
56 Correct 58 ms 6828 KB Output is correct
57 Correct 10 ms 6904 KB Output is correct
58 Correct 13 ms 7004 KB Output is correct
59 Correct 67 ms 7004 KB Output is correct
60 Correct 13 ms 7516 KB Output is correct
61 Correct 13 ms 7260 KB Output is correct
62 Correct 78 ms 7448 KB Output is correct
63 Correct 122 ms 7504 KB Output is correct
64 Correct 186 ms 7436 KB Output is correct
65 Correct 171 ms 7436 KB Output is correct
66 Correct 136 ms 7312 KB Output is correct
67 Correct 127 ms 7440 KB Output is correct
68 Correct 198 ms 7296 KB Output is correct
69 Correct 195 ms 7252 KB Output is correct
70 Correct 196 ms 7316 KB Output is correct
71 Correct 186 ms 7248 KB Output is correct
72 Correct 184 ms 7304 KB Output is correct
73 Correct 564 ms 7360 KB Output is correct
74 Correct 598 ms 7308 KB Output is correct
75 Correct 691 ms 7300 KB Output is correct
76 Correct 663 ms 7308 KB Output is correct
77 Correct 684 ms 7312 KB Output is correct
78 Correct 558 ms 7300 KB Output is correct
79 Correct 548 ms 7308 KB Output is correct
80 Correct 565 ms 7300 KB Output is correct
81 Correct 521 ms 7304 KB Output is correct
82 Correct 503 ms 7252 KB Output is correct
83 Correct 210 ms 7320 KB Output is correct
84 Correct 207 ms 7324 KB Output is correct
85 Correct 213 ms 7248 KB Output is correct
86 Correct 213 ms 7304 KB Output is correct
87 Correct 211 ms 7248 KB Output is correct
88 Correct 169 ms 7308 KB Output is correct
89 Correct 166 ms 7308 KB Output is correct
90 Correct 177 ms 7432 KB Output is correct
91 Correct 169 ms 7316 KB Output is correct
92 Correct 170 ms 7248 KB Output is correct