답안 #976012

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976012 2024-05-06T04:59:21 Z Br3ad Mecho (IOI09_mecho) C++17
100 / 100
143 ms 7808 KB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define PF push_front
#define PB push_back
#define MP make_pair
#define max(a, b) ((a > b)? a : b)
#define min(a, b) ((a < b)? a : b)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)

const int N = 8e2 + 5;
const int M = 1e9 + 7; 
const int inf = 0x3f3f3f3f;
const ll int INF = 1e18;

const int xMove[4] = {0, 1, 0, -1};
const int yMove[4] = {1, 0, -1, 0};

int bin(int l, int r, function<bool(int)> f){
    while(l < r){
        int mid = l + (r - l + 1)/2;
        if(f(mid)){
            l = mid;
        }else {
            r = mid - 1;
        }
    }
    return r;
}

int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    // ifstream cin();
    // ofstream cout();
    
    pair<int, int> ini;
    char c[N][N];
    int n, step;
    cin >> n >> step;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> c[i][j];
            if(c[i][j] == 'M') ini = MP(i, j);
        }
    }

    queue<pair<int, int>> bee;
    bool vis_bee[N][N]{false};
    int stepBee[N][N]{0};
    memset(stepBee, inf, sizeof(stepBee));

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(c[i][j] == 'H'){
                bee.push(MP(i, j));
                vis_bee[i][j] = true;
                stepBee[i][j] = 0;
            }
        }
    }

    while(!bee.empty()){
        auto [x, y] = bee.front();
        bee.pop();

        for(int i = 0; i < 4; i++){
            int new_x = x + xMove[i];
            int new_y = y + yMove[i];
            if(new_x < 0 || new_x >= n || new_y < 0 || new_y >= n) continue;
            if(c[new_x][new_y] == 'T' || c[new_x][new_y] == 'D') continue;
            if(vis_bee[new_x][new_y]) continue;

            vis_bee[new_x][new_y] = true;
            stepBee[new_x][new_y] = stepBee[x][y] + 1;
            bee.push(MP(new_x, new_y));
        }
    }
    
    bool vis_bear[N][N]{false};
    int stepBear[N][N]{0};
    int ans = bin(-1, N*N, [&](int time_offset){
        // Initialize
        memset(vis_bear, false, sizeof(vis_bear));
        memset(stepBear, 0, sizeof(stepBear));

        queue<pair<int, int>> bear;
        bear.push(ini);
        vis_bear[ini.f][ini.s] = true;
        if(time_offset >= stepBee[ini.f][ini.s]) return false;

        while(!bear.empty()){
            auto [x, y] = bear.front();
            bear.pop();

            if(c[x][y] == 'D') return true;

            int cur_time = (stepBear[x][y] + 1)/step + time_offset;

            for(int i = 0; i < 4; i++){
                int new_x = x + xMove[i];
                int new_y = y + yMove[i];
                if(new_x < 0 || new_x >= n || new_y < 0 || new_y >= n) continue;
                if(c[new_x][new_y] == 'T' || vis_bear[new_x][new_y]) continue;
                if(cur_time >= stepBee[new_x][new_y]) continue;

                vis_bear[new_x][new_y] = true;
                stepBear[new_x][new_y] = stepBear[x][y] + 1;

                bear.push(MP(new_x, new_y));
            }
        }

        return false;
    });

    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7260 KB Output is correct
