답안 #783849

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
783849 2023-07-15T11:24:57 Z VMaksimoski008 Mecho (IOI09_mecho) C++14
84 / 100
183 ms 6876 KB
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <stack>
#include <cstring>
#include <chrono>
#include <random>
#include <numeric>
#include <bitset>
#include <array>
#include <functional>

#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << '\n'
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define each(x, v) for(auto &x : v)
#define mp make_pair

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;

void setIO() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

int n, s;
int dr[4] = {0, -1, 0, 1};
int dc[4] = {-1, 0, 1, 0};
char mat[800][800];
vector<pii> hives;
pii mecho, home;

int dist[800][800][2];
bool vis[800][800];

bool ok(int r, int c) {
    return (r >= 0 && r < n && c >= 0 && c < n && !vis[r][c] && mat[r][c] != 'T');
}

void hive_fill() {
    queue<pair<int, int> > q;
    for(pii &x : hives) {
        q.push(x);
        vis[x.first][x.second] = true;
    }

    while(!q.empty()) {
        int r = q.front().first;
        int c = q.front().second;
        q.pop();

        for(int i=0; i<4; i++) {
            int newR = r + dr[i];
            int newC = c + dc[i];
            if(!ok(newR, newC) || mat[newR][newC] == 'D') continue;
            vis[newR][newC] = true;
            dist[newR][newC][1] = dist[r][c][1] + 1;
            q.push({ newR, newC });
        }
    }
}

int calcMinutes(int steps) {
    return steps / s;
}

bool check(int mid) {
    queue<pii> q;
    memset(vis, 0, sizeof(vis));

    q.push(mecho);
    vis[mecho.first][mecho.second] = true;
    while(!q.empty()) {
        int r = q.front().first;
        int c = q.front().second;
        q.pop();

        if(r == home.first && c == home.second)
            return true;

        for(int i=0; i<4; i++) {
            int newR = r + dr[i];
            int newC = c + dc[i];
            if(!ok(newR, newC)) continue;
            if(mid >= dist[newR][newC][1] && mat[newR][newC] != 'D') 
                continue;
            if(mid + calcMinutes(dist[r][c][0] + 1) >= dist[newR][newC][1] && mat[newR][newC] != 'D')
                continue;
            q.push({ newR, newC });
            vis[newR][newC] = true;
            dist[newR][newC][0] = dist[r][c][0] + 1;
        }
    }

    return false;
}

int32_t main() {
    setIO();

    cin >> n >> s;
    memset(dist, 0, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            cin >> mat[i][j];
            if(mat[i][j] == 'H') hives.pb({i, j});
            else if(mat[i][j] == 'M') mecho = {i, j};
            else if(mat[i][j] == 'D') home = {i, j};
        }
    }

    hive_fill();

    int ans = -1;
    int l=0, r=n*n;
    while(l <= r) {
        int mid = (l + r) / 2;

        if(check(mid)) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }

    // for(int i=0; i<n; i++) {
    //     for(int j=0; j<n; j++) {
    //         cout << dist[i][j][1];
    //         if(j == n-1) cout << '\n';
    //         else cout << " ";
    //     }
    // }

    // cout << "-----------------\n";

    
    // for(int i=0; i<n; i++) {
    //     for(int j=0; j<n; j++) {
    //         cout << dist[i][j][0];
    //         if(j == n-1) cout << '\n';
    //         else cout << " ";
    //     }
    // }

    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5844 KB Output is correct
