Submission #893254

# Submission time Handle Problem Language Result Execution time Memory
893254 2023-12-26T18:43:23 Z maxFedorchuk Mecho (IOI09_mecho) C++14
100 / 100
352 ms 13552 KB
#include <bits/stdc++.h>
using namespace std;

const long long MX=850;
char ch[MX][MX];

long long bdg[MX][MX];
long long mech[MX][MX];

long long n,k;

long long homex;
long long homey;

queue < pair < long long , long long > > qb;
queue < pair < long long , long long > > qm;

long long corx[4]={0,0,1,-1};
long long cory[4]={1,-1,0,0};

void cn1()
{
    if(qb.empty())
    {
        return;
    }

    long long zrzn=bdg[qb.front().first][qb.front().second];

    while(!qb.empty())
    {
        long long zarx=qb.front().first;
        long long zary=qb.front().second;

        if(bdg[zarx][zary]!=zrzn)
        {
            return;
        }

        qb.pop();

        for(long long i=0;i<4;i++)
        {
            if(!bdg[zarx+corx[i]][zary+cory[i]] && (ch[zarx+corx[i]][zary+cory[i]]=='G' || ch[zarx+corx[i]][zary+cory[i]]=='M'))
            {
                bdg[zarx+corx[i]][zary+cory[i]]=bdg[zarx][zary]+1;
                qb.push({zarx+corx[i],zary+cory[i]});
            }
        }
    }

    return;
}

bool cn2()
{
    while(!qm.empty())
    {
        if(bdg[qm.front().first][qm.front().second])
        {
            qm.pop();
        }
        else
        {
            break;
        }
    }

    if(qm.empty())
    {
        return 0;
    }

    long long zrzn=mech[qm.front().first][qm.front().second]+k;

    while(!qm.empty())
    {
        long long zarx=qm.front().first;
        long long zary=qm.front().second;

        if(mech[zarx][zary]>=zrzn)
        {
            return 1;
        }

        qm.pop();

        if(bdg[zarx][zary])
        {
            continue;
        }

        for(long long i=0;i<4;i++)
        {
            if(!mech[zarx+corx[i]][zary+cory[i]] && !bdg[zarx+corx[i]][zary+cory[i]] && (ch[zarx+corx[i]][zary+cory[i]]=='G' || ch[zarx+corx[i]][zary+cory[i]]=='D'))
            {
                mech[zarx+corx[i]][zary+cory[i]]=mech[zarx][zary]+1;
                qm.push({zarx+corx[i],zary+cory[i]});
            }
        }

    }

    return 1;
}

