Submission #155437

# Submission time Handle Problem Language Result Execution time Memory
155437 2019-09-28T06:08:29 Z karma Mecho (IOI09_mecho) C++14
100 / 100
826 ms 6676 KB
#include<bits/stdc++.h>
#define pb      emplace_back
#define mp      make_pair
#define fi      first
#define se      second
#define ll      long long

using namespace std;

//T cay, M pos, D nha, G co, H ong

const int N = 803;
const int oo = int(1e9);
typedef pair<int, int> pii;

int lv[N][N], n, s;
bool vis[N][N];
string a[N], org[N];
queue<pii> q;
vector<pii> honey, cur, _cur, _honey;
pii st, en;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool Inside(int i, int j) {return i >= 1 && i <= n && j >= 1 && j <= n;}

int deg(int i, int j) {
    int x = 0; pii nxt;
    for(int k = 0; k < 4; ++k) {
        nxt = mp(i + dx[k], j + dy[k]);
        if(Inside(nxt.fi, nxt.se) && a[nxt.fi][nxt.se] != 'H'
                && a[nxt.fi][nxt.se] != 'T' && a[nxt.fi][nxt.se] != 'D') ++x;
    }
    return x;
}

bool Check(int mid) {
    pii top, nxt;
    while(q.size()) q.pop();
    for(int i = 1; i <= n; ++i) {
        a[i] = org[i];
        for(int j = 1; j <= n; ++j) {
            if(a[i][j] == 'H') q.push(mp(i, j));
            lv[i][j] = 0;
            if(a[i][j] == 'T' || a[i][j] == 'H') vis[i][j] = 1;
            else vis[i][j] = 0;
        }
    }
    if(mid) {
        while(q.size()) {
            top = q.front(); q.pop();
            for(int i = 0; i < 4; ++i) {
               nxt = mp(top.fi + dx[i], top.se + dy[i]);
               if(Inside(nxt.fi, nxt.se) && !vis[nxt.fi][nxt.se] && a[nxt.fi][nxt.se] != 'D') {
                  if((lv[nxt.fi][nxt.se] = lv[top.fi][top.se] + 1) < mid) q.push(nxt);
                  a[nxt.fi][nxt.se] = 'H'; vis[nxt.fi][nxt.se] = 1;
               }
            }
        }
    }
    _honey.clear();
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            if(a[i][j] == 'H') {
               if(deg(i, j)) _honey.pb(i, j);
            }
        }
    }
    if(a[st.fi][st.se] == 'H') return 0;
    _cur.clear(); _cur.pb(st); bool can = 1; vis[st.fi][st.se] = 1;
    while(can) { can = 0;
       for(int j = 1; j <= s; ++j) {
           if(_cur.empty()) break;
           cur.clear(); cur = _cur; _cur.clear(); can = 1;
           for(pii p: cur) {
              if(a[p.fi][p.se] == 'H') continue;
              for(int k = 0; k < 4; ++k) {
                 nxt = mp(p.fi + dx[k], p.se + dy[k]);
                 if(Inside(nxt.fi, nxt.se) && !vis[nxt.fi][nxt.se]) {
                    vis[nxt.fi][nxt.se] = 1; _cur.pb(nxt);
                    if(nxt == en) return 1;
                 }
              }
           }
       }
       honey.clear(); honey = _honey, _honey.clear();
       for(pii p: honey) {
          for(int k = 0; k < 4; ++k) {
             nxt = mp(p.fi + dx[k], p.se + dy[k]);
             if(Inside(nxt.fi, nxt.se) && a[nxt.fi][nxt.se] != 'H' && a[nxt.fi][nxt.se] != 'T' && a[nxt.fi][nxt.se] != 'D') {
                if(deg(nxt.fi, nxt.se)) _honey.pb(nxt);
                vis[nxt.fi][nxt.se] = 1; a[nxt.fi][nxt.se] = 'H';
             }
          }
       }
    }
    return 0;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    if(fopen("honey.inp", "r")) {
        freopen("honey.inp", "r", stdin);
        freopen("honey.out", "w", stdout);
    }
    cin >> n >> s;
    for(int i = 1; i <= n; ++i) {
        cin >> org[i]; org[i] = ' ' + org[i];
        for(int j = 1; j <= n; ++j) {
            if(org[i][j] == 'M') st = mp(i, j);
            if(org[i][j] == 'D') en = mp(i, j);
        }
    }
    int low = 0, high = int(n * n * 2), mid;
    while(low <= high) {
        mid = (low + high) >> 1;
        if(Check(mid)) low = mid + 1;
        else high = mid - 1;
    }
    cout << high;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("honey.inp", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("honey.out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 651 ms 5776 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 3 ms 632 KB Output is correct
13 Correct 4 ms 632 KB Output is correct
14 Correct 5 ms 632 KB Output is correct
15 Correct 3 ms 504 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 2 ms 632 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 632 KB Output is correct
22 Correct 4 ms 644 KB Output is correct
23 Correct 3 ms 636 KB Output is correct
24 Correct 3 ms 636 KB Output is correct
25 Correct 4 ms 636 KB Output is correct
26 Correct 4 ms 632 KB Output is correct
27 Correct 4 ms 632 KB Output is correct
28 Correct 4 ms 760 KB Output is correct
29 Correct 5 ms 632 KB Output is correct
30 Correct 5 ms 632 KB Output is correct
31 Correct 5 ms 636 KB Output is correct
32 Correct 5 ms 760 KB Output is correct
33 Correct 55 ms 2168 KB Output is correct
34 Correct 58 ms 2296 KB Output is correct
35 Correct 69 ms 2192 KB Output is correct
36 Correct 75 ms 2424 KB Output is correct
37 Correct 79 ms 2424 KB Output is correct
38 Correct 88 ms 2552 KB Output is correct
39 Correct 97 ms 2936 KB Output is correct
40 Correct 98 ms 2936 KB Output is correct
41 Correct 115 ms 2936 KB Output is correct
42 Correct 124 ms 3064 KB Output is correct
43 Correct 119 ms 3160 KB Output is correct
44 Correct 155 ms 3296 KB Output is correct
45 Correct 152 ms 3500 KB Output is correct
46 Correct 142 ms 3500 KB Output is correct
47 Correct 168 ms 3512 KB Output is correct
48 Correct 183 ms 3832 KB Output is correct
49 Correct 182 ms 3924 KB Output is correct
50 Correct 215 ms 3904 KB Output is correct
51 Correct 224 ms 4256 KB Output is correct
52 Correct 220 ms 4272 KB Output is correct
53 Correct 259 ms 4348 KB Output is correct
54 Correct 249 ms 4728 KB Output is correct
55 Correct 249 ms 4728 KB Output is correct
56 Correct 300 ms 4540 KB Output is correct
57 Correct 291 ms 4984 KB Output is correct
58 Correct 284 ms 4984 KB Output is correct
59 Correct 347 ms 4984 KB Output is correct
60 Correct 341 ms 5368 KB Output is correct
61 Correct 323 ms 5368 KB Output is correct
62 Correct 404 ms 5492 KB Output is correct
63 Correct 582 ms 5496 KB Output is correct
64 Correct 570 ms 5492 KB Output is correct
65 Correct 544 ms 5500 KB Output is correct
66 Correct 605 ms 5496 KB Output is correct
67 Correct 590 ms 5500 KB Output is correct
68 Correct 695 ms 5704 KB Output is correct
69 Correct 653 ms 5596 KB Output is correct
70 Correct 690 ms 5624 KB Output is correct
71 Correct 737 ms 5624 KB Output is correct
72 Correct 826 ms 5496 KB Output is correct
73 Correct 564 ms 6676 KB Output is correct
74 Correct 680 ms 5828 KB Output is correct
75 Correct 707 ms 5776 KB Output is correct
76 Correct 720 ms 5852 KB Output is correct
77 Correct 678 ms 5880 KB Output is correct
78 Correct 650 ms 6516 KB Output is correct
79 Correct 685 ms 5752 KB Output is correct
80 Correct 701 ms 5852 KB Output is correct
81 Correct 694 ms 5752 KB Output is correct
82 Correct 681 ms 5752 KB Output is correct
83 Correct 657 ms 6352 KB Output is correct
84 Correct 714 ms 5724 KB Output is correct
85 Correct 706 ms 5752 KB Output is correct
86 Correct 673 ms 5752 KB Output is correct
87 Correct 681 ms 5880 KB Output is correct
88 Correct 661 ms 6132 KB Output is correct
89 Correct 657 ms 5752 KB Output is correct
90 Correct 643 ms 5832 KB Output is correct
91 Correct 651 ms 6020 KB Output is correct
92 Correct 686 ms 6072 KB Output is correct