Submission #749386

# Submission time Handle Problem Language Result Execution time Memory
749386 2023-05-27T20:58:13 Z PanosPask Mecho (IOI09_mecho) C++14
84 / 100
350 ms 6220 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));
    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:99:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |     scanf("%d %d", &n, &steps);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp:107:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |             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 0 ms 212 KB Output is correct
7 Correct 117 ms 6092 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 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 Incorrect 1 ms 340 KB Output isn't correct
13 Incorrect 1 ms 212 KB Output isn't 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 0 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 328 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 1364 KB Output is correct
34 Correct 10 ms 1364 KB Output is correct
35 Correct 18 ms 1444 KB Output is correct
36 Correct 11 ms 1800 KB Output is correct
37 Correct 11 ms 1748 KB Output is correct
38 Correct 24 ms 1804 KB Output is correct
39 Correct 14 ms 2132 KB Output is correct
40 Correct 13 ms 2132 KB Output is correct
41 Correct 29 ms 2136 KB Output is correct
42 Correct 21 ms 2512 KB Output is correct
43 Correct 20 ms 2512 KB Output is correct
44 Correct 40 ms 2516 KB Output is correct
45 Correct 23 ms 3028 KB Output is correct
46 Correct 22 ms 3028 KB Output is correct
47 Correct 43 ms 3028 KB Output is correct
48 Correct 24 ms 3596 KB Output is correct
49 Correct 25 ms 3540 KB Output is correct
50 Correct 54 ms 3604 KB Output is correct
51 Correct 28 ms 4164 KB Output is correct
52 Correct 29 ms 4204 KB Output is correct
53 Correct 61 ms 4180 KB Output is correct
54 Correct 37 ms 4760 KB Output is correct
55 Correct 37 ms 4772 KB Output is correct
56 Correct 77 ms 4772 KB Output is correct
57 Correct 43 ms 5416 KB Output is correct
58 Correct 46 ms 5372 KB Output is correct
59 Correct 81 ms 5456 KB Output is correct
60 Correct 44 ms 6036 KB Output is correct
61 Correct 45 ms 6032 KB Output is correct
62 Correct 94 ms 6092 KB Output is correct
63 Correct 72 ms 6136 KB Output is correct
64 Correct 350 ms 6220 KB Output is correct
65 Correct 306 ms 6028 KB Output is correct
66 Correct 244 ms 6040 KB Output is correct
67 Correct 65 ms 6136 KB Output is correct
68 Correct 56 ms 6148 KB Output is correct
69 Correct 84 ms 6152 KB Output is correct
70 Correct 69 ms 6056 KB Output is correct
71 Correct 63 ms 6100 KB Output is correct
72 Incorrect 59 ms 6044 KB Output isn't correct
73 Incorrect 59 ms 6084 KB Output isn't correct
74 Correct 129 ms 6080 KB Output is correct
75 Correct 114 ms 6172 KB Output is correct
76 Correct 98 ms 6084 KB Output is correct
77 Correct 125 ms 6200 KB Output is correct
78 Correct 57 ms 6096 KB Output is correct
79 Correct 129 ms 6192 KB Output is correct
80 Correct 113 ms 6092 KB Output is correct
81 Correct 112 ms 6092 KB Output is correct
82 Correct 113 ms 6064 KB Output is correct
83 Correct 60 ms 6040 KB Output is correct
84 Correct 145 ms 6164 KB Output is correct
85 Correct 115 ms 6164 KB Output is correct
86 Correct 115 ms 6176 KB Output is correct
87 Correct 130 ms 6092 KB Output is correct
88 Correct 59 ms 6068 KB Output is correct
89 Correct 147 ms 6068 KB Output is correct
90 Correct 116 ms 6040 KB Output is correct
91 Correct 132 ms 6084 KB Output is correct
92 Correct 100 ms 6036 KB Output is correct