답안 #395793

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395793 2021-04-28T23:22:10 Z SirCovidThe19th Mecho (IOI09_mecho) C++14
84 / 100
267 ms 6388 KB
#include <bits/stdc++.h>
using namespace std;

int n, s;
int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, -1, 1};
int hiveDist[800][800];
char grid[800][800];

pair<int, int> start;
pair<int, int> dest;
vector<pair<int, int>> hives;

bool inside(int x, int y){
    return x >= 0 and x < n and y >= 0 and y < n;
}
bool checkDone(int x, int y){
    //check if bear is next to his home
    return (abs(x-dest.first) == 1 and y-dest.second == 0) or
           ((x-dest.first) == 0 and abs(y-dest.second) == 1);
}
void bees(){
    queue<pair<int, int>> trav;
    fill(&hiveDist[0][0], &hiveDist[0][0]+sizeof(hiveDist)/sizeof(hiveDist[0][0]), -1);
    for (pair<int, int> loc : hives){
        trav.push(loc); 
        hiveDist[loc.first][loc.second] = 0;
    }
    while (!trav.empty()){
        pair<int, int> curr = trav.front(); trav.pop();
        for (int i = 0; i < 4; i++){
            int x = curr.first+dx[i]; int y = curr.second+dy[i];
            if (inside(x, y) and grid[x][y] == 'G' and hiveDist[x][y] == -1){
                hiveDist[x][y] = hiveDist[curr.first][curr.second]+1;
                trav.push({x, y});
            }
        }
    }
}
bool bear(int wait){
    queue<pair<int, int>> trav;
    //floor(bearDist/s) = time
    int bearDist[n][n];
    fill(&bearDist[0][0], &bearDist[0][0]+sizeof(bearDist)/sizeof(bearDist[0][0]), -1);
    trav.push(start); bearDist[start.first][start.second] = wait*s;

    while (!trav.empty()){
        pair<int, int> curr = trav.front(); trav.pop();
        //smoll bfs on reachable positions
        queue<pair<int, int>> pos; pos.push(curr);
        while (!pos.empty()){
            pair<int, int> loc = pos.front(); pos.pop();
            for (int i = 0; i < 4; i++){
                int x = loc.first+dx[i]; int y = loc.second+dy[i];
                if (inside(x, y) and grid[x][y] == 'G' and bearDist[x][y] == -1){
                    bearDist[x][y] = bearDist[loc.first][loc.second]+1;
                    //uh oh, bees can reach here before you can
                    if (floor(bearDist[x][y]/s) >= hiveDist[x][y] and hiveDist[x][y] != -1) continue;
                    if (checkDone(x, y)) return true;
                    //max distance reached
                    int maxDist = (bearDist[curr.first][curr.second]/s)+1;
                    if (bearDist[x][y] == maxDist)
                        trav.push({x, y});
                    else
                        pos.push({x, y});
                }
            }
        }
    }
    return false;
}
int main() {

    cin >> n >> s;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            cin >> grid[i][j];
            if (grid[i][j] == 'M') start = {i, j};
            if (grid[i][j] == 'D') dest = {i, j};
            if (grid[i][j] == 'H') hives.push_back({i, j});
        }
    }

    bees();
    
    if (!bear(0)){
        cout<<-1; return 0;
    }

    int high = n*n; int low = 0; int ans;
    while (low < high){
        int mid = (low+high)/2;
        if (bear(mid)){
            ans = mid; low = mid+1;
        }
        else 
            high = mid;
    }
    cout<<ans;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:98:11: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   98 |     cout<<ans;
      |           ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 2 ms 2808 KB Output is correct
