답안 #488680

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
488680 2021-11-20T02:45:47 Z nehasane Mecho (IOI09_mecho) C++14
100 / 100
613 ms 8392 KB
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 800;
vector <string> field(MAX_N);
bool bees_visited[MAX_N][MAX_N], mecho_visited[MAX_N][MAX_N];
//mecho_dis records the time taken for mecho to reach node[x][y].
//bees_dis does the same for the bees
int mecho_dis[MAX_N][MAX_N], bees_dis[MAX_N][MAX_N];
queue <pair <int, int>> q;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
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;
            }
        }
    }
    //binary search on waiting time
    int l = 0, r = n*n;
    while (l <= r){
        int eating_time = (l + r) / 2;
        memset(bees_visited, false, sizeof(bees_visited));
        memset(mecho_visited, false, sizeof(mecho_visited));
        memset(bees_dis, 0, sizeof(bees_dis));
        memset(mecho_dis, 0, sizeof(mecho_dis));
        //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_dis[nx][ny] = bees_dis[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_dis[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_dis[x][y] + 1) / s < bees_dis[nx][ny] - eating_time){
                    mecho_visited[nx][ny] = true;
                    q.push({nx, ny});
                    mecho_dis[nx][ny] = mecho_dis[x][y] + 1;
                }
            }
        }
        //check if mecho reached a surrounding node 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_dis[nx][ny] / s) < bees_dis[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:67:36: warning: 'mechoy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   67 |         if (bees_dis[mechox][mechoy] <= eating_time)
      |             ~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:67:36: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:91:37: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
   91 |             int nx = home_x+ dx[i], ny = home_y + dy[i];
      |                                     ^~
mecho.cpp:91:17: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   91 |             int nx = home_x+ dx[i], ny = home_y + dy[i];
      |                 ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6480 KB Output is correct
2 Correct 4 ms 6480 KB Output is correct
3 Correct 4 ms 6480 KB Output is correct
4 Correct 4 ms 6480 KB Output is correct
5 Correct 4 ms 6480 KB Output is correct
6 Correct 4 ms 6480 KB Output is correct
7 Correct 478 ms 8088 KB Output is correct
8 Correct 4 ms 6480 KB Output is correct
9 Correct 5 ms 6552 KB Output is correct
10 Correct 5 ms 6516 KB Output is correct
11 Correct 4 ms 6480 KB Output is correct
12 Correct 7 ms 6480 KB Output is correct
13 Correct 7 ms 6564 KB Output is correct
14 Correct 7 ms 6480 KB Output is correct
15 Correct 6 ms 6480 KB Output is correct
16 Correct 4 ms 6480 KB Output is correct
17 Correct 6 ms 6480 KB Output is correct
18 Correct 5 ms 6480 KB Output is correct
19 Correct 5 ms 6480 KB Output is correct
20 Correct 5 ms 6480 KB Output is correct
21 Correct 5 ms 6480 KB Output is correct
22 Correct 6 ms 6480 KB Output is correct
23 Correct 6 ms 6480 KB Output is correct
24 Correct 6 ms 6480 KB Output is correct
25 Correct 7 ms 6480 KB Output is correct
26 Correct 7 ms 6480 KB Output is correct
27 Correct 6 ms 6608 KB Output is correct
28 Correct 7 ms 6480 KB Output is correct
29 Correct 7 ms 6488 KB Output is correct
30 Correct 7 ms 6532 KB Output is correct
31 Correct 6 ms 6480 KB Output is correct
32 Correct 7 ms 6608 KB Output is correct
33 Correct 37 ms 6864 KB Output is correct
34 Correct 38 ms 6760 KB Output is correct
35 Correct 73 ms 6864 KB Output is correct
36 Correct 54 ms 6932 KB Output is correct
37 Correct 48 ms 6928 KB Output is correct
38 Correct 96 ms 6920 KB Output is correct
39 Correct 59 ms 6992 KB Output is correct
40 Correct 59 ms 6992 KB Output is correct
41 Correct 120 ms 6992 KB Output is correct
42 Correct 76 ms 7244 KB Output is correct
43 Correct 75 ms 7276 KB Output is correct
44 Correct 152 ms 7336 KB Output is correct
45 Correct 90 ms 7388 KB Output is correct
46 Correct 85 ms 7388 KB Output is correct
47 Correct 171 ms 7376 KB Output is correct
48 Correct 102 ms 7520 KB Output is correct
49 Correct 108 ms 7504 KB Output is correct
50 Correct 206 ms 7496 KB Output is correct
51 Correct 122 ms 7608 KB Output is correct
52 Correct 122 ms 7608 KB Output is correct
53 Correct 268 ms 7608 KB Output is correct
54 Correct 139 ms 7632 KB Output is correct
55 Correct 140 ms 7632 KB Output is correct
56 Correct 302 ms 7624 KB Output is correct
57 Correct 160 ms 7832 KB Output is correct
58 Correct 155 ms 7848 KB Output is correct
59 Correct 363 ms 7852 KB Output is correct
60 Correct 191 ms 7984 KB Output is correct
61 Correct 176 ms 7956 KB Output is correct
62 Correct 401 ms 8008 KB Output is correct
63 Correct 487 ms 7972 KB Output is correct
64 Correct 613 ms 8008 KB Output is correct
65 Correct 539 ms 7996 KB Output is correct
66 Correct 515 ms 8016 KB Output is correct
67 Correct 507 ms 7964 KB Output is correct
68 Correct 486 ms 8016 KB Output is correct
69 Correct 496 ms 7996 KB Output is correct
70 Correct 461 ms 8016 KB Output is correct
71 Correct 480 ms 7888 KB Output is correct
72 Correct 451 ms 7996 KB Output is correct
73 Correct 437 ms 8268 KB Output is correct
74 Correct 480 ms 8272 KB Output is correct
75 Correct 490 ms 8268 KB Output is correct
76 Correct 450 ms 8272 KB Output is correct
77 Correct 488 ms 8392 KB Output is correct
78 Correct 472 ms 8232 KB Output is correct
79 Correct 460 ms 8232 KB Output is correct
80 Correct 478 ms 8232 KB Output is correct
81 Correct 480 ms 8264 KB Output is correct
82 Correct 493 ms 8228 KB Output is correct
83 Correct 502 ms 8192 KB Output is correct
84 Correct 482 ms 8192 KB Output is correct
85 Correct 508 ms 8144 KB Output is correct
86 Correct 489 ms 8188 KB Output is correct
87 Correct 494 ms 8192 KB Output is correct
88 Correct 476 ms 8132 KB Output is correct
89 Correct 495 ms 8264 KB Output is correct
90 Correct 479 ms 8144 KB Output is correct
91 Correct 496 ms 8252 KB Output is correct
92 Correct 505 ms 8180 KB Output is correct