답안 #1100016

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100016 2024-10-12T11:43:14 Z asli_bg Mecho (IOI09_mecho) C++11
23 / 100
1000 ms 26472 KB
#pragma GCC optimize("O3,unroll-loops")

#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define inset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>

#define int long long

#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vii;

#define cont(x) for(auto el:x) {cout<<el<<' ';} cout<<endl;
//#define endl '\n'
#define contp(x) for(auto el:x) {cout<<el.fi<<'-'<<el.se<<' ';} cout<<endl;
#define pb push_back

#define sp <<' '<<

#define DEBUG(x) {cout<<(#x) sp x<<endl;}

const int MAXN=805;
const int INF=1e9;

int n,s;
vector<string> grid;
set<pii> ari;
int mark[MAXN][MAXN];

set<pii> hive;

pii bas,son;
int yon1[4]={1,-1,0,0};
int yon2[4]={0,0,-1,1};
//sag, sol, asagi, yukari

bool isvalid(int x,int y,bool f){
    if(x>=n or y>=n or x<0 or y<0) return false;
    if(f){
        if(grid[x][y]!='G' and grid[x][y]!='M') return false;
        if(hive.count({x,y})) return false;
    }
    else{
        if(grid[x][y]!='G' and grid[x][y]!='D') return false;
    }
    return true;
}