bool can(long long zar)
{
    while(!qb.empty())
    {
        qb.pop();
    }
    while(!qm.empty())
    {
        qm.pop();
    }

    for(long long i=1;i<=n;i++)
    {
        for(long long j=1;j<=n;j++)
        {
            bdg[i][j]=0;
            mech[i][j]=0;

            if(ch[i][j]=='H')
            {
                bdg[i][j]=1;
                qb.push({i,j});
            }

            if(ch[i][j]=='M')
            {
                mech[i][j]=1;
                qm.push({i,j});
            }
        }
    }

    for(long long i=0;i<zar;i++)
    {
        cn1();
    }

    while(true)
    {
        if(!cn2())
        {
            return 0;
        }

        if(mech[homex][homey])
        {
            return 1;
        }

        cn1();
    }
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    cin>>n>>k;

    for(long long i=0;i<=(n+1);i++)
    {
        for(long long j=0;j<=(n+1);j++)
        {
            ch[i][j]='T';
        }
    }

    for(long long i=1;i<=n;i++)
    {
        for(long long j=1;j<=n;j++)
        {
            cin>>ch[i][j];

            if(ch[i][j]=='D')
            {
                homex=i;
                homey=j;
            }
        }
    }

    long long l=0,r=n*n;

    while(l+1<r)
    {
        long long mid=(l+r)/2;

        if(can(mid))
        {
            l=mid;
        }
        else
        {
            r=mid;
        }
    }

    if(!can(l))
    {
        cout<<"-1\n";
        return 0;
    }

    cout<<l<<"\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2516 KB Output is correct
5 Correct 1 ms 2520 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 236 ms 12964 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2536 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 2 ms 4700 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 4564 KB Output is correct
20 Correct 1 ms 4848 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 1 ms 4696 KB Output is correct
27 Correct 1 ms 4564 KB Output is correct
28 Correct 2 ms 4564 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 1 ms 4696 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 4696 KB Output is correct
33 Correct 23 ms 9264 KB Output is correct
34 Correct 18 ms 9260 KB Output is correct
35 Correct 26 ms 9052 KB Output is correct
36 Correct 31 ms 9344 KB Output is correct
37 Correct 30 ms 9344 KB Output is correct
38 Correct 36 ms 9308 KB Output is correct
39 Correct 38 ms 11480 KB Output is correct
40 Correct 33 ms 11228 KB Output is correct
41 Correct 44 ms 11468 KB Output is correct
42 Correct 50 ms 11568 KB Output is correct
43 Correct 42 ms 11548 KB Output is correct
44 Correct 58 ms 11560 KB Output is correct
45 Correct 68 ms 11608 KB Output is correct
46 Correct 59 ms 11636 KB Output is correct
47 Correct 68 ms 11612 KB Output is correct
48 Correct 69 ms 11740 KB Output is correct
49 Correct 54 ms 11988 KB Output is correct
50 Correct 117 ms 11612 KB Output is correct
51 Correct 90 ms 11844 KB Output is correct
52 Correct 74 ms 11860 KB Output is correct
53 Correct 98 ms 11620 KB Output is correct
54 Correct 107 ms 11964 KB Output is correct
55 Correct 78 ms 11868 KB Output is correct
56 Correct 138 ms 11956 KB Output is correct
57 Correct 109 ms 12124 KB Output is correct
58 Correct 110 ms 12252 KB Output is correct
59 Correct 144 ms 12292 KB Output is correct
60 Correct 124 ms 12724 KB Output is correct
61 Correct 108 ms 12520 KB Output is correct
62 Correct 215 ms 12744 KB Output is correct
63 Correct 245 ms 12728 KB Output is correct
64 Correct 280 ms 12728 KB Output is correct
65 Correct 247 ms 12732 KB Output is correct
66 Correct 255 ms 12516 KB Output is correct
67 Correct 251 ms 12732 KB Output is correct
68 Correct 352 ms 12792 KB Output is correct
69 Correct 220 ms 12636 KB Output is correct
70 Correct 280 ms 12784 KB Output is correct
71 Correct 259 ms 12788 KB Output is correct
72 Correct 271 ms 12792 KB Output is correct
73 Correct 189 ms 13284 KB Output is correct
74 Correct 227 ms 13292 KB Output is correct
75 Correct 252 ms 13412 KB Output is correct
76 Correct 247 ms 13552 KB Output is correct
77 Correct 245 ms 13256 KB Output is correct
78 Correct 221 ms 13228 KB Output is correct
79 Correct 223 ms 13228 KB Output is correct
80 Correct 244 ms 13484 KB Output is correct
81 Correct 228 ms 13024 KB Output is correct
82 Correct 222 ms 13144 KB Output is correct
83 Correct 224 ms 13148 KB Output is correct
84 Correct 244 ms 13152 KB Output is correct
85 Correct 255 ms 13148 KB Output is correct
86 Correct 250 ms 13148 KB Output is correct
87 Correct 232 ms 13148 KB Output is correct
88 Correct 228 ms 13312 KB Output is correct
89 Correct 229 ms 12892 KB Output is correct
90 Correct 224 ms 12888 KB Output is correct
91 Correct 244 ms 13056 KB Output is correct
92 Correct 217 ms 13048 KB Output is correct