Submission #535828

# Submission time Handle Problem Language Result Execution time Memory
535828 2022-03-11T11:40:04 Z SlavicG Mecho (IOI09_mecho) C++17
100 / 100
186 ms 11360 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()

#define int long long

const int N = 805;
char c[N][N];
int n, s;
bool ok(int i, int j) {
    return (i >= 0 && j >= 0 && i < n && j < n && c[i][j] != 'T' && c[i][j] != 'H');
}

vector<vector<int>> bfs(vector<pair<int,int>> nodes, char forbidden) {
    vector<vector<int>> dist(n, vector<int>(n, INT_MAX));
    queue<pair<int,int>> q;
    for(auto f: nodes) {
        dist[f.first][f.second] = 0;
        q.push({f.first, f.second});
    }

    while(!q.empty()) {
        int i = q.front().first, j = q.front().second;
        q.pop();
        if(ok(i + 1, j) && c[i + 1][j] != forbidden && dist[i + 1][j] == INT_MAX) {
            dist[i + 1][j] = dist[i][j] + 1;
            q.push({i + 1, j});
        }   
        if(ok(i - 1, j) && c[i - 1][j] != forbidden && dist[i - 1][j] == INT_MAX) {
            dist[i - 1][j] = dist[i][j] + 1;
            q.push({i - 1, j});
        }  

        if(ok(i, j + 1) && c[i][j + 1] != forbidden && dist[i][j + 1] == INT_MAX) {
            dist[i][j + 1] = dist[i][j] + 1;
            q.push({i, j + 1});
        }  

        if(ok(i, j - 1) && c[i][j - 1] != forbidden && dist[i][j - 1] == INT_MAX) {
            dist[i][j - 1] = dist[i][j] + 1;
            q.push({i, j - 1});
        }  
    }
    return dist;
}
vector<vector<int>> bees;
bool dfs(int startx, int starty, int val) {
    queue<pair<pair<int, int>, int>> q;
    q.push({{startx, starty}, 0});
    if(val >= bees[startx][starty]) return false;
    vector<vector<int>> dist(n, vector<int>(n, -1));
    dist[startx][starty] = val;

    while(!q.empty()) {
        int i = q.front().first.first, j = q.front().first.second, moves = q.front().second;
        q.pop();
        if(c[i][j] == 'D') return true;

        if(ok(i + 1, j) && dist[i + 1][j] == -1 && (dist[i][j] + ((moves + 1) % s == 0)) < bees[i + 1][j]) {
            dist[i + 1][j] = dist[i][j] + ((moves + 1) % s == 0);
            q.push({{i + 1, j}, moves + 1});
        }
        if(ok(i - 1, j) && dist[i - 1][j] == -1 && (dist[i][j] + ((moves + 1) % s == 0)) < bees[i - 1][j]) {
            dist[i - 1][j] = dist[i][j] + ((moves + 1) % s == 0);
            q.push({{i - 1, j}, moves + 1});
        }
        if(ok(i, j + 1) && dist[i][j + 1] == -1 && (dist[i][j] + ((moves + 1) % s == 0)) < bees[i][j + 1]) {
            dist[i][j + 1] = dist[i][j] + ((moves + 1) % s == 0);
            q.push({{i, j + 1}, moves + 1});
        }
        if(ok(i, j - 1) && dist[i][j - 1] == -1 && (dist[i][j] + ((moves + 1) % s == 0)) < bees[i][j - 1]) {
            dist[i][j - 1] = dist[i][j] + ((moves + 1) % s == 0);
            q.push({{i, j - 1}, moves + 1});
        }
    }
    return false;

}
void solve() {
    cin >> n >> s;

    int startx, starty;
    vector<pair<int,int>> f;
    for(int i = 0;i < n; ++i) {
        for(int j = 0;j < n; ++j) {
            cin >> c[i][j];
            if(c[i][j] == 'M') {
                startx = i, starty = j;
            }
            if(c[i][j] == 'H') {
                f.pb({i, j});
            }
        }
    }

    bees = bfs(f, 'D');
    
    int l = 0, r = 1e9;
    int ans = -1;
    while(l <= r) {
        int mid = l + r >> 1;

        if(dfs(startx, starty, mid)) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    cout << ans << "\n";
}
 
int32_t main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
}