bool check(int deg){
    //düz shortest path bfs
    vector<vb> vis(n+1, vb(n+1,false));
    queue<pair<pii,int>> q;

    if(mark[bas.fi][bas.se]<deg) return false;

    q.push({bas,0});
    vis[bas.fi][bas.se]=true;

    //cout<<"DEEEEEG" sp deg<<endl;

    while(!q.empty()){
        auto el=q.front();
        q.pop();

        int adim=el.se;
        int x=el.fi.fi;
        int y=el.fi.se;
        //cout<<"control" sp adim sp x sp y<<endl;

        FOR(i,4){
            int yenix=x+yon1[i];
            int yeniy=y+yon2[i];

            if(!isvalid(yenix,yeniy,false)) continue;
            if(vis[yenix][yeniy]) continue;
            
            int sure=adim/s;
            //DEBUG(sure);
            //DEBUG(yenix);
            //DEBUG(yeniy);
            if(mark[yenix][yeniy]<deg+sure) continue;
            if((adim+1)%s==0){
                if(mark[yenix][yeniy]==deg+sure) continue;   
            }

            q.push({{yenix,yeniy},adim+1});
            vis[yenix][yeniy]=true;
        }
    }

    return vis[son.fi][son.se];
}

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

    cin>>n>>s;
    grid.resize(n);
    FOR(i,n) cin>>grid[i];

    FOR(i,n){
        FOR(j,n){
            if(grid[i][j]=='H'){
                hive.insert({i,j});
                mark[i][j]=0;
            }
            if(grid[i][j]=='M') {
                bas={i,j};
            }
            if(grid[i][j]=='D'){
                son={i,j};
                mark[i][j]=INF;
            }
        }
    }

    for(int i=0;true;i++){ //x kez ilerle
        set<pii> tut;

        for(auto el:hive){
            FOR(i,4){
                int yenix=el.fi+yon1[i];
                int yeniy=el.se+yon2[i];
                if(isvalid(yenix,yeniy,true)){
                    tut.insert({yenix,yeniy});
                }
            }
        }

        if(tut.empty()) break;

        for(auto el:tut){
            mark[el.fi][el.se]=i+1;
            hive.insert(el);
        }
    }

    /*FOR(i,n){
        FOR(j,n) cout<<mark[i][j]<<' ';
        cout<<endl;
    }
    cout<<endl;*/


    int l=0;
    int r=n+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if(check(mid)) l=mid;
        else r=mid;
    }

    cout<<l-1<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Execution timed out 1101 ms 22520 KB Time limit exceeded
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Incorrect 33 ms 2752 KB Output isn't correct
13 Correct 9 ms 2652 KB Output is correct
14 Correct 17 ms 2648 KB Output is correct
15 Incorrect 1 ms 348 KB Output isn't correct
16 Incorrect 1 ms 348 KB Output isn't correct
17 Incorrect 2 ms 348 KB Output isn't correct
18 Incorrect 1 ms 348 KB Output isn't correct
19 Incorrect 5 ms 2616 KB Output isn't correct
20 Incorrect 3 ms 2652 KB Output isn't correct
21 Incorrect 17 ms 2616 KB Output isn't correct
22 Incorrect 9 ms 2652 KB Output isn't correct
23 Incorrect 54 ms 2616 KB Output isn't correct
24 Incorrect 25 ms 2652 KB Output isn't correct
25 Incorrect 132 ms 2564 KB Output isn't correct
26 Incorrect 68 ms 2712 KB Output isn't correct
27 Incorrect 194 ms 2580 KB Output isn't correct
28 Incorrect 91 ms 2652 KB Output isn't correct
29 Incorrect 266 ms 2644 KB Output isn't correct
30 Incorrect 120 ms 2652 KB Output isn't correct
31 Incorrect 351 ms 2644 KB Output isn't correct
32 Incorrect 171 ms 2652 KB Output isn't correct
33 Execution timed out 1069 ms 2900 KB Time limit exceeded
34 Execution timed out 1071 ms 3156 KB Time limit exceeded
35 Execution timed out 1072 ms 6664 KB Time limit exceeded
36 Execution timed out 1038 ms 2896 KB Time limit exceeded
37 Execution timed out 1037 ms 5200 KB Time limit exceeded
38 Execution timed out 1044 ms 8728 KB Time limit exceeded
39 Execution timed out 1070 ms 2900 KB Time limit exceeded
40 Execution timed out 1048 ms 5200 KB Time limit exceeded
41 Execution timed out 1052 ms 9104 KB Time limit exceeded
42 Execution timed out 1059 ms 2900 KB Time limit exceeded
43 Execution timed out 1071 ms 3084 KB Time limit exceeded
44 Execution timed out 1062 ms 9312 KB Time limit exceeded
45 Execution timed out 1066 ms 2904 KB Time limit exceeded
46 Execution timed out 1072 ms 3156 KB Time limit exceeded
47 Execution timed out 1049 ms 9512 KB Time limit exceeded
48 Execution timed out 1020 ms 3120 KB Time limit exceeded
49 Execution timed out 1069 ms 3200 KB Time limit exceeded
50 Execution timed out 1071 ms 9508 KB Time limit exceeded
51 Execution timed out 1040 ms 3156 KB Time limit exceeded
52 Execution timed out 1081 ms 3540 KB Time limit exceeded
53 Execution timed out 1076 ms 9480 KB Time limit exceeded
54 Execution timed out 1024 ms 1156 KB Time limit exceeded
55 Execution timed out 1039 ms 1360 KB Time limit exceeded
56 Execution timed out 1051 ms 9808 KB Time limit exceeded
57 Execution timed out 1038 ms 1364 KB Time limit exceeded
58 Execution timed out 1033 ms 1360 KB Time limit exceeded
59 Execution timed out 1068 ms 10312 KB Time limit exceeded
60 Execution timed out 1025 ms 1360 KB Time limit exceeded
61 Execution timed out 1025 ms 1400 KB Time limit exceeded
62 Execution timed out 1097 ms 10500 KB Time limit exceeded
63 Execution timed out 1073 ms 8276 KB Time limit exceeded
64 Execution timed out 1044 ms 5004 KB Time limit exceeded
65 Execution timed out 1041 ms 4860 KB Time limit exceeded
66 Execution timed out 1040 ms 8272 KB Time limit exceeded
67 Execution timed out 1045 ms 5444 KB Time limit exceeded
68 Execution timed out 1002 ms 18360 KB Time limit exceeded
69 Execution timed out 1065 ms 18676 KB Time limit exceeded
70 Execution timed out 1022 ms 18388 KB Time limit exceeded
71 Execution timed out 1043 ms 18656 KB Time limit exceeded
72 Execution timed out 1041 ms 18760 KB Time limit exceeded
73 Execution timed out 1050 ms 26192 KB Time limit exceeded
74 Execution timed out 1066 ms 26320 KB Time limit exceeded
75 Execution timed out 1075 ms 26456 KB Time limit exceeded
76 Execution timed out 1067 ms 26472 KB Time limit exceeded
77 Execution timed out 1072 ms 26272 KB Time limit exceeded
78 Execution timed out 1058 ms 25428 KB Time limit exceeded
79 Execution timed out 1043 ms 25684 KB Time limit exceeded
80 Execution timed out 1073 ms 25584 KB Time limit exceeded
81 Execution timed out 1066 ms 25464 KB Time limit exceeded
82 Execution timed out 1090 ms 25680 KB Time limit exceeded
83 Execution timed out 1073 ms 25784 KB Time limit exceeded
84 Execution timed out 1046 ms 25896 KB Time limit exceeded
85 Execution timed out 1103 ms 26140 KB Time limit exceeded
86 Execution timed out 1050 ms 26172 KB Time limit exceeded
87 Execution timed out 1014 ms 25812 KB Time limit exceeded
88 Execution timed out 1046 ms 25336 KB Time limit exceeded
89 Execution timed out 1057 ms 25348 KB Time limit exceeded
90 Execution timed out 1018 ms 25388 KB Time limit exceeded
91 Execution timed out 1030 ms 25304 KB Time limit exceeded
92 Execution timed out 1060 ms 25428 KB Time limit exceeded