답안 #978135

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
978135 2024-05-08T22:16:22 Z Bodisha Mecho (IOI09_mecho) C++17
54 / 100
275 ms 7288 KB
#include <bits/stdc++.h>
#define MAX_N 801
#define UNVISITED 0
#define BEAR 1
#define BEE 2

using namespace std;

int n, s;
char grid[MAX_N][MAX_N];
int visited[MAX_N][MAX_N];
int beed[MAX_N][MAX_N];

bool check(int t) {
    pair<int, int> mecho_pos;
    pair<int, int> home_pos;
    vector<pair<int, int>> hive_edges;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            visited[i][j] = UNVISITED;
            beed[i][j] = -69;
            if(grid[i][j] == 'H') {
                hive_edges.push_back({i, j});
            }
            if(grid[i][j] == 'M') {
                mecho_pos = {i, j};
            }
            if(grid[i][j] == 'D') {
                home_pos = {i, j};
            }
        }
    }
    queue<pair<int, int>> beeq;
    vector<pair<int, int>> bees_edge;
    for(auto iter : hive_edges) {
        visited[iter.first][iter.second] = BEE;
        beed[iter.first][iter.second] = 0;
        beeq.push(iter);
    }
    while(!beeq.empty()) {
        pair<int, int> curr = beeq.front();
        beeq.pop();
        if(beed[curr.first][curr.second] == t) {
            bees_edge.push_back(curr);
            continue;
        }
        if(visited[curr.first + 1][curr.second] == UNVISITED && (grid[curr.first + 1][curr.second] == 'G' || grid[curr.first + 1][curr.second] == 'M')) {
            visited[curr.first + 1][curr.second] = BEE;
            beed[curr.first + 1][curr.second] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first + 1, curr.second});
        }
        if(visited[curr.first - 1][curr.second] == UNVISITED && (grid[curr.first - 1][curr.second] == 'G' || grid[curr.first - 1][curr.second] == 'M')) {
            visited[curr.first - 1][curr.second] = BEE;
            beed[curr.first - 1][curr.second] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first - 1, curr.second});
        }
        if(visited[curr.first][curr.second + 1] == UNVISITED && (grid[curr.first][curr.second + 1] == 'G' || grid[curr.first][curr.second + 1] == 'M')) {
            visited[curr.first][curr.second + 1] = BEE;
            beed[curr.first][curr.second + 1] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first, curr.second + 1});
        }
        if(visited[curr.first][curr.second - 1] == UNVISITED && (grid[curr.first][curr.second - 1] == 'G' || grid[curr.first][curr.second - 1] == 'M')) {
            visited[curr.first][curr.second - 1] = BEE;
            beed[curr.first][curr.second - 1] = beed[curr.first][curr.second] + 1;
            beeq.push({curr.first, curr.second - 1});
        }
    }
    if(visited[mecho_pos.first][mecho_pos.second] == BEE) {
        return false;
    }
    set<pair<int, int>> mecho_edge;
    mecho_edge.insert(mecho_pos);
    bool found = false;
    while(!found) {
        if(mecho_edge.size() == 0) {
            return false;
        }
        queue<pair<pair<int, int>, int>> q; // mecho queue
        for(auto iter : mecho_edge) {
            visited[iter.first][iter.second] = BEAR;
            q.push({iter, 0});
        }
        mecho_edge.clear();
        while(!q.empty()) {
            pair<pair<int, int>, int> curr = q.front();
            q.pop();
            if(curr.first.first + 1 < n && visited[curr.first.first + 1][curr.first.second] == UNVISITED && (grid[curr.first.first + 1][curr.first.second] == 'G' || grid[curr.first.first + 1][curr.first.second] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first + 1][curr.first.second] = BEAR;
                    mecho_edge.insert({curr.first.first + 1, curr.first.second});
                } else {
                    visited[curr.first.first + 1][curr.first.second] = BEAR;
                    q.push({{curr.first.first + 1, curr.first.second}, curr.second + 1});
                }
            }
            if(curr.first.first - 1 >= 0 && visited[curr.first.first - 1][curr.first.second] == UNVISITED && (grid[curr.first.first - 1][curr.first.second] == 'G' || grid[curr.first.first - 1][curr.first.second] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first - 1][curr.first.second] = BEAR;
                    mecho_edge.insert({curr.first.first - 1, curr.first.second});
                } else {
                    visited[curr.first.first - 1][curr.first.second] = BEAR;
                    q.push({{curr.first.first - 1, curr.first.second}, curr.second + 1});
                }
            }
            if(curr.first.second + 1 < n && visited[curr.first.first][curr.first.second + 1] == UNVISITED && (grid[curr.first.first][curr.first.second + 1] == 'G' || grid[curr.first.first][curr.first.second + 1] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first][curr.first.second + 1] = BEAR;
                    mecho_edge.insert({curr.first.first, curr.first.second + 1});
                } else {
                    visited[curr.first.first][curr.first.second + 1] = BEAR;
                    q.push({{curr.first.first, curr.first.second + 1}, curr.second + 1});
                }
            }
            if(curr.first.second - 1 >= 0 && visited[curr.first.first][curr.first.second - 1] == UNVISITED && (grid[curr.first.first][curr.first.second - 1] == 'G' || grid[curr.first.first][curr.first.second - 1] == 'D')){
                if(curr.second == s - 1) {
                    visited[curr.first.first][curr.first.second - 1] = BEAR;
                    mecho_edge.insert({curr.first.first, curr.first.second - 1});
                } else {
                    visited[curr.first.first][curr.first.second - 1] = BEAR;
                    q.push({{curr.first.first, curr.first.second - 1}, curr.second + 1});
                }
            }
        }
        if(visited[home_pos.first][home_pos.second] == BEAR) {
            found = true;
            break;
        }
        vector<pair<int, int>> tmpedge;
        for(auto iter : bees_edge) {
            if(iter.first + 1 < n && visited[iter.first + 1][iter.second] == UNVISITED && grid[iter.first + 1][iter.second] == 'G') {
                visited[iter.first + 1][iter.second] = true;
                tmpedge.push_back({iter.first + 1, iter.second});
            }
            if(iter.first - 1 >= 0 && visited[iter.first - 1][iter.second] == UNVISITED && grid[iter.first - 1][iter.second] == 'G') {
                visited[iter.first - 1][iter.second] = true;
                tmpedge.push_back({iter.first - 1, iter.second});
            }
            if(iter.second + 1 < n && visited[iter.first][iter.second + 1] == UNVISITED && grid[iter.first][iter.second + 1] == 'G') {
                visited[iter.first][iter.second + 1] = true;
                tmpedge.push_back({iter.first, iter.second + 1});
            }
            if(iter.second - 1 >= 0 && visited[iter.first][iter.second - 1] == UNVISITED && grid[iter.first][iter.second - 1] == 'G') {
                visited[iter.first][iter.second - 1] = true;
                tmpedge.push_back({iter.first, iter.second - 1});
            }
        }
        bees_edge.clear();
        bees_edge = tmpedge;
    }
    if(found) {
        return true;
    } else {
        return false;
    }
}

