Submission #286241

# Submission time Handle Problem Language Result Execution time Memory
286241 2020-08-30T08:46:03 Z AaronNaidu Mecho (IOI09_mecho) C++14
100 / 100
218 ms 6960 KB
#include <bits/stdc++.h>
using namespace std;

char board[801][801];
int beeDist[801][801];
int mechoDist[801][801];
int n, s;
pair<int, int> home, mecho;
vector<pair<int, int> > hives;

bool canEscapeAfter(int a) {
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            mechoDist[i][j] = -1;    
        }
    }
    queue<pair<int, int> > q;
    q.push(mecho);
    mechoDist[mecho.first][mecho.second] = s * a;
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        if (x == home.first and y == home.second)
        {
            return true;
        }
        if (mechoDist[x][y]/s >= beeDist[x][y])
        {
            continue;
        }
        if (0 <= x+1 and x+1 < n)
        {
            if (board[x+1][y] == 'G' or board[x+1][y] == 'D')
            {
                if (mechoDist[x+1][y] == -1)
                {
                    mechoDist[x+1][y] = mechoDist[x][y] + 1;
                    q.push({x+1, y});
                }
            }
        }
        if (0 <= x-1 and x-1 < n)
        {
            if (board[x-1][y] == 'G' or board[x-1][y] == 'D')
            {
                if (mechoDist[x-1][y] == -1)
                {
                    mechoDist[x-1][y] = mechoDist[x][y] + 1;
                    q.push({x-1, y});
                }
            }
        }
        if (0 <= y+1 and y+1 < n)
        {
            if (board[x][y+1] == 'G' or board[x][y+1] == 'D')
            {
                if (mechoDist[x][y+1] == -1)
                {
                    mechoDist[x][y+1] = mechoDist[x][y] + 1;
                    q.push({x, y+1});
                }
            }
        }
        if (0 <= y-1 and y-1 < n)
        {
            if (board[x][y-1] == 'G' or board[x][y-1] == 'D')
            {
                if (mechoDist[x][y-1] == -1)
                {
                    mechoDist[x][y-1] = mechoDist[x][y] + 1;
                    q.push({x, y-1});
                }
            }
        }
    } 
    return false;
}

int main() {
    cin >> n >> s;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> board[i][j];
            beeDist[i][j] = -1;
            if (board[i][j] == 'H')
            {
                hives.push_back({i,j});
            }
            if (board[i][j] == 'M')
            {
                mecho = {i,j};
            }
            if (board[i][j] == 'D')
            {
                home = {i,j};
            }
        }
    }
    queue<pair<int, int> > bees;
    for (auto i : hives)
    {
        bees.push(i);
        beeDist[i.first][i.second] = 0;
    }
    while (!bees.empty())
    {
        int x = bees.front().first;
        int y = bees.front().second;
        bees.pop();
        if (0 <= x+1 and x+1 < n)
        {
            if (board[x+1][y] == 'G' or board[x+1][y] == 'M')
            {
                if (beeDist[x+1][y] == -1)
                {
                    beeDist[x+1][y] = beeDist[x][y] + 1;
                    bees.push({x+1, y});
                }
            }
        }
        if (0 <= x-1 and x-1 < n)
        {
            if (board[x-1][y] == 'G' or board[x-1][y] == 'M')
            {
                if (beeDist[x-1][y] == -1)
                {
                    beeDist[x-1][y] = beeDist[x][y] + 1;
                    bees.push({x-1, y});
                }
            }
        }
        if (0 <= y+1 and y+1 < n)
        {
            if (board[x][y+1] == 'G' or board[x][y+1] == 'M')
            {
                if (beeDist[x][y+1] == -1)
                {
                    beeDist[x][y+1] = beeDist[x][y] + 1;
                    bees.push({x, y+1});
                }
            }
        }
        if (0 <= y-1 and y-1 < n)
        {
            if (board[x][y-1] == 'G' or board[x][y-1] == 'M')
            {
                if (beeDist[x][y-1] == -1)
                {
                    beeDist[x][y-1] = beeDist[x][y] + 1;
                    bees.push({x, y-1});
                }
            }
        }
    }
    int regionStart = 0;
    int regionEnd = beeDist[mecho.first][mecho.second] - 1;
    //cout << "End on " << regionEnd << "\n";
    int ans = -1;
    while (regionStart <= regionEnd)
    {
        int med = (regionStart + regionEnd)/2;
       // cout << "Checking " << med << "\n";
        if (canEscapeAfter(med))
        {
            ans = med;
            regionStart = med + 1;
          //  cout << "Works\n";
        }
        else
        {
            regionEnd = med - 1;
           // cout << "Does'nt work\n";
        }
    }
    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 131 ms 6776 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 768 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 1 ms 768 KB Output is correct
