Submission #391418

# Submission time Handle Problem Language Result Execution time Memory
391418 2021-04-18T17:28:44 Z ml1234 Mecho (IOI09_mecho) C++17
100 / 100
274 ms 6292 KB
#include <iostream>
#include <string>
#include <queue>
using namespace std;
bool validmecho(int i, int j);
bool validbees(int i, int j);
bool bfs(int starttime);

string grid[800];
int bees[800][800];
int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
int n, s;
pair<int, int> mecho;
int main() 
{
    cin >> n >> s;
    for (int i = 0; i < n; i++)
    {
        cin >> grid[i];
        for (int j = 0; j < n; j++)
        {
            if (grid[i][j] == 'M')
            {
                mecho = {i, j};
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            bees[i][j] = 1e9;
        }
    }
    queue<pair<int, int>> q;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (grid[i][j] == 'H')
            {
                q.push({i, j});
                bees[i][j] = 0;
            }
        }
    }
    while (!q.empty())
    {
        int curri = q.front().first, currj = q.front().second;
        q.pop();
        for (int d = 0; d < 4; d++)
        {
            int nexti = curri + dx[d], nextj = currj + dy[d];
            if (validbees(nexti, nextj) && bees[nexti][nextj] > bees[curri][currj] + s)
            {
                bees[nexti][nextj] = bees[curri][currj] + s;
                q.push({nexti, nextj});
            }
        }
    }
    int low = -1, high = n * n;
    while (low < high)
    {
        int mid = (low + high + 1) / 2;
        if (bfs(mid))
        {
            low = mid;
        }
        else
        {
            high = mid - 1;
        }
    }
    cout << low << endl;
    return 0;
}

bool ongrid(int i, int j)
{
    return (i >= 0 && i < n && j >= 0 && j < n);
}
bool validbees(int i, int j)
{
    return (ongrid(i, j) && !(grid[i][j] == 'T') && !(grid[i][j] == 'D') && !(grid[i][j] == 'H'));
}
bool validmecho(int i, int j)
{
    return (ongrid(i, j) && !(grid[i][j] == 'T'));
}

bool bfs(int starttime)
{
    if (starttime * s >= bees[mecho.first][mecho.second])
    {
        return false;
    }
    queue<pair<int, int>> q;
    q.push(mecho);
    vector<vector<int>> mechodist(n, vector<int> (n, -1));
    mechodist[mecho.first][mecho.second] = starttime * s;
    while (!q.empty())
    {
        int curri = q.front().first, currj = q.front().second;
        q.pop();
        if (grid[curri][currj] == 'D')
        {
            return true;
        }
        for (int d = 0; d < 4; d++)
        {
            int nexti = curri + dx[d], nextj = currj + dy[d];
            if (!validmecho(nexti, nextj))
            {
                continue;
            }
            if (mechodist[nexti][nextj] == -1)
            {
                if (bees[nexti][nextj] > mechodist[curri][currj] + 1)
                {
                    mechodist[nexti][nextj] = mechodist[curri][currj] + 1;
                    q.push({nexti, nextj});
                }
            }
        }
    }
    return false;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 142 ms 6140 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 476 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Correct 8 ms 2068 KB Output is correct
34 Correct 8 ms 2080 KB Output is correct
35 Correct 31 ms 2104 KB Output is correct
36 Correct 10 ms 2380 KB Output is correct
37 Correct 11 ms 2420 KB Output is correct
38 Correct 42 ms 2380 KB Output is correct
39 Correct 13 ms 2760 KB Output is correct
40 Correct 13 ms 2764 KB Output is correct
41 Correct 53 ms 2772 KB Output is correct
42 Correct 18 ms 3372 KB Output is correct
43 Correct 17 ms 3384 KB Output is correct
44 Correct 69 ms 3360 KB Output is correct
45 Correct 20 ms 3776 KB Output is correct
46 Correct 19 ms 3788 KB Output is correct
47 Correct 78 ms 3784 KB Output is correct
48 Correct 23 ms 4204 KB Output is correct
49 Correct 23 ms 4208 KB Output is correct
50 Correct 107 ms 4196 KB Output is correct
51 Correct 30 ms 4676 KB Output is correct
52 Correct 27 ms 4652 KB Output is correct
53 Correct 118 ms 4668 KB Output is correct
54 Correct 33 ms 5184 KB Output is correct
55 Correct 30 ms 5112 KB Output is correct
56 Correct 142 ms 5124 KB Output is correct
57 Correct 37 ms 5636 KB Output is correct
58 Correct 38 ms 5624 KB Output is correct
59 Correct 167 ms 5728 KB Output is correct
60 Correct 41 ms 6164 KB Output is correct
61 Correct 40 ms 6140 KB Output is correct
62 Correct 201 ms 6132 KB Output is correct
63 Correct 150 ms 6120 KB Output is correct
64 Correct 274 ms 6244 KB Output is correct
65 Correct 206 ms 6116 KB Output is correct
66 Correct 169 ms 6108 KB Output is correct
67 Correct 167 ms 6252 KB Output is correct
68 Correct 93 ms 6136 KB Output is correct
69 Correct 93 ms 6136 KB Output is correct
70 Correct 59 ms 6148 KB Output is correct
71 Correct 67 ms 6148 KB Output is correct
72 Correct 52 ms 6108 KB Output is correct
73 Correct 61 ms 6160 KB Output is correct
74 Correct 96 ms 6132 KB Output is correct
75 Correct 120 ms 6156 KB Output is correct
76 Correct 103 ms 6148 KB Output is correct
77 Correct 102 ms 6152 KB Output is correct
78 Correct 123 ms 6292 KB Output is correct
79 Correct 82 ms 6140 KB Output is correct
80 Correct 101 ms 6132 KB Output is correct
81 Correct 108 ms 6168 KB Output is correct
82 Correct 97 ms 6140 KB Output is correct
83 Correct 131 ms 6140 KB Output is correct
84 Correct 118 ms 6152 KB Output is correct
85 Correct 130 ms 6224 KB Output is correct
86 Correct 138 ms 6148 KB Output is correct
87 Correct 146 ms 6136 KB Output is correct
88 Correct 135 ms 6136 KB Output is correct
89 Correct 132 ms 6136 KB Output is correct
90 Correct 137 ms 6144 KB Output is correct
91 Correct 136 ms 6140 KB Output is correct
92 Correct 146 ms 6128 KB Output is correct