Submission #636759

# Submission time Handle Problem Language Result Execution time Memory
636759 2022-08-30T03:53:15 Z wyhong3103 Mecho (IOI09_mecho) C++14
100 / 100
568 ms 6516 KB
#include <bits/stdc++.h>
#define all(x) begin(x),end(x)
#define fir first
#define sec second
#define sz(x) x.size()
#define pb push_back
 
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using pdb = pair<double,double>;
using pll = pair<ll,ll>;
using vll = vector<ll>;
const double EPS = (1e-6);
 
void setio(string s){
    freopen((s + ".in").c_str(),"r",stdin);
    freopen((s + ".out").c_str(),"w",stdout);
}

vi dr = {1, -1, 0, 0};
vi dc = {0, 0, 1, -1};


bool valid(int ans, int n, int s,pi m, vector<pi>& hives, vector<string>& grid){
    bool ok = 1;

    //1 = mecho, -1 = bee
    vector<vi> occupy(n, vi(n));
    vector<vi> dist(n, vi (n, -1));

    queue<pi> mecho;
    mecho.push(m);
    dist[m.fir][m.sec] = 0;
    occupy[m.fir][m.sec] = 1;

    queue<pi> bees;
    for(auto& i :hives){
        bees.push(i);
        dist[i.fir][i.sec] = 0;
        occupy[i.fir][i.sec] = -1;
    }

    while (!bees.empty()){
        pi cur = bees.front(); 
        if (dist[cur.fir][cur.sec] == ans){
            break;
        }
        bees.pop();


        for(int i{}; i < 4; i++){
            if (cur.fir+dr[i] < 0 || cur.fir+dr[i] >= n || cur.sec+dc[i] < 0 || cur.sec+dc[i] >= n) continue;
            if (occupy[cur.fir+dr[i]][cur.sec+dc[i]] >= 0 && grid[cur.fir+dr[i]][cur.sec+dc[i]] != 'T' && grid[cur.fir+dr[i]][cur.sec+dc[i]] != 'D'){
                if (cur.fir+dr[i] == m.fir &&  cur.sec+dc[i] == m.sec) ok = 0;
                occupy[cur.fir+dr[i]][cur.sec+dc[i]] = -1;
                dist[cur.fir+dr[i]][cur.sec+dc[i]] = dist[cur.fir][cur.sec]+1;
                bees.push({cur.fir+dr[i], cur.sec+dc[i]});
            }
        } 
    }
    

    

    int cur_time = ans+1;
    bool found = 0;
    

    while (!found && ok && !mecho.empty() && !bees.empty()){
        while (!mecho.empty()){
            pi cur = mecho.front();

            if (occupy[cur.fir][cur.sec] == -1){
                mecho.pop();
                continue;
            }
            if (dist[cur.fir][cur.sec] == (cur_time-ans)*s) break;
            mecho.pop();

            for(int i{}; i < 4; i++){
                if (cur.fir+dr[i] < 0 || cur.fir+dr[i] >= n || cur.sec+dc[i] < 0 || cur.sec+dc[i] >= n) continue;
                if (occupy[cur.fir+dr[i]][cur.sec+dc[i]] == 0 && grid[cur.fir+dr[i]][cur.sec+dc[i]] != 'T'){
                    occupy[cur.fir+dr[i]][cur.sec+dc[i]] = 1;
                    dist[cur.fir+dr[i]][cur.sec+dc[i]] = dist[cur.fir][cur.sec]+1;
                    mecho.push({cur.fir+dr[i], cur.sec+dc[i]});
                }
                if (grid[cur.fir+dr[i]][cur.sec+dc[i]] == 'D'){
                    found = 1;
                    break;
                }
            }
        }

        while (!bees.empty()){
            pi cur = bees.front(); 
            if (dist[cur.fir][cur.sec] == cur_time){
                break;
            }
            bees.pop();

            for(int i{}; i < 4; i++){
                if (cur.fir+dr[i] < 0 || cur.fir+dr[i] >= n || cur.sec+dc[i] < 0 || cur.sec+dc[i] >= n) continue;
                if (occupy[cur.fir+dr[i]][cur.sec+dc[i]] >= 0 && grid[cur.fir+dr[i]][cur.sec+dc[i]] != 'T' && grid[cur.fir+dr[i]][cur.sec+dc[i]] != 'D'){
                    occupy[cur.fir+dr[i]][cur.sec+dc[i]] = -1;
                    dist[cur.fir+dr[i]][cur.sec+dc[i]] = dist[cur.fir][cur.sec]+1;
                    bees.push({cur.fir+dr[i], cur.sec+dc[i]});
                }
            } 
        }

        cur_time++;
    /*
    for(int i{}; i < n; i++){
        for(int j{};j  < n; j++){
            cout << dist[i][j] << '\t';
        }
        cout << '\n';
    }
    cout << '\n';

    for(int i{}; i < n; i++){
        for(int j{};j  < n; j++){
            cout << occupy[i][j] << '\t';
        }
        cout << '\n';
    }
    cout << '\n';
    */
    }

    return (found && ok);
}


