Submission #442470

# Submission time Handle Problem Language Result Execution time Memory
442470 2021-07-07T20:01:15 Z dawangk Mecho (IOI09_mecho) C++14
38 / 100
209 ms 9424 KB
#include <bits/stdc++.h>
using namespace std;
#define inputJunk ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug cout<<"HERE"<<endl;
#define ell "\n"

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<pi, int> trip;
typedef pair<pll, ll> tripll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<trip> vtrip;

const int INF = 1e9+1212;
const ll P = 9973, M = 1e9+9;
const int MM = 8e2+5; int mod = 1e9+7;//998244353

int n, k, sx, sy, ex, ey, dist[MM][MM]; char grid[MM][MM];
queue<pi> q; pi dir[4] {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
pi step[MM][MM];

bool vv(int a){
    return a>=0&&a<n;
}

bool valid(int x, int y){
    return vv(x)&&vv(y)&&(grid[x][y]=='G'||grid[x][y]=='M')&dist[x][y]==-1;
}

bool dfs(int t){
    queue<pi> q;
    for(int i = 0;i<n;i++){
        for(int j = 0;j<n;j++){
            step[i][j] = {-1, 0};
        }
    }
    step[sx][sy] = {t, 0};
    q.push({sx, sy});
    while(!q.empty()){
        int x = q.front().f, y = q.front().s; q.pop();
        for(int i = 0;i<4;i++){
            int newx = x+dir[i].f, newy = y+dir[i].s;
            //cout<<newx<<" "<<newy<<endl;
            if(vv(newx)&&vv(newy)&&(grid[newx][newy]=='G'||grid[newx][newy]=='D')&&step[x][y].f<dist[newx][newy]&&step[newx][newy].f==-1){
                int newt = (step[x][y].s+1==k)?step[x][y].f+1:step[x][y].f;
                int newused = (step[x][y].s+1)%k;
                step[newx][newy] = {newt, newused};
                q.push({newx, newy});
            }
        }
    }
    return step[ex][ey].f!=-1;
}

int main(){
    inputJunk
    cin>>n>>k;
    for(int i = 0;i<n;i++){
        for(int j = 0;j<n;j++){
            cin>>grid[i][j];
            dist[i][j] = -1;
            if(grid[i][j]=='M'){sx = i; sy = j;}
            else if(grid[i][j]=='D'){ex = i;ey = j;}
            else if(grid[i][j]=='H'){q.push({i, j}); dist[i][j] = 0;}
        }
    }

    while(!q.empty()){
        int curx = q.front().f, cury = q.front().s;q.pop();
        //cout<<curx<<" "<<cury<<" "<<endl;
        for(int i = 0;i<4;i++){
            int newx = curx+dir[i].f, newy = cury+dir[i].s;
            if(valid(newx, newy)){
                dist[newx][newy] = dist[curx][cury]+1;
                q.push({newx, newy});
            }
        }
    }
/*
    for(int i = 0;i<n;i++){
        for(int j = 0;j<n;j++){
            cout<<dist[i][j]<<" ";
        }cout<<ell;
    }*/
    dist[ex][ey] = INF;
    int lo = 0, hi = 100000;
    if(!dfs(0)){
        cout<<-1<<endl;
        return 0;
    }
    //cout<<"BOB"<<dfss(2)<<ell;

    while(lo<hi){
        //cout<<lo<<" "<<hi<<endl;
        int mid = (lo+hi+1)/2;
        if(dfs(mid)){
            lo = mid;
        }else{
            hi = mid-1;
        }
    }
    cout<<lo<<endl;

}
/*CUSTOM TEST CASE 1
*/

/*CUSTOM TEST CASE 2
*/

/*CUSTOM TEST CASE 3
*/

/*Comments of Shame
- floating error (use integer division instead)
- cin vs getline
- upperbound and lowerbound returns iteratorsf
- use long long when number is big enough
- for dp invalid cases needs to be skipped
- some base cases won't work (review cow poetry)
- always check bounds, some TLE are due to incorrect bounds!
- dont mess up return types
= RESET ARRAYS!!!!!!!!!!
- USE UR BRAIN
- INF setting problems
*/
/*
    freopen("time.in","r", stdin);
    freopen("time.out","w", stdout);
*/

Compilation message

mecho.cpp: In function 'bool valid(int, int)':
mecho.cpp:36:71: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
   36 |     return vv(x)&&vv(y)&&(grid[x][y]=='G'||grid[x][y]=='M')&dist[x][y]==-1;
      |                                                             ~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Incorrect 1 ms 332 KB Output isn't correct
4 Incorrect 0 ms 328 KB Output isn't correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 135 ms 9192 KB Output is correct
8 Incorrect 1 ms 332 KB Output isn't correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Incorrect 1 ms 716 KB Output isn't correct
13 Incorrect 1 ms 724 KB Output isn't correct
14 Correct 1 ms 716 KB Output is correct
15 Incorrect 1 ms 460 KB Output isn't correct
16 Correct 1 ms 456 KB Output is correct
17 Incorrect 1 ms 452 KB Output isn't correct
18 Correct 1 ms 456 KB Output is correct
19 Incorrect 1 ms 460 KB Output isn't correct
20 Correct 1 ms 460 KB Output is correct
21 Incorrect 1 ms 588 KB Output isn't correct
22 Correct 1 ms 588 KB Output is correct
23 Incorrect 1 ms 716 KB Output isn't correct
24 Correct 1 ms 716 KB Output is correct
25 Incorrect 1 ms 844 KB Output isn't correct
26 Correct 1 ms 716 KB Output is correct
27 Incorrect 1 ms 844 KB Output isn't correct
28 Correct 1 ms 844 KB Output is correct
29 Incorrect 1 ms 844 KB Output isn't correct
30 Correct 1 ms 844 KB Output is correct
31 Incorrect 1 ms 972 KB Output isn't correct
32 Correct 1 ms 844 KB Output is correct
33 Incorrect 10 ms 3908 KB Output isn't correct
34 Correct 10 ms 3996 KB Output is correct
35 Correct 33 ms 3916 KB Output is correct
36 Incorrect 12 ms 4580 KB Output isn't correct
37 Correct 13 ms 4508 KB Output is correct
38 Correct 43 ms 4556 KB Output is correct
39 Incorrect 16 ms 5032 KB Output isn't correct
40 Correct 17 ms 5108 KB Output is correct
41 Correct 57 ms 5116 KB Output is correct
42 Incorrect 24 ms 5580 KB Output isn't correct
43 Correct 19 ms 5580 KB Output is correct
44 Correct 72 ms 5580 KB Output is correct
45 Incorrect 32 ms 6220 KB Output isn't correct
46 Correct 24 ms 6244 KB Output is correct
47 Correct 85 ms 6252 KB Output is correct
48 Incorrect 41 ms 6732 KB Output isn't correct
49 Correct 28 ms 6716 KB Output is correct
50 Correct 102 ms 6804 KB Output is correct
51 Incorrect 54 ms 7372 KB Output isn't correct
52 Incorrect 39 ms 7372 KB Output isn't correct
53 Correct 142 ms 7396 KB Output is correct
54 Incorrect 63 ms 8032 KB Output isn't correct
55 Incorrect 56 ms 7876 KB Output isn't correct
56 Correct 150 ms 7876 KB Output is correct
57 Incorrect 73 ms 8524 KB Output isn't correct
58 Incorrect 77 ms 8428 KB Output isn't correct
59 Correct 163 ms 8544 KB Output is correct
60 Incorrect 87 ms 9216 KB Output isn't correct
61 Incorrect 92 ms 9028 KB Output isn't correct
62 Correct 209 ms 9132 KB Output is correct
63 Correct 135 ms 9204 KB Output is correct
64 Correct 209 ms 9092 KB Output is correct
65 Correct 186 ms 9136 KB Output is correct
66 Incorrect 162 ms 9028 KB Output isn't correct
67 Correct 43 ms 9080 KB Output is correct
68 Correct 67 ms 9156 KB Output is correct
69 Correct 72 ms 9156 KB Output is correct
70 Correct 58 ms 9176 KB Output is correct
71 Correct 65 ms 9112 KB Output is correct
72 Incorrect 59 ms 9284 KB Output isn't correct
73 Incorrect 72 ms 9368 KB Output isn't correct
74 Correct 102 ms 9376 KB Output is correct
75 Correct 105 ms 9412 KB Output is correct
76 Correct 101 ms 9372 KB Output is correct
77 Correct 109 ms 9424 KB Output is correct
78 Correct 46 ms 9284 KB Output is correct
79 Correct 112 ms 9316 KB Output is correct
80 Correct 104 ms 9256 KB Output is correct
81 Correct 114 ms 9336 KB Output is correct
82 Correct 92 ms 9336 KB Output is correct
83 Correct 115 ms 9340 KB Output is correct
84 Correct 149 ms 9308 KB Output is correct
85 Correct 153 ms 9284 KB Output is correct
86 Correct 120 ms 9332 KB Output is correct
87 Correct 120 ms 9344 KB Output is correct
88 Correct 125 ms 9240 KB Output is correct
89 Correct 140 ms 9284 KB Output is correct
90 Correct 132 ms 9284 KB Output is correct
91 Correct 144 ms 9252 KB Output is correct
92 Correct 150 ms 9236 KB Output is correct