Submission #789052

# Submission time Handle Problem Language Result Execution time Memory
789052 2023-07-20T22:51:29 Z orcslop Mecho (IOI09_mecho) C++17
60 / 100
184 ms 6896 KB
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size() 

const int MAXN = 805; 
const int dx[4] = {-1, 1, 0, 0}; 
const int dy[4] = {0, 0, -1, 1}; 

int n, s; 
pair<int, int> mecho, home; 
queue<pair<int, int>> q; 
char grid[MAXN][MAXN]; 
int depth[2][MAXN][MAXN]; 

bool bbound(int x, int y){
    return (x >= 0 && y >= 0 && x < n && y < n && (grid[x][y] == 'G' || grid[x][y] == 'H' || grid[x][y] == 'M' || grid[x][y] == 'D')); 
}

bool mbound(int x, int y){
    return (x >= 0 && y >= 0 && x < n && y < n && (grid[x][y] == 'G' || grid[x][y] == 'M' || grid[x][y] == 'D')); 
}

bool check(int lim){
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) depth[1][i][j] = 0; 
    depth[1][mecho.first][mecho.second] = 1; 
    while(!q.empty()) q.pop(); 
    q.push(mecho); 

    while(!q.empty()){
        pair<int, int> curr = q.front(); 
        q.pop(); 
        for(int i = 0; i < 4; i++){
            int x = curr.first + dx[i]; 
            int y = curr.second + dy[i]; 
            int d = depth[1][curr.first][curr.second] + 1; 
            // cout << d << '\n'; 
            if(mbound(x, y) && !depth[1][x][y]){
                // cout << d << '\n'; 
                if((d - 1) / s + 1 + lim <= depth[0][x][y]){
                    depth[1][x][y] = d; 
                    q.push(make_pair(x, y)); 
                }
                if(x == home.first && y == home.second && (d - 1) / s + lim - ((d-1) % s == 0) <= depth[0][x][y]) return true; 

            }
        }
    }
    return depth[1][home.first][home.second]; 
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    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] == 'H') {
                q.push(make_pair(i, j)); 
                depth[0][i][j] = 1; 
            }
            else if(grid[i][j] == 'M') mecho = make_pair(i, j); 
            else if(grid[i][j] == 'D') home = make_pair(i, j); 
        }
    }
    while(!q.empty()){
        pair<int, int> curr = q.front(); 
        q.pop(); 
        for(int i = 0; i < 4; i++){
            int x = curr.first + dx[i]; 
            int y = curr.second + dy[i]; 
            if(bbound(x, y) && !depth[0][x][y]){
                depth[0][x][y] = depth[0][curr.first][curr.second] + 1; 
                q.push(make_pair(x, y)); 
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++) {
            // depth[1][i][j] = (depth[1][i][j] + s - 1) / s; 
            depth[0][i][j] -= (depth[0][i][j] > 0); 
        }
    }
    int low = -1, high = 5000; 
    while (low < high) {
        int mid = low + (high - low + 1) / 2;
        if (check(mid)) low = mid; 
        else high = mid - 1;
    }
    cout << (low) << '\n'; 
    // cout << check(1) << '\n'; 
    // for(int i = 0; i < n; i++){
    //     for(int j = 0; j < n; j++){
    //         cout << depth[0][i][j] << ' '; 
    //     }
    //     cout << "     "; 
    //     for(int j = 0; j < n; j++){
    //         cout << depth[1][i][j] << ' '; 
    //     }
    //     cout << '\n'; 

    // }
    return 0; 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 80 ms 6732 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Incorrect 1 ms 712 KB Output isn't correct
13 Incorrect 1 ms 596 KB Output isn't correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 580 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 1 ms 724 KB Output is correct
30 Correct 2 ms 768 KB Output is correct
31 Correct 1 ms 852 KB Output is correct
32 Correct 1 ms 852 KB Output is correct
33 Incorrect 4 ms 2900 KB Output isn't correct
34 Incorrect 4 ms 2900 KB Output isn't correct
35 Correct 21 ms 2896 KB Output is correct
36 Incorrect 6 ms 3284 KB Output isn't correct
37 Incorrect 5 ms 3284 KB Output isn't correct
38 Correct 24 ms 3328 KB Output is correct
39 Incorrect 6 ms 3668 KB Output isn't correct
40 Incorrect 6 ms 3668 KB Output isn't correct
41 Correct 31 ms 3668 KB Output is correct
42 Incorrect 7 ms 4052 KB Output isn't correct
43 Incorrect 7 ms 4052 KB Output isn't correct
44 Correct 38 ms 4044 KB Output is correct
45 Incorrect 9 ms 4436 KB Output isn't correct
46 Incorrect 8 ms 4412 KB Output isn't correct
47 Correct 48 ms 4540 KB Output is correct
48 Incorrect 10 ms 4884 KB Output isn't correct
49 Incorrect 14 ms 4916 KB Output isn't correct
50 Correct 55 ms 4820 KB Output is correct
51 Incorrect 11 ms 5332 KB Output isn't correct
52 Incorrect 11 ms 5332 KB Output isn't correct
53 Correct 70 ms 5344 KB Output is correct
54 Incorrect 13 ms 5700 KB Output isn't correct
55 Incorrect 13 ms 5724 KB Output isn't correct
56 Correct 83 ms 5760 KB Output is correct
57 Incorrect 17 ms 6144 KB Output isn't correct
58 Incorrect 15 ms 6144 KB Output isn't correct
59 Correct 92 ms 6184 KB Output is correct
60 Incorrect 16 ms 6612 KB Output isn't correct
61 Incorrect 16 ms 6628 KB Output isn't correct
62 Correct 112 ms 6624 KB Output is correct
63 Correct 103 ms 6616 KB Output is correct
64 Correct 184 ms 6612 KB Output is correct
65 Correct 140 ms 6620 KB Output is correct
66 Correct 107 ms 6568 KB Output is correct
67 Correct 103 ms 6604 KB Output is correct
68 Correct 43 ms 6604 KB Output is correct
69 Correct 45 ms 6684 KB Output is correct
70 Correct 32 ms 6592 KB Output is correct
71 Correct 37 ms 6568 KB Output is correct
72 Incorrect 28 ms 6604 KB Output isn't correct
73 Incorrect 35 ms 6832 KB Output isn't correct
74 Correct 56 ms 6896 KB Output is correct
75 Correct 68 ms 6848 KB Output is correct
76 Correct 57 ms 6896 KB Output is correct
77 Correct 56 ms 6800 KB Output is correct
78 Correct 71 ms 6872 KB Output is correct
79 Correct 44 ms 6808 KB Output is correct
80 Correct 56 ms 6868 KB Output is correct
81 Correct 65 ms 6824 KB Output is correct
82 Correct 56 ms 6856 KB Output is correct
83 Correct 74 ms 6824 KB Output is correct
84 Correct 65 ms 6824 KB Output is correct
85 Correct 73 ms 6824 KB Output is correct
86 Correct 69 ms 6732 KB Output is correct
87 Correct 69 ms 6832 KB Output is correct
88 Correct 74 ms 6772 KB Output is correct
89 Correct 77 ms 6732 KB Output is correct
90 Correct 80 ms 6776 KB Output is correct
91 Correct 75 ms 6776 KB Output is correct
92 Correct 81 ms 6780 KB Output is correct