Submission #843765

# Submission time Handle Problem Language Result Execution time Memory
843765 2023-09-04T14:26:16 Z saturina Mecho (IOI09_mecho) C++14
84 / 100
577 ms 7048 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 = 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';
//    }
//    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 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
3 Correct 2 ms 4700 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 1 ms 4696 KB Output is correct
6 Correct 1 ms 4696 KB Output is correct
7 Correct 125 ms 7048 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 4696 KB Output is correct
12 Incorrect 1 ms 4696 KB Output isn't correct
13 Incorrect 3 ms 4696 KB Output isn't correct
14 Correct 2 ms 4696 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 4696 KB Output is correct
18 Correct 1 ms 4696 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4696 KB Output is correct
21 Correct 2 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 4696 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 1 ms 4696 KB Output is correct
27 Correct 1 ms 4952 KB Output is correct
28 Correct 1 ms 4696 KB Output is correct
29 Correct 1 ms 4696 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Correct 2 ms 5116 KB Output is correct
32 Correct 1 ms 4696 KB Output is correct
33 Correct 4 ms 4696 KB Output is correct
34 Correct 5 ms 4700 KB Output is correct
35 Correct 13 ms 4696 KB Output is correct
36 Correct 4 ms 4952 KB Output is correct
37 Correct 4 ms 4952 KB Output is correct
38 Correct 17 ms 4952 KB Output is correct
39 Correct 5 ms 5220 KB Output is correct
40 Correct 6 ms 5208 KB Output is correct
41 Correct 22 ms 5208 KB Output is correct
42 Correct 5 ms 5464 KB Output is correct
43 Correct 9 ms 5468 KB Output is correct
44 Correct 29 ms 5464 KB Output is correct
45 Correct 6 ms 5720 KB Output is correct
46 Correct 7 ms 5464 KB Output is correct
47 Correct 34 ms 5468 KB Output is correct
48 Correct 7 ms 5720 KB Output is correct
49 Correct 9 ms 5724 KB Output is correct
50 Correct 43 ms 5932 KB Output is correct
51 Correct 9 ms 5976 KB Output is correct
52 Correct 11 ms 5976 KB Output is correct
53 Correct 48 ms 5980 KB Output is correct
54 Correct 11 ms 6232 KB Output is correct
55 Correct 13 ms 6232 KB Output is correct
56 Correct 58 ms 6232 KB Output is correct
57 Correct 10 ms 6488 KB Output is correct
58 Correct 12 ms 6488 KB Output is correct
59 Correct 67 ms 6600 KB Output is correct
60 Correct 12 ms 6744 KB Output is correct
61 Correct 15 ms 6744 KB Output is correct
62 Correct 76 ms 6828 KB Output is correct
63 Correct 112 ms 6736 KB Output is correct
64 Correct 158 ms 6824 KB Output is correct
65 Correct 160 ms 6824 KB Output is correct
66 Correct 125 ms 6736 KB Output is correct
67 Correct 118 ms 6772 KB Output is correct
68 Correct 188 ms 6740 KB Output is correct
69 Correct 188 ms 6776 KB Output is correct
70 Correct 182 ms 6776 KB Output is correct
71 Correct 178 ms 6736 KB Output is correct
72 Incorrect 178 ms 6784 KB Output isn't correct
73 Incorrect 505 ms 6780 KB Output isn't correct
74 Correct 542 ms 6776 KB Output is correct
75 Correct 571 ms 6736 KB Output is correct
76 Correct 541 ms 6992 KB Output is correct
77 Correct 577 ms 6992 KB Output is correct
78 Correct 462 ms 6820 KB Output is correct
79 Correct 447 ms 6780 KB Output is correct
80 Correct 443 ms 6736 KB Output is correct
81 Correct 443 ms 6736 KB Output is correct
82 Correct 467 ms 6796 KB Output is correct
83 Correct 184 ms 6668 KB Output is correct
84 Correct 177 ms 6784 KB Output is correct
85 Correct 177 ms 6736 KB Output is correct
86 Correct 184 ms 6740 KB Output is correct
87 Correct 186 ms 6684 KB Output is correct
88 Correct 157 ms 6824 KB Output is correct
89 Correct 151 ms 6736 KB Output is correct
90 Correct 168 ms 6780 KB Output is correct
91 Correct 161 ms 6780 KB Output is correct
92 Correct 151 ms 6832 KB Output is correct