답안 #491408

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
491408 2021-12-02T01:17:19 Z nehasane Mecho (IOI09_mecho) C++14
100 / 100
633 ms 6804 KB
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 800;
vector <string> field(MAX_N);
int n, s;
bool valid_sq(int x, int y){
    if (x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G' || field[x][y] == 'M'))
        return true;
    return false;
}
int main()
{
    cin >> n >> s;
    for (int i = 0; i < n; i++)
        cin >> field[i];
    vector <pair <int, int>> hives;
    int mechox, mechoy, home_x, home_y;
    //find x and y coordinates for for Mecho, the bees and the cave
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (field[i][j] == 'M'){
                mechox = i;
                mechoy = j;
            }else if (field[i][j] == 'H'){
                hives.push_back({i, j});
            }else if (field[i][j] == 'D'){
                home_x = i;
                home_y = j;
            }
        }
    }

    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, -1, 0, 1};

    //  binary search on waiting time
    int l = 0, r = n * n;
    queue <pair <int, int>> q;
    while (l <= r){
        vector <vector <bool>> bees_visited(n, vector<bool>(n, false));
        vector <vector <bool>> mecho_visited(n, vector<bool>(n, false));
        vector <vector <int>> bees_time(n, vector<int>(n, 0));
        vector <vector <int>> mecho_time(n, vector<int>(n, 0));
        
        int eating_time = (l + r) / 2;

        //  move bees
        for (auto i : hives){
            q.push({i.first, i.second});
            bees_visited[i.first][i.second] = true;
        }
        
        while (!q.empty()){
            int x = q.front().first, y = q.front().second;
            q.pop();
            for (int i = 0; i < 4; i++){
                int nx = x + dx[i], ny = y + dy[i];
                if (valid_sq(nx, ny) && !bees_visited[nx][ny]){
                    bees_time[nx][ny] = bees_time[x][y] + 1;    
                    q.push({nx, ny});
                    bees_visited[nx][ny] = true;
                }
            }
        }
        
        
        //  move Mecho
        q.push({mechox, mechoy});
        mecho_visited[mechox][mechoy] = true;
        if (bees_time[mechox][mechoy] <= eating_time)
            q.pop();
        while (!q.empty()){
            int x = q.front().first, y = q.front().second;
            q.pop();
            for (int i = 0; i < 4; i++){
                int nx = x + dx[i], ny = y + dy[i];
                /*  check if mecho reaces node[x][y] before the bees
                    divide the time mecho takes to reach a node by s, since mecho
                    walks s steps at a time.
                    substract the eating time from the time taken for the bees to 
                    reach the node, because that time was used by mecho for eating
                */
                if (valid_sq(nx, ny) && !mecho_visited[nx][ny] && 
                    (mecho_time[x][y] + 1) / s < bees_time[nx][ny] - eating_time){
                        mecho_visited[nx][ny] = true;
                        q.push({nx, ny});
                        mecho_time[nx][ny] = mecho_time[x][y] + 1;
                    }
                }
        }
        
        //  check if mecho reached a node surrounding his cave before the bees
        bool mecho_reached = false;
        for (int i = 0; i < 4; i++){
            int nx = home_x+ dx[i], ny = home_y + dy[i];
            if (valid_sq(nx, ny) && (mecho_time[nx][ny] / s) < bees_time[nx][ny] - eating_time
                && mecho_visited[nx][ny]){
                    mecho_reached = true;
                }
        }
        if (mecho_reached){
            l = eating_time + 1;
        }
        else{
            r = eating_time - 1;
        }
    }
    cout << l - 1 << '\n';
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:70:29: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
   70 |         mecho_visited[mechox][mechoy] = true;
      |                             ^
mecho.cpp:96:37: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |             int nx = home_x+ dx[i], ny = home_y + dy[i];
      |                                     ^~
mecho.cpp:96:17: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |             int nx = home_x+ dx[i], ny = home_y + dy[i];
      |                 ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 578 ms 6496 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 328 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 39 ms 1528 KB Output is correct
34 Correct 39 ms 1520 KB Output is correct
35 Correct 75 ms 1524 KB Output is correct
36 Correct 56 ms 1876 KB Output is correct
37 Correct 55 ms 1868 KB Output is correct
38 Correct 94 ms 1868 KB Output is correct
39 Correct 64 ms 2232 KB Output is correct
40 Correct 66 ms 2252 KB Output is correct
41 Correct 131 ms 2252 KB Output is correct
42 Correct 85 ms 2920 KB Output is correct
43 Correct 84 ms 2904 KB Output is correct
44 Correct 162 ms 2892 KB Output is correct
45 Correct 117 ms 3368 KB Output is correct
46 Correct 102 ms 3412 KB Output is correct
47 Correct 193 ms 3356 KB Output is correct
48 Correct 128 ms 4056 KB Output is correct
49 Correct 128 ms 3924 KB Output is correct
50 Correct 221 ms 3916 KB Output is correct
51 Correct 154 ms 4628 KB Output is correct
52 Correct 150 ms 4472 KB Output is correct
53 Correct 283 ms 4428 KB Output is correct
54 Correct 182 ms 5176 KB Output is correct
55 Correct 177 ms 5076 KB Output is correct
56 Correct 330 ms 4940 KB Output is correct
57 Correct 204 ms 5704 KB Output is correct
58 Correct 201 ms 5704 KB Output is correct
59 Correct 408 ms 5692 KB Output is correct
60 Correct 244 ms 6500 KB Output is correct
61 Correct 221 ms 6400 KB Output is correct
62 Correct 450 ms 6384 KB Output is correct
63 Correct 523 ms 6468 KB Output is correct
64 Correct 633 ms 6384 KB Output is correct
65 Correct 558 ms 6348 KB Output is correct
66 Correct 522 ms 6312 KB Output is correct
67 Correct 526 ms 6376 KB Output is correct
68 Correct 464 ms 6412 KB Output is correct
69 Correct 485 ms 6420 KB Output is correct
70 Correct 474 ms 6468 KB Output is correct
71 Correct 483 ms 6464 KB Output is correct
72 Correct 467 ms 6408 KB Output is correct
73 Correct 480 ms 6680 KB Output is correct
74 Correct 534 ms 6692 KB Output is correct
75 Correct 547 ms 6804 KB Output is correct
76 Correct 497 ms 6684 KB Output is correct
77 Correct 534 ms 6688 KB Output is correct
78 Correct 508 ms 6644 KB Output is correct
79 Correct 478 ms 6604 KB Output is correct
80 Correct 491 ms 6648 KB Output is correct
81 Correct 492 ms 6648 KB Output is correct
82 Correct 492 ms 6748 KB Output is correct
83 Correct 519 ms 6728 KB Output is correct
84 Correct 559 ms 6604 KB Output is correct
85 Correct 549 ms 6604 KB Output is correct
86 Correct 518 ms 6592 KB Output is correct
87 Correct 518 ms 6620 KB Output is correct
88 Correct 564 ms 6548 KB Output is correct
89 Correct 522 ms 6476 KB Output is correct
90 Correct 510 ms 6692 KB Output is correct
91 Correct 540 ms 6664 KB Output is correct
92 Correct 550 ms 6552 KB Output is correct