Compilation message

mecho.cpp: In function 'void solve()':
mecho.cpp:109:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  109 |         int mid = l + r >> 1;
      |                   ~~^~~
mecho.cpp:111:15: warning: 'starty' may be used uninitialized in this function [-Wmaybe-uninitialized]
  111 |         if(dfs(startx, starty, mid)) {
      |            ~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp:111:15: warning: 'startx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 108 ms 11036 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 8 ms 2580 KB Output is correct
34 Correct 6 ms 2556 KB Output is correct
35 Correct 18 ms 2556 KB Output is correct
36 Correct 8 ms 3180 KB Output is correct
37 Correct 7 ms 3192 KB Output is correct
38 Correct 25 ms 3180 KB Output is correct
39 Correct 11 ms 3920 KB Output is correct
40 Correct 9 ms 3900 KB Output is correct
41 Correct 31 ms 3916 KB Output is correct
42 Correct 13 ms 4740 KB Output is correct
43 Correct 9 ms 4652 KB Output is correct
44 Correct 44 ms 4684 KB Output is correct
45 Correct 17 ms 5572 KB Output is correct
46 Correct 13 ms 5576 KB Output is correct
47 Correct 50 ms 5548 KB Output is correct
48 Correct 21 ms 6500 KB Output is correct
49 Correct 21 ms 6696 KB Output is correct
50 Correct 62 ms 6476 KB Output is correct
51 Correct 25 ms 7540 KB Output is correct
52 Correct 23 ms 7508 KB Output is correct
53 Correct 82 ms 7532 KB Output is correct
54 Correct 36 ms 8708 KB Output is correct
55 Correct 32 ms 8688 KB Output is correct
56 Correct 96 ms 8616 KB Output is correct
57 Correct 33 ms 9828 KB Output is correct
58 Correct 24 ms 9796 KB Output is correct
59 Correct 167 ms 9776 KB Output is correct
60 Correct 38 ms 11140 KB Output is correct
61 Correct 41 ms 11204 KB Output is correct
62 Correct 146 ms 11056 KB Output is correct
63 Correct 116 ms 11068 KB Output is correct
64 Correct 186 ms 11080 KB Output is correct
65 Correct 183 ms 11080 KB Output is correct
66 Correct 132 ms 11068 KB Output is correct
67 Correct 119 ms 11084 KB Output is correct
68 Correct 61 ms 11096 KB Output is correct
69 Correct 59 ms 11120 KB Output is correct
70 Correct 43 ms 11100 KB Output is correct
71 Correct 50 ms 11140 KB Output is correct
72 Correct 39 ms 11172 KB Output is correct
73 Correct 39 ms 11092 KB Output is correct
74 Correct 69 ms 11156 KB Output is correct
75 Correct 73 ms 11136 KB Output is correct
76 Correct 71 ms 11140 KB Output is correct
77 Correct 75 ms 11116 KB Output is correct
78 Correct 82 ms 11140 KB Output is correct
79 Correct 66 ms 11132 KB Output is correct
80 Correct 70 ms 11120 KB Output is correct
81 Correct 76 ms 11124 KB Output is correct
82 Correct 75 ms 11128 KB Output is correct
83 Correct 104 ms 11120 KB Output is correct
84 Correct 92 ms 11360 KB Output is correct
85 Correct 90 ms 11080 KB Output is correct
86 Correct 93 ms 11112 KB Output is correct
87 Correct 93 ms 11108 KB Output is correct
88 Correct 104 ms 11088 KB Output is correct
89 Correct 97 ms 11100 KB Output is correct
90 Correct 107 ms 11092 KB Output is correct
91 Correct 98 ms 11112 KB Output is correct
92 Correct 102 ms 11092 KB Output is correct