Submission #960577

# Submission time Handle Problem Language Result Execution time Memory
960577 2024-04-10T16:04:39 Z htphong0909 Mecho (IOI09_mecho) C++17
100 / 100
168 ms 17528 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

char arr[1001][1001];
int n, s;
int stx, sty, ex, ey;
bool in(int x, int y)
{
    return (min(x, y) >= 1 && max(x, y) <= n);
}

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int dBee[1001][1001];
int D[1001][1001];

bool check(int mid)
{
    memset(D, 0x3f, sizeof D);
    D[stx][sty] = mid * s;
    queue<pair<int, int>> q;
    q.emplace(stx, sty);
    while (!q.empty())
    {
        int ux, uy;
        tie(ux, uy) = q.front();
        q.pop();

        if (D[ux][uy] >= dBee[ux][uy]) continue;

        for (int i = 0; i < 4; i++)
        {
            int vx = ux + dx[i];
            int vy = uy + dy[i];
            if (in(vx, vy) && arr[vx][vy] != 'T' && D[vx][vy] > D[ux][uy] + 1)
            {
                D[vx][vy] = D[ux][uy] + 1;
                q.emplace(vx, vy);
            }
        }
    }
    if (D[ex][ey] < (int)1e18) return true;
    else return false;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen("TEST.IN", "r", stdin);
    //freopen("TEST.OUT", "w", stdout);
    cin >> n >> s;
    queue<pair<int, int>> q;
    memset(dBee, 0x3f, sizeof dBee);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> arr[i][j];
            if (arr[i][j] == 'H')
            {
                q.emplace(i, j);
                dBee[i][j] = 0;
            }
            if (arr[i][j] == 'M')
            {
                tie(stx, sty) = make_pair(i, j);
            }
            if (arr[i][j] == 'D')
            {
                tie(ex, ey) = make_pair(i, j);
            }
        }
    }
    while (!q.empty())
    {
        int ux, uy;
        tie(ux, uy) = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int vx = ux + dx[i];
            int vy = uy + dy[i];
            if (in(vx, vy) && arr[vx][vy] != 'T' && arr[vx][vy] != 'D' && dBee[vx][vy] > dBee[ux][uy] + s)
            {
                dBee[vx][vy] = dBee[ux][uy] + s;
                q.emplace(vx, vy);
            }
        }
    }
    int l = 0;
    int r = 1e9;
    int ans = -1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (check(mid))
        {
            ans = mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 16988 KB Output is correct
2 Correct 6 ms 16988 KB Output is correct
3 Correct 6 ms 16988 KB Output is correct
4 Correct 6 ms 16988 KB Output is correct
5 Correct 7 ms 17052 KB Output is correct
6 Correct 6 ms 16988 KB Output is correct
7 Correct 68 ms 17256 KB Output is correct
8 Correct 7 ms 16988 KB Output is correct
9 Correct 6 ms 16988 KB Output is correct
10 Correct 6 ms 16988 KB Output is correct
11 Correct 6 ms 17048 KB Output is correct
12 Correct 7 ms 16988 KB Output is correct
13 Correct 7 ms 17056 KB Output is correct
14 Correct 6 ms 16988 KB Output is correct
15 Correct 7 ms 16988 KB Output is correct
16 Correct 8 ms 16988 KB Output is correct
17 Correct 6 ms 16988 KB Output is correct
18 Correct 7 ms 17048 KB Output is correct
19 Correct 7 ms 16984 KB Output is correct
20 Correct 7 ms 16984 KB Output is correct
21 Correct 6 ms 16984 KB Output is correct
22 Correct 7 ms 16988 KB Output is correct
23 Correct 7 ms 16988 KB Output is correct
24 Correct 6 ms 16988 KB Output is correct
25 Correct 7 ms 16988 KB Output is correct
26 Correct 7 ms 16988 KB Output is correct
27 Correct 7 ms 17052 KB Output is correct
28 Correct 7 ms 16988 KB Output is correct
29 Correct 7 ms 16988 KB Output is correct
30 Correct 7 ms 16984 KB Output is correct
31 Correct 6 ms 16988 KB Output is correct
32 Correct 6 ms 16988 KB Output is correct
33 Correct 9 ms 16988 KB Output is correct
34 Correct 10 ms 16988 KB Output is correct
35 Correct 22 ms 17064 KB Output is correct
36 Correct 10 ms 16988 KB Output is correct
37 Correct 11 ms 17128 KB Output is correct
38 Correct 25 ms 16988 KB Output is correct
39 Correct 10 ms 16988 KB Output is correct
40 Correct 11 ms 16988 KB Output is correct
41 Correct 30 ms 16984 KB Output is correct
42 Correct 10 ms 16988 KB Output is correct
43 Correct 12 ms 16860 KB Output is correct
44 Correct 41 ms 16880 KB Output is correct
45 Correct 12 ms 16988 KB Output is correct
46 Correct 12 ms 16868 KB Output is correct
47 Correct 44 ms 17240 KB Output is correct
48 Correct 14 ms 16984 KB Output is correct
49 Correct 15 ms 16988 KB Output is correct
50 Correct 63 ms 16988 KB Output is correct
51 Correct 15 ms 17240 KB Output is correct
52 Correct 15 ms 16988 KB Output is correct
53 Correct 66 ms 16984 KB Output is correct
54 Correct 16 ms 16988 KB Output is correct
55 Correct 19 ms 16988 KB Output is correct
56 Correct 84 ms 17044 KB Output is correct
57 Correct 16 ms 16988 KB Output is correct
58 Correct 16 ms 16988 KB Output is correct
59 Correct 87 ms 16988 KB Output is correct
60 Correct 17 ms 16984 KB Output is correct
61 Correct 18 ms 16988 KB Output is correct
62 Correct 95 ms 17068 KB Output is correct
63 Correct 100 ms 17268 KB Output is correct
64 Correct 168 ms 17036 KB Output is correct
65 Correct 149 ms 16860 KB Output is correct
66 Correct 100 ms 17036 KB Output is correct
67 Correct 89 ms 16988 KB Output is correct
68 Correct 43 ms 16988 KB Output is correct
69 Correct 42 ms 16988 KB Output is correct
70 Correct 31 ms 16984 KB Output is correct
71 Correct 33 ms 16904 KB Output is correct
72 Correct 29 ms 16920 KB Output is correct
73 Correct 37 ms 17488 KB Output is correct
74 Correct 48 ms 17456 KB Output is correct
75 Correct 52 ms 17488 KB Output is correct
76 Correct 47 ms 17412 KB Output is correct
77 Correct 51 ms 17500 KB Output is correct
78 Correct 56 ms 17500 KB Output is correct
79 Correct 47 ms 17496 KB Output is correct
80 Correct 48 ms 17360 KB Output is correct
81 Correct 55 ms 17528 KB Output is correct
82 Correct 47 ms 17524 KB Output is correct
83 Correct 65 ms 17292 KB Output is correct
84 Correct 59 ms 17460 KB Output is correct
85 Correct 62 ms 17500 KB Output is correct
86 Correct 63 ms 17288 KB Output is correct
87 Correct 60 ms 17460 KB Output is correct
88 Correct 66 ms 17360 KB Output is correct
89 Correct 63 ms 17360 KB Output is correct
90 Correct 85 ms 17356 KB Output is correct
91 Correct 66 ms 17360 KB Output is correct
92 Correct 65 ms 17232 KB Output is correct