int main() {
    cin >> n >> s;
    for(int i = 0; i < n; i++) {
        string tmp;
        cin >> tmp;
        for(int j = 0; j < n; j++) {
            grid[i][j] = tmp[j];
        }
    }
    int l = 0, r = n * n + 1;
    int ans = -1;
    // true true true ... (true) false false
    while(l <= r) {
        int mid = l + (r - l) / 2;
        if(check(mid)) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Incorrect 1 ms 2396 KB Output isn't correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Incorrect 1 ms 2396 KB Output isn't correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 185 ms 6464 KB Output is correct
8 Incorrect 1 ms 2396 KB Output isn't correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Incorrect 1 ms 2396 KB Output isn't correct
16 Correct 1 ms 2396 KB Output is correct
17 Incorrect 1 ms 2396 KB Output isn't correct
18 Correct 1 ms 2396 KB Output is correct
19 Incorrect 1 ms 2392 KB Output isn't correct
20 Correct 1 ms 2396 KB Output is correct
21 Incorrect 1 ms 2396 KB Output isn't correct
22 Correct 1 ms 2652 KB Output is correct
23 Incorrect 1 ms 2652 KB Output isn't correct
24 Correct 1 ms 2652 KB Output is correct
25 Incorrect 1 ms 4700 KB Output isn't correct
26 Correct 1 ms 4700 KB Output is correct
27 Incorrect 1 ms 4696 KB Output isn't correct
28 Correct 2 ms 4696 KB Output is correct
29 Incorrect 2 ms 4700 KB Output isn't correct
30 Correct 1 ms 4700 KB Output is correct
31 Incorrect 2 ms 4700 KB Output isn't correct
32 Correct 1 ms 4696 KB Output is correct
33 Incorrect 21 ms 4812 KB Output isn't correct
34 Correct 18 ms 4956 KB Output is correct
35 Correct 22 ms 4984 KB Output is correct
36 Incorrect 31 ms 4956 KB Output isn't correct
37 Correct 23 ms 4956 KB Output is correct
38 Correct 34 ms 4956 KB Output is correct
39 Incorrect 39 ms 4956 KB Output isn't correct
40 Correct 25 ms 5068 KB Output is correct
41 Correct 37 ms 4956 KB Output is correct
42 Incorrect 47 ms 5100 KB Output isn't correct
43 Correct 35 ms 4952 KB Output is correct
44 Correct 47 ms 5104 KB Output is correct
45 Incorrect 54 ms 5208 KB Output isn't correct
46 Correct 38 ms 4952 KB Output is correct
47 Correct 54 ms 5132 KB Output is correct
48 Incorrect 65 ms 5212 KB Output isn't correct
49 Correct 48 ms 5212 KB Output is correct
50 Correct 66 ms 5328 KB Output is correct
51 Incorrect 77 ms 5536 KB Output isn't correct
52 Correct 55 ms 5468 KB Output is correct
53 Correct 78 ms 5468 KB Output is correct
54 Incorrect 100 ms 5716 KB Output isn't correct
55 Correct 63 ms 5464 KB Output is correct
56 Correct 98 ms 5704 KB Output is correct
57 Incorrect 107 ms 5724 KB Output isn't correct
58 Correct 80 ms 5724 KB Output is correct
59 Correct 109 ms 5724 KB Output is correct
60 Incorrect 120 ms 5976 KB Output isn't correct
61 Correct 82 ms 5980 KB Output is correct
62 Correct 123 ms 6100 KB Output is correct
63 Correct 250 ms 5976 KB Output is correct
64 Correct 216 ms 6092 KB Output is correct
65 Correct 263 ms 6224 KB Output is correct
66 Incorrect 272 ms 6092 KB Output isn't correct
67 Correct 275 ms 5980 KB Output is correct
68 Correct 220 ms 6104 KB Output is correct
69 Correct 198 ms 5980 KB Output is correct
70 Correct 203 ms 6116 KB Output is correct
71 Correct 235 ms 6284 KB Output is correct
72 Correct 245 ms 6100 KB Output is correct
73 Correct 153 ms 7264 KB Output is correct
74 Correct 175 ms 6236 KB Output is correct
75 Correct 202 ms 6232 KB Output is correct
76 Correct 229 ms 6236 KB Output is correct
77 Correct 181 ms 6388 KB Output is correct
78 Correct 194 ms 7136 KB Output is correct
79 Correct 188 ms 6232 KB Output is correct
80 Correct 194 ms 6480 KB Output is correct
81 Correct 186 ms 6236 KB Output is correct
82 Correct 184 ms 6232 KB Output is correct
83 Correct 199 ms 7288 KB Output is correct
84 Correct 188 ms 6232 KB Output is correct
85 Correct 196 ms 6236 KB Output is correct
86 Correct 202 ms 6236 KB Output is correct
87 Correct 194 ms 6236 KB Output is correct
88 Correct 186 ms 6940 KB Output is correct
89 Correct 183 ms 6236 KB Output is correct
90 Correct 187 ms 6500 KB Output is correct
91 Correct 192 ms 6504 KB Output is correct
92 Correct 218 ms 6752 KB Output is correct