답안 #491407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
491407 2021-12-02T01:11:03 Z nehasane Mecho (IOI09_mecho) C++14
0 / 100
1000 ms 6768 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:69:29: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
   69 |         mecho_visited[mechox][mechoy] = true;
      |                             ^
mecho.cpp:94:41: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
   94 |                 int nx = home_x+ dx[i], ny = home_y + dy[i];
      |                                         ^~
mecho.cpp:94:21: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   94 |                 int nx = home_x+ dx[i], ny = home_y + dy[i];
      |                     ^~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1089 ms 3024 KB Time limit exceeded
2 Execution timed out 1089 ms 2304 KB Time limit exceeded
3 Execution timed out 1083 ms 2628 KB Time limit exceeded
4 Execution timed out 1070 ms 1792 KB Time limit exceeded
5 Execution timed out 1085 ms 1976 KB Time limit exceeded
6 Execution timed out 1095 ms 912 KB Time limit exceeded
7 Execution timed out 1070 ms 6488 KB Time limit exceeded
8 Execution timed out 1080 ms 708 KB Time limit exceeded
9 Execution timed out 1084 ms 668 KB Time limit exceeded
10 Execution timed out 1088 ms 716 KB Time limit exceeded
11 Execution timed out 1098 ms 608 KB Time limit exceeded
12 Execution timed out 1090 ms 360 KB Time limit exceeded
13 Execution timed out 1094 ms 368 KB Time limit exceeded
14 Execution timed out 1084 ms 460 KB Time limit exceeded
15 Execution timed out 1073 ms 580 KB Time limit exceeded
16 Execution timed out 1096 ms 704 KB Time limit exceeded
17 Execution timed out 1090 ms 524 KB Time limit exceeded
18 Execution timed out 1096 ms 568 KB Time limit exceeded
19 Execution timed out 1076 ms 588 KB Time limit exceeded
20 Execution timed out 1097 ms 608 KB Time limit exceeded
21 Execution timed out 1094 ms 460 KB Time limit exceeded
22 Execution timed out 1051 ms 580 KB Time limit exceeded
23 Execution timed out 1087 ms 500 KB Time limit exceeded
24 Execution timed out 1085 ms 512 KB Time limit exceeded
25 Execution timed out 1091 ms 488 KB Time limit exceeded
26 Execution timed out 1091 ms 452 KB Time limit exceeded
27 Execution timed out 1090 ms 396 KB Time limit exceeded
28 Execution timed out 1082 ms 504 KB Time limit exceeded
29 Execution timed out 1081 ms 632 KB Time limit exceeded
30 Execution timed out 1087 ms 756 KB Time limit exceeded
31 Execution timed out 1057 ms 708 KB Time limit exceeded
32 Execution timed out 1091 ms 396 KB Time limit exceeded
33 Execution timed out 1084 ms 1484 KB Time limit exceeded
34 Execution timed out 1093 ms 1484 KB Time limit exceeded
35 Execution timed out 1086 ms 1532 KB Time limit exceeded
36 Execution timed out 1091 ms 1872 KB Time limit exceeded
37 Execution timed out 1099 ms 1868 KB Time limit exceeded
38 Execution timed out 1095 ms 1868 KB Time limit exceeded
39 Execution timed out 1082 ms 2252 KB Time limit exceeded
40 Execution timed out 1085 ms 2172 KB Time limit exceeded
41 Execution timed out 1091 ms 2260 KB Time limit exceeded
42 Execution timed out 1093 ms 2824 KB Time limit exceeded
43 Execution timed out 1081 ms 2892 KB Time limit exceeded
44 Execution timed out 1093 ms 2892 KB Time limit exceeded
45 Execution timed out 1091 ms 3360 KB Time limit exceeded
46 Execution timed out 1094 ms 3276 KB Time limit exceeded
47 Execution timed out 1079 ms 3364 KB Time limit exceeded
48 Execution timed out 1096 ms 3920 KB Time limit exceeded
49 Execution timed out 1089 ms 3916 KB Time limit exceeded
50 Execution timed out 1084 ms 3928 KB Time limit exceeded
51 Execution timed out 1080 ms 4428 KB Time limit exceeded
52 Execution timed out 1086 ms 4428 KB Time limit exceeded
53 Execution timed out 1095 ms 4444 KB Time limit exceeded
54 Execution timed out 1095 ms 5068 KB Time limit exceeded
55 Execution timed out 1071 ms 5060 KB Time limit exceeded
56 Execution timed out 1093 ms 4952 KB Time limit exceeded
57 Execution timed out 1089 ms 5708 KB Time limit exceeded
58 Execution timed out 1092 ms 5704 KB Time limit exceeded
59 Execution timed out 1085 ms 5712 KB Time limit exceeded
60 Execution timed out 1091 ms 6348 KB Time limit exceeded
61 Execution timed out 1093 ms 6272 KB Time limit exceeded
62 Execution timed out 1087 ms 6380 KB Time limit exceeded
63 Execution timed out 1098 ms 6384 KB Time limit exceeded
64 Execution timed out 1091 ms 6508 KB Time limit exceeded
65 Execution timed out 1094 ms 6392 KB Time limit exceeded
66 Execution timed out 1089 ms 6384 KB Time limit exceeded
67 Execution timed out 1080 ms 6312 KB Time limit exceeded
68 Execution timed out 1094 ms 6452 KB Time limit exceeded
69 Execution timed out 1099 ms 6404 KB Time limit exceeded
70 Execution timed out 1089 ms 6324 KB Time limit exceeded
71 Execution timed out 1098 ms 6328 KB Time limit exceeded
72 Execution timed out 1092 ms 6416 KB Time limit exceeded
73 Execution timed out 1098 ms 6724 KB Time limit exceeded
74 Execution timed out 1091 ms 6688 KB Time limit exceeded
75 Execution timed out 1096 ms 6680 KB Time limit exceeded
76 Execution timed out 1097 ms 6724 KB Time limit exceeded
77 Execution timed out 1094 ms 6568 KB Time limit exceeded
78 Execution timed out 1099 ms 6764 KB Time limit exceeded
79 Execution timed out 1092 ms 6608 KB Time limit exceeded
80 Execution timed out 1093 ms 6768 KB Time limit exceeded
81 Execution timed out 1096 ms 6724 KB Time limit exceeded
82 Execution timed out 1093 ms 6644 KB Time limit exceeded
83 Execution timed out 1089 ms 6600 KB Time limit exceeded
84 Execution timed out 1095 ms 6608 KB Time limit exceeded
85 Execution timed out 1097 ms 6608 KB Time limit exceeded
86 Execution timed out 1089 ms 6608 KB Time limit exceeded
87 Execution timed out 1086 ms 6608 KB Time limit exceeded
88 Execution timed out 1069 ms 6544 KB Time limit exceeded
89 Execution timed out 1088 ms 6556 KB Time limit exceeded
90 Execution timed out 1092 ms 6632 KB Time limit exceeded
91 Execution timed out 1092 ms 6644 KB Time limit exceeded
92 Execution timed out 1091 ms 6548 KB Time limit exceeded