2 Correct 5 ms 7420 KB Output is correct
3 Correct 5 ms 7260 KB Output is correct
4 Correct 5 ms 7336 KB Output is correct
5 Correct 6 ms 7260 KB Output is correct
6 Correct 5 ms 7260 KB Output is correct
7 Correct 61 ms 7464 KB Output is correct
8 Correct 5 ms 7256 KB Output is correct
9 Correct 5 ms 7256 KB Output is correct
10 Correct 6 ms 7260 KB Output is correct
11 Correct 5 ms 7424 KB Output is correct
12 Correct 5 ms 7260 KB Output is correct
13 Correct 6 ms 7260 KB Output is correct
14 Correct 5 ms 7260 KB Output is correct
15 Correct 6 ms 7260 KB Output is correct
16 Correct 5 ms 7260 KB Output is correct
17 Correct 5 ms 7260 KB Output is correct
18 Correct 7 ms 7256 KB Output is correct
19 Correct 5 ms 7260 KB Output is correct
20 Correct 5 ms 7260 KB Output is correct
21 Correct 7 ms 7256 KB Output is correct
22 Correct 6 ms 7260 KB Output is correct
23 Correct 6 ms 7256 KB Output is correct
24 Correct 5 ms 7260 KB Output is correct
25 Correct 5 ms 7260 KB Output is correct
26 Correct 7 ms 7260 KB Output is correct
27 Correct 6 ms 7432 KB Output is correct
28 Correct 5 ms 7260 KB Output is correct
29 Correct 6 ms 7260 KB Output is correct
30 Correct 7 ms 7204 KB Output is correct
31 Correct 5 ms 7260 KB Output is correct
32 Correct 6 ms 7260 KB Output is correct
33 Correct 7 ms 7260 KB Output is correct
34 Correct 7 ms 7424 KB Output is correct
35 Correct 20 ms 7260 KB Output is correct
36 Correct 9 ms 7256 KB Output is correct
37 Correct 9 ms 7428 KB Output is correct
38 Correct 25 ms 7260 KB Output is correct
39 Correct 9 ms 7428 KB Output is correct
40 Correct 9 ms 7256 KB Output is correct
41 Correct 26 ms 7260 KB Output is correct
42 Correct 11 ms 7428 KB Output is correct
43 Correct 9 ms 7260 KB Output is correct
44 Correct 33 ms 7412 KB Output is correct
45 Correct 10 ms 7260 KB Output is correct
46 Correct 10 ms 7676 KB Output is correct
47 Correct 56 ms 7256 KB Output is correct
48 Correct 11 ms 7256 KB Output is correct
49 Correct 11 ms 7260 KB Output is correct
50 Correct 47 ms 7408 KB Output is correct
51 Correct 13 ms 7256 KB Output is correct
52 Correct 13 ms 7260 KB Output is correct
53 Correct 65 ms 7408 KB Output is correct
54 Correct 15 ms 7260 KB Output is correct
55 Correct 14 ms 7424 KB Output is correct
56 Correct 79 ms 7516 KB Output is correct
57 Correct 15 ms 7256 KB Output is correct
58 Correct 21 ms 7256 KB Output is correct
59 Correct 73 ms 7408 KB Output is correct
60 Correct 16 ms 7260 KB Output is correct
61 Correct 15 ms 7260 KB Output is correct
62 Correct 96 ms 7260 KB Output is correct
63 Correct 77 ms 7256 KB Output is correct
64 Correct 143 ms 7260 KB Output is correct
65 Correct 123 ms 7256 KB Output is correct
66 Correct 107 ms 7584 KB Output is correct
67 Correct 96 ms 7408 KB Output is correct
68 Correct 44 ms 7468 KB Output is correct
69 Correct 38 ms 7260 KB Output is correct
70 Correct 29 ms 7260 KB Output is correct
71 Correct 32 ms 7260 KB Output is correct
72 Correct 26 ms 7428 KB Output is correct
73 Correct 27 ms 7516 KB Output is correct
74 Correct 51 ms 7708 KB Output is correct
75 Correct 67 ms 7512 KB Output is correct
76 Correct 52 ms 7512 KB Output is correct
77 Correct 53 ms 7512 KB Output is correct
78 Correct 64 ms 7772 KB Output is correct
79 Correct 38 ms 7688 KB Output is correct
80 Correct 45 ms 7528 KB Output is correct
81 Correct 51 ms 7516 KB Output is correct
82 Correct 47 ms 7660 KB Output is correct
83 Correct 59 ms 7512 KB Output is correct
84 Correct 57 ms 7620 KB Output is correct
85 Correct 56 ms 7516 KB Output is correct
86 Correct 54 ms 7808 KB Output is correct
87 Correct 56 ms 7512 KB Output is correct
88 Correct 63 ms 7420 KB Output is correct
89 Correct 70 ms 7564 KB Output is correct
90 Correct 60 ms 7572 KB Output is correct
91 Correct 60 ms 7516 KB Output is correct
92 Correct 63 ms 7516 KB Output is correct