답안 #391417

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391417 2021-04-18T17:28:06 Z ml1234 Mecho (IOI09_mecho) C++17
100 / 100
269 ms 6256 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 = 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;
}
# 결과 실행 시간 메모리 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 141 ms 6132 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 336 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 464 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 460 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 2076 KB Output is correct
34 Correct 8 ms 2080 KB Output is correct
35 Correct 31 ms 2068 KB Output is correct
36 Correct 10 ms 2380 KB Output is correct
37 Correct 11 ms 2380 KB Output is correct
38 Correct 46 ms 2512 KB Output is correct
39 Correct 14 ms 2796 KB Output is correct
40 Correct 13 ms 2748 KB Output is correct
41 Correct 54 ms 2740 KB Output is correct
42 Correct 16 ms 3360 KB Output is correct
43 Correct 17 ms 3400 KB Output is correct
44 Correct 69 ms 3352 KB Output is correct
45 Correct 22 ms 3784 KB Output is correct
46 Correct 19 ms 3784 KB Output is correct
47 Correct 77 ms 3740 KB Output is correct
48 Correct 30 ms 4180 KB Output is correct
49 Correct 28 ms 4224 KB Output is correct
50 Correct 99 ms 4200 KB Output is correct
51 Correct 28 ms 4684 KB Output is correct
52 Correct 27 ms 4688 KB Output is correct
53 Correct 122 ms 4920 KB Output is correct
54 Correct 34 ms 5164 KB Output is correct
55 Correct 32 ms 5124 KB Output is correct
56 Correct 143 ms 5112 KB Output is correct
57 Correct 38 ms 5636 KB Output is correct
58 Correct 37 ms 5644 KB Output is correct
59 Correct 170 ms 5700 KB Output is correct
60 Correct 42 ms 6140 KB Output is correct
61 Correct 39 ms 6156 KB Output is correct
62 Correct 189 ms 6136 KB Output is correct
63 Correct 150 ms 6116 KB Output is correct
64 Correct 269 ms 6144 KB Output is correct
65 Correct 214 ms 6116 KB Output is correct
66 Correct 176 ms 6132 KB Output is correct
67 Correct 170 ms 6240 KB Output is correct
68 Correct 77 ms 6132 KB Output is correct
69 Correct 75 ms 6136 KB Output is correct
70 Correct 62 ms 6140 KB Output is correct
71 Correct 71 ms 6256 KB Output is correct
72 Correct 50 ms 6144 KB Output is correct
73 Correct 61 ms 6032 KB Output is correct
74 Correct 97 ms 6244 KB Output is correct
75 Correct 115 ms 6136 KB Output is correct
76 Correct 104 ms 6144 KB Output is correct
77 Correct 104 ms 6180 KB Output is correct
78 Correct 148 ms 6144 KB Output is correct
79 Correct 81 ms 6152 KB Output is correct
80 Correct 100 ms 6136 KB Output is correct
81 Correct 109 ms 6156 KB Output is correct
82 Correct 102 ms 6120 KB Output is correct
83 Correct 128 ms 6152 KB Output is correct
84 Correct 117 ms 6128 KB Output is correct
85 Correct 131 ms 6156 KB Output is correct
86 Correct 134 ms 6152 KB Output is correct
87 Correct 126 ms 6136 KB Output is correct
88 Correct 131 ms 6140 KB Output is correct
89 Correct 134 ms 6136 KB Output is correct
90 Correct 136 ms 6144 KB Output is correct
91 Correct 141 ms 6112 KB Output is correct
92 Correct 156 ms 6160 KB Output is correct