Submission #391416

# Submission time Handle Problem Language Result Execution time Memory
391416 2021-04-18T17:26:59 Z ml1234 Mecho (IOI09_mecho) C++17
84 / 100
264 ms 6340 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 = 0, high = 2 * 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 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 157 ms 6280 KB Output is correct
8 Incorrect 1 ms 332 KB Output isn't 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 Incorrect 1 ms 460 KB Output isn't 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 332 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 460 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 2108 KB Output is correct
34 Correct 8 ms 2120 KB Output is correct
35 Correct 31 ms 2116 KB Output is correct
36 Correct 10 ms 2420 KB Output is correct
37 Correct 11 ms 2380 KB Output is correct
38 Correct 42 ms 2484 KB Output is correct
39 Correct 14 ms 2840 KB Output is correct
40 Correct 16 ms 2828 KB Output is correct
41 Correct 61 ms 2816 KB Output is correct
42 Correct 17 ms 3472 KB Output is correct
43 Correct 17 ms 3416 KB Output is correct
44 Correct 70 ms 3412 KB Output is correct
45 Correct 21 ms 3836 KB Output is correct
46 Correct 21 ms 3868 KB Output is correct
47 Correct 84 ms 3844 KB Output is correct
48 Correct 23 ms 4296 KB Output is correct
49 Correct 22 ms 4284 KB Output is correct
50 Correct 103 ms 4260 KB Output is correct
51 Correct 28 ms 4748 KB Output is correct
52 Correct 28 ms 4756 KB Output is correct
53 Correct 123 ms 4736 KB Output is correct
54 Correct 34 ms 5240 KB Output is correct
55 Correct 30 ms 5212 KB Output is correct
56 Correct 145 ms 5208 KB Output is correct
57 Correct 38 ms 5700 KB Output is correct
58 Correct 37 ms 5712 KB Output is correct
59 Correct 168 ms 5692 KB Output is correct
60 Correct 43 ms 6216 KB Output is correct
61 Correct 51 ms 6236 KB Output is correct
62 Correct 201 ms 6268 KB Output is correct
63 Correct 143 ms 6196 KB Output is correct
64 Correct 264 ms 6200 KB Output is correct
65 Correct 207 ms 6192 KB Output is correct
66 Correct 170 ms 6204 KB Output is correct
67 Incorrect 171 ms 6192 KB Output isn't correct
68 Correct 76 ms 6324 KB Output is correct
69 Correct 69 ms 6220 KB Output is correct
70 Correct 70 ms 6340 KB Output is correct
71 Correct 72 ms 6220 KB Output is correct
72 Correct 48 ms 6180 KB Output is correct
73 Correct 56 ms 6212 KB Output is correct
74 Correct 97 ms 6232 KB Output is correct
75 Correct 117 ms 6212 KB Output is correct
76 Correct 112 ms 6224 KB Output is correct
77 Correct 121 ms 6212 KB Output is correct
78 Incorrect 121 ms 6236 KB Output isn't correct
79 Correct 82 ms 6224 KB Output is correct
80 Correct 97 ms 6216 KB Output is correct
81 Correct 117 ms 6232 KB Output is correct
82 Correct 123 ms 6236 KB Output is correct
83 Correct 143 ms 6300 KB Output is correct
84 Correct 126 ms 6224 KB Output is correct
85 Correct 127 ms 6240 KB Output is correct
86 Correct 157 ms 6224 KB Output is correct
87 Correct 137 ms 6216 KB Output is correct
88 Correct 136 ms 6216 KB Output is correct
89 Correct 146 ms 6228 KB Output is correct
90 Correct 170 ms 6304 KB Output is correct
91 Correct 139 ms 6324 KB Output is correct
92 Correct 134 ms 6208 KB Output is correct