3 Correct 2 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2764 KB Output is correct
7 Correct 169 ms 5996 KB Output is correct
8 Correct 2 ms 2764 KB Output is correct
9 Correct 2 ms 2764 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Incorrect 2 ms 2764 KB Output isn't correct
13 Incorrect 2 ms 2764 KB Output isn't correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 2 ms 2764 KB Output is correct
16 Correct 2 ms 2764 KB Output is correct
17 Correct 2 ms 2764 KB Output is correct
18 Correct 2 ms 2764 KB Output is correct
19 Correct 2 ms 2768 KB Output is correct
20 Correct 3 ms 2764 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2764 KB Output is correct
25 Correct 2 ms 2764 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 3 ms 2764 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 2 ms 2764 KB Output is correct
31 Correct 2 ms 2892 KB Output is correct
32 Correct 2 ms 2844 KB Output is correct
33 Correct 12 ms 3468 KB Output is correct
34 Correct 13 ms 3532 KB Output is correct
35 Correct 38 ms 3548 KB Output is correct
36 Correct 15 ms 3660 KB Output is correct
37 Correct 15 ms 3672 KB Output is correct
38 Correct 54 ms 3724 KB Output is correct
39 Correct 18 ms 3916 KB Output is correct
40 Correct 19 ms 3928 KB Output is correct
41 Correct 64 ms 3928 KB Output is correct
42 Correct 22 ms 4172 KB Output is correct
43 Correct 23 ms 4088 KB Output is correct
44 Correct 81 ms 4168 KB Output is correct
45 Correct 26 ms 4424 KB Output is correct
46 Correct 27 ms 4364 KB Output is correct
47 Correct 99 ms 4428 KB Output is correct
48 Correct 32 ms 4632 KB Output is correct
49 Correct 37 ms 4672 KB Output is correct
50 Correct 127 ms 4664 KB Output is correct
51 Correct 40 ms 4956 KB Output is correct
52 Correct 37 ms 4952 KB Output is correct
53 Correct 138 ms 4956 KB Output is correct
54 Correct 42 ms 5188 KB Output is correct
55 Correct 42 ms 5140 KB Output is correct
56 Correct 156 ms 5256 KB Output is correct
57 Correct 48 ms 5572 KB Output is correct
58 Correct 48 ms 5536 KB Output is correct
59 Correct 195 ms 5576 KB Output is correct
60 Correct 55 ms 6008 KB Output is correct
61 Correct 54 ms 5912 KB Output is correct
62 Correct 224 ms 5984 KB Output is correct
63 Correct 184 ms 5972 KB Output is correct
64 Correct 267 ms 5920 KB Output is correct
65 Correct 255 ms 5976 KB Output is correct
66 Correct 195 ms 5920 KB Output is correct
67 Correct 70 ms 5828 KB Output is correct
68 Correct 99 ms 5832 KB Output is correct
69 Correct 92 ms 5892 KB Output is correct
70 Correct 85 ms 5928 KB Output is correct
71 Correct 78 ms 5828 KB Output is correct
72 Incorrect 89 ms 5940 KB Output isn't correct
73 Incorrect 136 ms 6212 KB Output isn't correct
74 Correct 128 ms 6336 KB Output is correct
75 Correct 133 ms 6292 KB Output is correct
76 Correct 124 ms 6388 KB Output is correct
77 Correct 131 ms 6132 KB Output is correct
78 Correct 68 ms 6084 KB Output is correct
79 Correct 116 ms 6108 KB Output is correct
80 Correct 122 ms 6064 KB Output is correct
81 Correct 142 ms 6216 KB Output is correct
82 Correct 125 ms 6180 KB Output is correct
83 Correct 159 ms 6212 KB Output is correct
84 Correct 142 ms 6140 KB Output is correct
85 Correct 136 ms 6052 KB Output is correct
86 Correct 141 ms 6140 KB Output is correct
87 Correct 145 ms 6144 KB Output is correct
88 Correct 158 ms 5984 KB Output is correct
89 Correct 158 ms 5980 KB Output is correct
90 Correct 158 ms 6088 KB Output is correct
91 Correct 174 ms 6084 KB Output is correct
92 Correct 154 ms 6284 KB Output is correct