26 Correct 1 ms 768 KB Output is correct
27 Correct 1 ms 768 KB Output is correct
28 Correct 1 ms 768 KB Output is correct
29 Correct 1 ms 768 KB Output is correct
30 Correct 1 ms 768 KB Output is correct
31 Correct 1 ms 896 KB Output is correct
32 Correct 1 ms 896 KB Output is correct
33 Correct 15 ms 2944 KB Output is correct
34 Correct 15 ms 2944 KB Output is correct
35 Correct 27 ms 2944 KB Output is correct
36 Correct 19 ms 3328 KB Output is correct
37 Correct 21 ms 3328 KB Output is correct
38 Correct 34 ms 3328 KB Output is correct
39 Correct 24 ms 3704 KB Output is correct
40 Correct 24 ms 3712 KB Output is correct
41 Correct 43 ms 3704 KB Output is correct
42 Correct 31 ms 4088 KB Output is correct
43 Correct 29 ms 4096 KB Output is correct
44 Correct 57 ms 4096 KB Output is correct
45 Correct 36 ms 4480 KB Output is correct
46 Correct 35 ms 4480 KB Output is correct
47 Correct 65 ms 4480 KB Output is correct
48 Correct 42 ms 4856 KB Output is correct
49 Correct 41 ms 4992 KB Output is correct
50 Correct 80 ms 4984 KB Output is correct
51 Correct 50 ms 5368 KB Output is correct
52 Correct 48 ms 5368 KB Output is correct
53 Correct 96 ms 5372 KB Output is correct
54 Correct 58 ms 5752 KB Output is correct
55 Correct 57 ms 5752 KB Output is correct
56 Correct 117 ms 5752 KB Output is correct
57 Correct 66 ms 6136 KB Output is correct
58 Correct 71 ms 6212 KB Output is correct
59 Correct 138 ms 6264 KB Output is correct
60 Correct 75 ms 6652 KB Output is correct
61 Correct 80 ms 6648 KB Output is correct
62 Correct 165 ms 6648 KB Output is correct
63 Correct 165 ms 6648 KB Output is correct
64 Correct 217 ms 6648 KB Output is correct
65 Correct 218 ms 6776 KB Output is correct
66 Correct 174 ms 6776 KB Output is correct
67 Correct 157 ms 6776 KB Output is correct
68 Correct 93 ms 6648 KB Output is correct
69 Correct 90 ms 6632 KB Output is correct
70 Correct 93 ms 6648 KB Output is correct
71 Correct 80 ms 6648 KB Output is correct
72 Correct 83 ms 6648 KB Output is correct
73 Correct 81 ms 6904 KB Output is correct
74 Correct 103 ms 6904 KB Output is correct
75 Correct 138 ms 6960 KB Output is correct
76 Correct 104 ms 6904 KB Output is correct
77 Correct 104 ms 6904 KB Output is correct
78 Correct 109 ms 6904 KB Output is correct
79 Correct 122 ms 6904 KB Output is correct
80 Correct 105 ms 6904 KB Output is correct
81 Correct 105 ms 6904 KB Output is correct
82 Correct 100 ms 6904 KB Output is correct
83 Correct 121 ms 6776 KB Output is correct
84 Correct 108 ms 6776 KB Output is correct
85 Correct 110 ms 6776 KB Output is correct
86 Correct 124 ms 6880 KB Output is correct
87 Correct 121 ms 6776 KB Output is correct
88 Correct 131 ms 6776 KB Output is correct
89 Correct 131 ms 6776 KB Output is correct
90 Correct 138 ms 6776 KB Output is correct
91 Correct 135 ms 6776 KB Output is correct
92 Correct 134 ms 6776 KB Output is correct