void solve(){
    int n, s;
    cin >> n >> s;

    vector<string> grid(n);
    for(auto& i : grid) cin >> i;

    vector<pi> hives;
    pi m;
    for(int i{}; i < n; i++){
        for(int j{};j < n; j++){
            if (grid[i][j] == 'M') m = {i,j};
            else if (grid[i][j] == 'H'){
                hives.pb({i,j});
            }
        }
    }

    int lo = 0, hi = n*n;
    
    while (hi > lo){
        int mid = lo + (hi-lo+1)/2;
        if (valid(mid, n, s, m, hives, grid)){
            lo = mid;
        }else hi = mid-1;
    }

    cout << (valid(lo,n,s,m,hives,grid) ? lo : -1);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    while(t--){
        solve();
    }
    return 0;
}

Compilation message

mecho.cpp: In function 'void setio(std::string)':
mecho.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |     freopen((s + ".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     freopen((s + ".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 410 ms 6220 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 328 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 32 ms 1436 KB Output is correct
34 Correct 30 ms 1416 KB Output is correct
35 Correct 48 ms 1428 KB Output is correct
36 Correct 43 ms 1748 KB Output is correct
37 Correct 42 ms 1748 KB Output is correct
38 Correct 64 ms 1748 KB Output is correct
39 Correct 65 ms 2164 KB Output is correct
40 Correct 57 ms 2160 KB Output is correct
41 Correct 99 ms 2132 KB Output is correct
42 Correct 80 ms 2612 KB Output is correct
43 Correct 70 ms 2600 KB Output is correct
44 Correct 109 ms 2596 KB Output is correct
45 Correct 100 ms 3056 KB Output is correct
46 Correct 96 ms 3052 KB Output is correct
47 Correct 124 ms 3040 KB Output is correct
48 Correct 119 ms 3596 KB Output is correct
49 Correct 107 ms 3664 KB Output is correct
50 Correct 175 ms 3540 KB Output is correct
51 Correct 136 ms 4144 KB Output is correct
52 Correct 125 ms 4264 KB Output is correct
53 Correct 190 ms 4136 KB Output is correct
54 Correct 200 ms 4732 KB Output is correct
55 Correct 144 ms 4792 KB Output is correct
56 Correct 293 ms 4800 KB Output is correct
57 Correct 192 ms 5420 KB Output is correct
58 Correct 165 ms 5392 KB Output is correct
59 Correct 342 ms 5544 KB Output is correct
60 Correct 212 ms 6084 KB Output is correct
61 Correct 209 ms 6080 KB Output is correct
62 Correct 394 ms 6124 KB Output is correct
63 Correct 427 ms 6072 KB Output is correct
64 Correct 438 ms 6232 KB Output is correct
65 Correct 409 ms 6296 KB Output is correct
66 Correct 441 ms 6088 KB Output is correct
67 Correct 424 ms 6168 KB Output is correct
68 Correct 426 ms 6108 KB Output is correct
69 Correct 353 ms 6116 KB Output is correct
70 Correct 437 ms 6100 KB Output is correct
71 Correct 434 ms 6104 KB Output is correct
72 Correct 475 ms 6100 KB Output is correct
73 Correct 411 ms 6512 KB Output is correct
74 Correct 471 ms 6516 KB Output is correct
75 Correct 473 ms 6408 KB Output is correct
76 Correct 568 ms 6388 KB Output is correct
77 Correct 470 ms 6372 KB Output is correct
78 Correct 492 ms 6440 KB Output is correct
79 Correct 466 ms 6336 KB Output is correct
80 Correct 432 ms 6336 KB Output is correct
81 Correct 433 ms 6412 KB Output is correct
82 Correct 454 ms 6516 KB Output is correct
83 Correct 430 ms 6440 KB Output is correct
84 Correct 439 ms 6364 KB Output is correct
85 Correct 479 ms 6308 KB Output is correct
86 Correct 447 ms 6472 KB Output is correct
87 Correct 445 ms 6316 KB Output is correct
88 Correct 435 ms 6272 KB Output is correct
89 Correct 491 ms 6300 KB Output is correct
90 Correct 469 ms 6348 KB Output is correct
91 Correct 401 ms 6240 KB Output is correct
92 Correct 440 ms 6288 KB Output is correct