Submission #843761

# Submission time Handle Problem Language Result Execution time Memory
843761 2023-09-04T14:23:20 Z saturina Mecho (IOI09_mecho) C++14
84 / 100
580 ms 7676 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)
{
    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;
                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 = n * n;
    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';
//    }
//    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:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 2 ms 4700 KB Output is correct
4 Correct 1 ms 4904 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 129 ms 7416 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Incorrect 1 ms 4700 KB Output isn't correct
13 Incorrect 2 ms 4700 KB Output isn't correct
14 Correct 2 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 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 2 ms 4700 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 2 ms 4700 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 5 ms 4956 KB Output is correct
34 Correct 4 ms 4956 KB Output is correct
35 Correct 14 ms 5208 KB Output is correct
36 Correct 6 ms 5212 KB Output is correct
37 Correct 6 ms 5212 KB Output is correct
38 Correct 18 ms 5212 KB Output is correct
39 Correct 5 ms 5484 KB Output is correct
40 Correct 7 ms 5468 KB Output is correct
41 Correct 24 ms 5488 KB Output is correct
42 Correct 7 ms 5724 KB Output is correct
43 Correct 8 ms 5724 KB Output is correct
44 Correct 33 ms 5776 KB Output is correct
45 Correct 7 ms 5980 KB Output is correct
46 Correct 9 ms 5968 KB Output is correct
47 Correct 37 ms 5980 KB Output is correct
48 Correct 9 ms 6236 KB Output is correct
49 Correct 16 ms 6096 KB Output is correct
50 Correct 40 ms 6604 KB Output is correct
51 Correct 11 ms 6492 KB Output is correct
52 Correct 17 ms 6492 KB Output is correct
53 Correct 49 ms 6492 KB Output is correct
54 Correct 13 ms 6748 KB Output is correct
55 Correct 18 ms 6716 KB Output is correct
56 Correct 57 ms 6748 KB Output is correct
57 Correct 12 ms 7004 KB Output is correct
58 Correct 15 ms 7004 KB Output is correct
59 Correct 72 ms 7252 KB Output is correct
60 Correct 13 ms 7252 KB Output is correct
61 Correct 18 ms 7260 KB Output is correct
62 Correct 72 ms 7260 KB Output is correct
63 Correct 114 ms 7428 KB Output is correct
64 Correct 175 ms 7408 KB Output is correct
65 Correct 138 ms 7508 KB Output is correct
66 Correct 125 ms 7252 KB Output is correct
67 Correct 119 ms 7252 KB Output is correct
68 Correct 190 ms 7508 KB Output is correct
69 Correct 185 ms 7252 KB Output is correct
70 Correct 182 ms 7408 KB Output is correct
71 Correct 181 ms 7252 KB Output is correct
72 Incorrect 174 ms 7252 KB Output isn't correct
73 Incorrect 527 ms 7412 KB Output isn't correct
74 Correct 541 ms 7460 KB Output is correct
75 Correct 564 ms 7412 KB Output is correct
76 Correct 539 ms 7628 KB Output is correct
77 Correct 580 ms 7508 KB Output is correct
78 Correct 463 ms 7508 KB Output is correct
79 Correct 466 ms 7508 KB Output is correct
80 Correct 448 ms 7252 KB Output is correct
81 Correct 447 ms 7416 KB Output is correct
82 Correct 460 ms 7676 KB Output is correct
83 Correct 183 ms 7420 KB Output is correct
84 Correct 177 ms 7460 KB Output is correct
85 Correct 200 ms 7412 KB Output is correct
86 Correct 185 ms 7420 KB Output is correct
87 Correct 184 ms 7252 KB Output is correct
88 Correct 154 ms 7252 KB Output is correct
89 Correct 146 ms 7252 KB Output is correct
90 Correct 153 ms 7252 KB Output is correct
91 Correct 152 ms 7252 KB Output is correct
92 Correct 151 ms 7252 KB Output is correct