답안 #960576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960576 2024-04-10T16:02:33 Z htphong0909 Mecho (IOI09_mecho) C++17
95 / 100
158 ms 17628 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' && 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 16988 KB Output is correct
2 Correct 6 ms 17052 KB Output is correct
3 Correct 8 ms 17052 KB Output is correct
4 Correct 7 ms 16988 KB Output is correct
5 Correct 7 ms 17052 KB Output is correct
6 Correct 7 ms 16988 KB Output is correct
7 Correct 73 ms 17260 KB Output is correct
8 Correct 7 ms 16984 KB Output is correct
9 Correct 7 ms 17052 KB Output is correct
10 Correct 7 ms 16988 KB Output is correct
11 Correct 6 ms 16984 KB Output is correct
12 Incorrect 9 ms 17056 KB Output isn't correct
13 Correct 7 ms 16988 KB Output is correct
14 Correct 7 ms 16988 KB Output is correct
15 Correct 7 ms 16988 KB Output is correct
16 Correct 7 ms 16988 KB Output is correct
17 Correct 7 ms 16988 KB Output is correct
18 Correct 7 ms 16988 KB Output is correct
19 Correct 7 ms 16988 KB Output is correct
20 Correct 7 ms 16988 KB Output is correct
21 Correct 6 ms 16988 KB Output is correct
22 Correct 7 ms 16988 KB Output is correct
23 Correct 7 ms 16988 KB Output is correct
24 Correct 9 ms 16988 KB Output is correct
25 Correct 7 ms 16984 KB Output is correct
26 Correct 7 ms 16988 KB Output is correct
27 Correct 7 ms 16988 KB Output is correct
28 Correct 8 ms 16984 KB Output is correct
29 Correct 7 ms 16988 KB Output is correct
30 Correct 7 ms 16988 KB Output is correct
31 Correct 7 ms 16984 KB Output is correct
32 Correct 7 ms 17056 KB Output is correct
33 Correct 9 ms 16988 KB Output is correct
34 Correct 10 ms 16984 KB Output is correct
35 Correct 21 ms 16860 KB Output is correct
36 Correct 9 ms 16984 KB Output is correct
37 Correct 10 ms 16988 KB Output is correct
38 Correct 27 ms 16988 KB Output is correct
39 Correct 10 ms 16984 KB Output is correct
40 Correct 12 ms 17060 KB Output is correct
41 Correct 31 ms 16988 KB Output is correct
42 Correct 11 ms 16988 KB Output is correct
43 Correct 11 ms 16988 KB Output is correct
44 Correct 40 ms 16988 KB Output is correct
45 Correct 12 ms 16988 KB Output is correct
46 Correct 12 ms 17056 KB Output is correct
47 Correct 47 ms 17048 KB Output is correct
48 Correct 13 ms 16984 KB Output is correct
49 Correct 14 ms 16988 KB Output is correct
50 Correct 54 ms 16988 KB Output is correct
51 Correct 15 ms 16868 KB Output is correct
52 Correct 16 ms 16984 KB Output is correct
53 Correct 68 ms 17048 KB Output is correct
54 Correct 17 ms 16984 KB Output is correct
55 Correct 18 ms 16988 KB Output is correct
56 Correct 87 ms 17048 KB Output is correct
57 Correct 18 ms 16988 KB Output is correct
58 Correct 20 ms 16988 KB Output is correct
59 Correct 86 ms 16988 KB Output is correct
60 Correct 18 ms 16988 KB Output is correct
61 Correct 24 ms 16864 KB Output is correct
62 Correct 127 ms 16984 KB Output is correct
63 Correct 85 ms 17048 KB Output is correct
64 Correct 145 ms 17040 KB Output is correct
65 Correct 158 ms 17040 KB Output is correct
66 Correct 99 ms 17044 KB Output is correct
67 Correct 101 ms 16988 KB Output is correct
68 Correct 44 ms 16976 KB Output is correct
69 Correct 43 ms 16976 KB Output is correct
70 Correct 30 ms 17092 KB Output is correct
71 Correct 32 ms 16920 KB Output is correct
72 Correct 29 ms 16988 KB Output is correct
73 Correct 34 ms 17356 KB Output is correct
74 Correct 48 ms 17556 KB Output is correct
75 Correct 52 ms 17588 KB Output is correct
76 Correct 58 ms 17628 KB Output is correct
77 Correct 63 ms 17500 KB Output is correct
78 Correct 63 ms 17528 KB Output is correct
79 Correct 45 ms 17500 KB Output is correct
80 Correct 51 ms 17500 KB Output is correct
81 Correct 57 ms 17332 KB Output is correct
82 Correct 47 ms 17496 KB Output is correct
83 Correct 68 ms 17452 KB Output is correct
84 Correct 62 ms 17292 KB Output is correct
85 Correct 56 ms 17460 KB Output is correct
86 Correct 60 ms 17280 KB Output is correct
87 Correct 59 ms 17460 KB Output is correct
88 Correct 69 ms 17244 KB Output is correct
89 Correct 64 ms 17232 KB Output is correct
90 Correct 74 ms 17356 KB Output is correct
91 Correct 64 ms 17356 KB Output is correct
92 Correct 79 ms 17240 KB Output is correct