Submission #749387

# Submission time Handle Problem Language Result Execution time Memory
749387 2023-05-27T21:05:27 Z PanosPask Mecho (IOI09_mecho) C++14
100 / 100
334 ms 6212 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair

using namespace std;

const int DIRS = 4;
const int d_v[] = {1, 0, -1, 0};
const int d_h[] = {0, 1, 0, -1};

typedef pair<int, int> pi;

int n, steps;
vector<pi> hives;
vector<vector<char>> grid;
vector<vector<int>> bee_distance;
vector<vector<int>> dist;
vector<vector<bool>> vis;
pi dest, mecho;

bool inbound(int coord)
{
    return coord >= 0 && coord < n;
}

void calculate_distance(vector<pi>& sources, vector<vector<int>>& dist, bool ismecho, int stepcycle)
{
    queue<pi> q;
    dist.resize(n);
    for (int i = 0; i < n; i++)
        dist[i].assign(n, 1e9);

    for (int i = 0; i < sources.size(); i++) {
        dist[sources[i].f][sources[i].s] = 0;
        q.push(sources[i]);
    }

    while(!q.empty()) {
        int i, j;
        tie(i, j) = q.front(); q.pop();

        for (int x = 0; x < DIRS; x++) {
            int ni = i + d_v[x];
            int nj = j + d_h[x];
            if (inbound(ni) && inbound(nj)
                && (grid[ni][nj] == 'G' || grid[ni][nj] == 'M' || (ismecho && grid[ni][nj] == 'D'))
                && dist[ni][nj] == 1e9) {

                dist[ni][nj] = dist[i][j] + stepcycle;
                q.push(mp(ni, nj));
            }
        }
    }
}

bool solve_mecho(pi m, int curtime)
{
    queue<pi> q;
    q.push(m);

    dist = bee_distance;
    vis.assign(n, vector<bool>(n, false));
    if (dist[m.f][m.s] <= curtime)
        return false;

    dist[m.f][m.s] = curtime;

    while (!q.empty()) {
        int i, j;
        tie(i, j) = q.front(); q.pop();

        if (i == dest.f && j == dest.s)
            return true;

        for (int x = 0; x < 4; x++) {
            int ni = i + d_v[x];
            int nj = j + d_h[x];

            if (inbound(ni) && inbound(nj)
                && dist[ni][nj] > dist[i][j] + 1
                && (grid[ni][nj] == 'G' || grid[ni][nj] == 'D')
                && !vis[ni][nj]) {
                    vis[ni][nj] = true;
                    dist[ni][nj] = dist[i][j] + 1;
                    q.push(mp(ni, nj));
                }
        }
    }

    return false;
}

bool can_reach(int x)
{
    return solve_mecho(mecho, x * steps);
}

int main(void)
{
    scanf("%d %d", &n, &steps);

    grid.resize(n, vector<char>(n));
    bee_distance.assign(n, vector<int>(n, 1e9));
    vis.assign(n, vector<bool>(n, false));

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            scanf(" %c", &grid[i][j]);
            if (grid[i][j] == 'H')
                hives.push_back(mp(i, j));
            else if (grid[i][j] == 'D')
                dest = mp(i, j);
            else if (grid[i][j] == 'M')
                mecho = mp(i, j);
        }

    calculate_distance(hives, bee_distance, false, steps);

    int l = -1;
    int r = 1;

    while(can_reach(r)) {
        l = r;
        r *= 2;
    }
    while (r > l + 1) {
        int mid = (l + r) / 2;
        if (can_reach(mid))
            l = mid;
        else
            r = mid;
    }

    int ans = l;
    printf("%d\n", ans);

    return 0;
}

Compilation message

mecho.cpp: In function 'void calculate_distance(std::vector<std::pair<int, int> >&, std::vector<std::vector<int> >&, bool, int)':
mecho.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int i = 0; i < sources.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |     scanf("%d %d", &n, &steps);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp:110:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |             scanf(" %c", &grid[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~
# 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 118 ms 6144 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 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 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 9 ms 1344 KB Output is correct
34 Correct 10 ms 1444 KB Output is correct
35 Correct 18 ms 1364 KB Output is correct
36 Correct 11 ms 1792 KB Output is correct
37 Correct 13 ms 1796 KB Output is correct
38 Correct 25 ms 1904 KB Output is correct
39 Correct 14 ms 2136 KB Output is correct
40 Correct 14 ms 2180 KB Output is correct
41 Correct 32 ms 2132 KB Output is correct
42 Correct 20 ms 2612 KB Output is correct
43 Correct 19 ms 2500 KB Output is correct
44 Correct 36 ms 2604 KB Output is correct
45 Correct 23 ms 3032 KB Output is correct
46 Correct 22 ms 3076 KB Output is correct
47 Correct 42 ms 3064 KB Output is correct
48 Correct 24 ms 3540 KB Output is correct
49 Correct 26 ms 3588 KB Output is correct
50 Correct 53 ms 3572 KB Output is correct
51 Correct 28 ms 4156 KB Output is correct
52 Correct 29 ms 4172 KB Output is correct
53 Correct 61 ms 4152 KB Output is correct
54 Correct 35 ms 4756 KB Output is correct
55 Correct 35 ms 4756 KB Output is correct
56 Correct 73 ms 4776 KB Output is correct
57 Correct 40 ms 5380 KB Output is correct
58 Correct 40 ms 5384 KB Output is correct
59 Correct 79 ms 5396 KB Output is correct
60 Correct 46 ms 6092 KB Output is correct
61 Correct 44 ms 6040 KB Output is correct
62 Correct 92 ms 6128 KB Output is correct
63 Correct 69 ms 6032 KB Output is correct
64 Correct 334 ms 6140 KB Output is correct
65 Correct 293 ms 6040 KB Output is correct
66 Correct 238 ms 6092 KB Output is correct
67 Correct 63 ms 6140 KB Output is correct
68 Correct 55 ms 6056 KB Output is correct
69 Correct 85 ms 6212 KB Output is correct
70 Correct 66 ms 6040 KB Output is correct
71 Correct 62 ms 6036 KB Output is correct
72 Correct 62 ms 6032 KB Output is correct
73 Correct 51 ms 6104 KB Output is correct
74 Correct 139 ms 6072 KB Output is correct
75 Correct 115 ms 6076 KB Output is correct
76 Correct 105 ms 6092 KB Output is correct
77 Correct 132 ms 6176 KB Output is correct
78 Correct 60 ms 6052 KB Output is correct
79 Correct 132 ms 6172 KB Output is correct
80 Correct 112 ms 6092 KB Output is correct
81 Correct 106 ms 6168 KB Output is correct
82 Correct 116 ms 6092 KB Output is correct
83 Correct 57 ms 6160 KB Output is correct
84 Correct 135 ms 6168 KB Output is correct
85 Correct 119 ms 6072 KB Output is correct
86 Correct 105 ms 6168 KB Output is correct
87 Correct 129 ms 6128 KB Output is correct
88 Correct 58 ms 6164 KB Output is correct
89 Correct 145 ms 6044 KB Output is correct
90 Correct 113 ms 6036 KB Output is correct
91 Correct 130 ms 6036 KB Output is correct
92 Correct 98 ms 6164 KB Output is correct