Submission #998330

# Submission time Handle Problem Language Result Execution time Memory
998330 2024-06-13T15:48:45 Z AtinaR Mecho (IOI09_mecho) C++14
100 / 100
137 ms 11904 KB
#include <bits/stdc++.h>

using namespace std;
const long long MAX=810;
long long n,s;
char mat[MAX][MAX];
long long si,sj,ei,ej;
long long di[4]= {0,0,-1,1};
long long dj[4]= {1,-1,0,0};
vector<pair<long long,long long> > hives;
long long distmecho[MAX][MAX];
long long occupied[MAX][MAX];
bool canspreadhive(long long i, long long j)
{
    if(i<0 || i>=n || j<0 || j>=n || occupied[i][j]!=-1 || mat[i][j]=='T' || mat[i][j]=='D')return false;
    return true;
}
void spread_hives()
{
    memset(occupied,-1,sizeof(occupied));
    queue<pair<long long,long long> > q;
    for(auto x:hives)
    {
        q.push(x);
        occupied[x.first][x.second]=0;
    }
    while(!q.empty())
    {
        auto curr=q.front();
        q.pop();
        for(long long k=0; k<4; k++)
        {
            long long newi=curr.first+di[k];
            long long newj=curr.second+dj[k];
            if(!canspreadhive(newi,newj))continue;
            occupied[newi][newj]=occupied[curr.first][curr.second]+1;
            q.push({newi,newj});
        }
    }
}
bool canmechostep(long long i, long long j)
{
    if(i<0 || i>=n || j<0 || j>=n || distmecho[i][j]!=-1 || mat[i][j]=='H' || mat[i][j]=='T')return false;
    return true;
}
bool reached(int mechodis, int beedis)
{
    return mechodis/s<beedis;
}
bool can_stay(long long X)
{
    memset(distmecho,-1,sizeof(distmecho));
    distmecho[si][sj]=0;
    queue<pair<long long,long long> > q;
    q.push({si,sj});
    if(occupied[si][sj]<=X)return false;
    while(!q.empty())
    {
        auto curr=q.front();
        q.pop();
        for(long long k=0; k<4; k++)
        {
            long long newi=curr.first+di[k];
            long long newj=curr.second+dj[k];
            if(!canmechostep(newi,newj))continue;
            long long newtime=distmecho[curr.first][curr.second]+1;
            if(newi==ei && newj==ej)return true;
            if(reached(newtime,occupied[newi][newj]-X))
            {
                distmecho[newi][newj]=newtime;
                q.push({newi,newj});
            }
        }
    }
    if(distmecho[ei][ej]!=-1)return true;
    else return false;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>s;
    for(long long i=0; i<n; i++)
    {
        for(long long j=0; j<n; j++)
        {
            cin>>mat[i][j];
            if(mat[i][j]=='M')
            {
                si=i;
                sj=j;
            }
            else if(mat[i][j]=='D')
            {
                ei=i;
                ej=j;
            }
            else if(mat[i][j]=='H')
            {
                hives.push_back({i,j});
            }
        }
    }
    spread_hives();
    long long B=0,E=1e6;
    long long res=-1;
    while(B<=E)
    {
        long long mid=(B+E)/2;
        if(can_stay(mid))
        {
            res=mid;
            B=mid+1;
        }
        else
        {
            E=mid-1;
        }
    }
    cout<<res<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10840 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 5 ms 10844 KB Output is correct
6 Correct 4 ms 10844 KB Output is correct
7 Correct 74 ms 11604 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 4 ms 10844 KB Output is correct
10 Correct 3 ms 10844 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 3 ms 10888 KB Output is correct
18 Correct 3 ms 10840 KB Output is correct
19 Correct 3 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 4 ms 10844 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10844 KB Output is correct
24 Correct 3 ms 10844 KB Output is correct
25 Correct 3 ms 10844 KB Output is correct
26 Correct 3 ms 10844 KB Output is correct
27 Correct 3 ms 10844 KB Output is correct
28 Correct 3 ms 10844 KB Output is correct
29 Correct 4 ms 10844 KB Output is correct
30 Correct 4 ms 10844 KB Output is correct
31 Correct 3 ms 10844 KB Output is correct
32 Correct 3 ms 10836 KB Output is correct
33 Correct 5 ms 11012 KB Output is correct
34 Correct 5 ms 10844 KB Output is correct
35 Correct 16 ms 11024 KB Output is correct
36 Correct 6 ms 10844 KB Output is correct
37 Correct 6 ms 10844 KB Output is correct
38 Correct 20 ms 10844 KB Output is correct
39 Correct 7 ms 10844 KB Output is correct
40 Correct 6 ms 11084 KB Output is correct
41 Correct 34 ms 11100 KB Output is correct
42 Correct 8 ms 11100 KB Output is correct
43 Correct 7 ms 10964 KB Output is correct
44 Correct 32 ms 11096 KB Output is correct
45 Correct 8 ms 11100 KB Output is correct
46 Correct 9 ms 10948 KB Output is correct
47 Correct 39 ms 11176 KB Output is correct
48 Correct 9 ms 11096 KB Output is correct
49 Correct 9 ms 11096 KB Output is correct
50 Correct 46 ms 11096 KB Output is correct
51 Correct 10 ms 11096 KB Output is correct
52 Correct 10 ms 11240 KB Output is correct
53 Correct 53 ms 11096 KB Output is correct
54 Correct 11 ms 11096 KB Output is correct
55 Correct 11 ms 11096 KB Output is correct
56 Correct 62 ms 11268 KB Output is correct
57 Correct 12 ms 11096 KB Output is correct
58 Correct 12 ms 11100 KB Output is correct
59 Correct 69 ms 11336 KB Output is correct
60 Correct 14 ms 11124 KB Output is correct
61 Correct 13 ms 11352 KB Output is correct
62 Correct 88 ms 11372 KB Output is correct
63 Correct 79 ms 11356 KB Output is correct
64 Correct 122 ms 11612 KB Output is correct
65 Correct 137 ms 11364 KB Output is correct
66 Correct 99 ms 11356 KB Output is correct
67 Correct 85 ms 11360 KB Output is correct
68 Correct 36 ms 11344 KB Output is correct
69 Correct 37 ms 11352 KB Output is correct
70 Correct 35 ms 11256 KB Output is correct
71 Correct 30 ms 11356 KB Output is correct
72 Correct 25 ms 11356 KB Output is correct
73 Correct 29 ms 11752 KB Output is correct
74 Correct 43 ms 11752 KB Output is correct
75 Correct 48 ms 11868 KB Output is correct
76 Correct 43 ms 11864 KB Output is correct
77 Correct 62 ms 11868 KB Output is correct
78 Correct 49 ms 11868 KB Output is correct
79 Correct 40 ms 11868 KB Output is correct
80 Correct 44 ms 11856 KB Output is correct
81 Correct 48 ms 11904 KB Output is correct
82 Correct 38 ms 11868 KB Output is correct
83 Correct 55 ms 11544 KB Output is correct
84 Correct 46 ms 11612 KB Output is correct
85 Correct 49 ms 11612 KB Output is correct
86 Correct 54 ms 11612 KB Output is correct
87 Correct 53 ms 11608 KB Output is correct
88 Correct 55 ms 11608 KB Output is correct
89 Correct 60 ms 11612 KB Output is correct
90 Correct 65 ms 11608 KB Output is correct
91 Correct 57 ms 11608 KB Output is correct
92 Correct 60 ms 11608 KB Output is correct