2 Correct 2 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5844 KB Output is correct
5 Correct 3 ms 5844 KB Output is correct
6 Correct 3 ms 5844 KB Output is correct
7 Correct 76 ms 6680 KB Output is correct
8 Correct 2 ms 5972 KB Output is correct
9 Correct 3 ms 5844 KB Output is correct
10 Correct 3 ms 5992 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Incorrect 3 ms 5972 KB Output isn't correct
13 Incorrect 3 ms 5972 KB Output isn't correct
14 Correct 3 ms 5972 KB Output is correct
15 Correct 2 ms 5972 KB Output is correct
16 Correct 3 ms 5972 KB Output is correct
17 Correct 2 ms 5972 KB Output is correct
18 Correct 2 ms 5972 KB Output is correct
19 Correct 3 ms 5972 KB Output is correct
20 Correct 2 ms 5972 KB Output is correct
21 Correct 2 ms 5972 KB Output is correct
22 Correct 2 ms 5972 KB Output is correct
23 Correct 2 ms 5972 KB Output is correct
24 Correct 2 ms 5972 KB Output is correct
25 Correct 3 ms 5972 KB Output is correct
26 Correct 2 ms 5972 KB Output is correct
27 Correct 3 ms 5972 KB Output is correct
28 Correct 2 ms 5972 KB Output is correct
29 Correct 2 ms 5972 KB Output is correct
30 Correct 2 ms 5972 KB Output is correct
31 Correct 2 ms 5972 KB Output is correct
32 Correct 3 ms 5972 KB Output is correct
33 Correct 5 ms 6228 KB Output is correct
34 Correct 5 ms 6240 KB Output is correct
35 Correct 21 ms 6228 KB Output is correct
36 Correct 5 ms 6228 KB Output is correct
37 Correct 6 ms 6228 KB Output is correct
38 Correct 26 ms 6284 KB Output is correct
39 Correct 6 ms 6228 KB Output is correct
40 Correct 6 ms 6228 KB Output is correct
41 Correct 35 ms 6228 KB Output is correct
42 Correct 7 ms 6228 KB Output is correct
43 Correct 7 ms 6356 KB Output is correct
44 Correct 44 ms 6356 KB Output is correct
45 Correct 8 ms 6300 KB Output is correct
46 Correct 10 ms 6356 KB Output is correct
47 Correct 48 ms 6280 KB Output is correct
48 Correct 9 ms 6356 KB Output is correct
49 Correct 9 ms 6336 KB Output is correct
50 Correct 60 ms 6420 KB Output is correct
51 Correct 10 ms 6356 KB Output is correct
52 Correct 10 ms 6432 KB Output is correct
53 Correct 74 ms 6380 KB Output is correct
54 Correct 11 ms 6484 KB Output is correct
55 Correct 12 ms 6484 KB Output is correct
56 Correct 89 ms 6512 KB Output is correct
57 Correct 13 ms 6484 KB Output is correct
58 Correct 13 ms 6548 KB Output is correct
59 Correct 100 ms 6544 KB Output is correct
60 Correct 14 ms 6484 KB Output is correct
61 Correct 18 ms 6488 KB Output is correct
62 Correct 117 ms 6576 KB Output is correct
63 Correct 109 ms 6496 KB Output is correct
64 Correct 183 ms 6576 KB Output is correct
65 Correct 130 ms 6592 KB Output is correct
66 Correct 103 ms 6568 KB Output is correct
67 Correct 103 ms 6584 KB Output is correct
68 Correct 46 ms 6612 KB Output is correct
69 Correct 44 ms 6612 KB Output is correct
70 Correct 31 ms 6600 KB Output is correct
71 Correct 36 ms 6612 KB Output is correct
72 Incorrect 27 ms 6592 KB Output isn't correct
73 Incorrect 36 ms 6868 KB Output isn't correct
74 Correct 50 ms 6868 KB Output is correct
75 Correct 68 ms 6868 KB Output is correct
76 Correct 57 ms 6876 KB Output is correct
77 Correct 55 ms 6868 KB Output is correct
78 Correct 79 ms 6836 KB Output is correct
79 Correct 43 ms 6836 KB Output is correct
80 Correct 55 ms 6740 KB Output is correct
81 Correct 63 ms 6800 KB Output is correct
82 Correct 53 ms 6740 KB Output is correct
83 Correct 70 ms 6740 KB Output is correct
84 Correct 60 ms 6796 KB Output is correct
85 Correct 67 ms 6792 KB Output is correct
86 Correct 80 ms 6796 KB Output is correct
87 Correct 66 ms 6800 KB Output is correct
88 Correct 73 ms 6748 KB Output is correct
89 Correct 72 ms 6740 KB Output is correct
90 Correct 73 ms 6740 KB Output is correct
91 Correct 72 ms 6740 KB Output is correct
92 Correct 82 ms 6760 KB Output is correct