답안 #558333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
558333 2022-05-07T06:16:36 Z Cookie Mecho (IOI09_mecho) C++14
100 / 100
182 ms 7388 KB
#include <bits/stdc++.h>
using namespace std;
#define LIFESUCKS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ld long double
#define ar array
#include<cstdio>
#define vt vector
#include<fstream>
ifstream fin("piggyback.in");
ofstream fout("piggyback.out");
#include<fstream>
#define pb push_back
#define all(c) (c).begin(), (c).end()

#define fi first
#define se second
#define vt vector
using namespace std;
int n, s, sti, stj, eni, enj;
bool vis[801][801];
int dis[801][801];
int dis2[801][801];
char a[801][801];
bool bee = true;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
bool check(int mid){
    memset(vis, false, sizeof(vis));
    memset(dis2, false, sizeof(dis2));
    queue<pair<int, int>>q;
    q.push({sti, stj});
    vis[sti][stj] = true;
    if(mid >= dis[sti][stj])return(false);
    dis2[sti][stj] = 0;
    while(!q.empty()){
        auto now = q.front(); q.pop();
        //cout << "mm";
        for(int i = 0; i < 4; i++){
            int cri = now.fi + x[i];
            int crj = now.se + y[i];
            if(cri < 0 || crj < 0 || cri >= n || crj >= n)continue;
            if(!vis[cri][crj] &&(a[cri][crj] == 'G' || a[cri][crj] == 'D' || a[cri][crj] == 'M')){
               // cout << "lol";
                if(dis[cri][crj] > floor((double)(dis2[now.fi][now.se] + 1) / (double)s) + mid){
                    vis[cri][crj] = true;
                    dis2[cri][crj] = dis2[now.fi][now.se] + 1;
                    if(a[cri][crj] == 'D')return(true);
                    q.push({cri, crj});
                }
            }
        }
    }
    //cout << "\n";
    return(false);
}
void bfs(){
    memset(vis, false, sizeof(vis));
    
    queue<pair<int, int>>q;
    
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(a[i][j] == 'H'){
                q.push({i, j});
                vis[i][j] = true;
                dis[i][j] = 0;
            }
        }
    }
    while(!q.empty()){
        
        auto now = q.front(); q.pop();
        for(int i = 0; i < 4; i++){
            int cri = now.fi + x[i];
            int crj = now.se + y[i];
            if(cri < 0 || crj < 0 || cri >= n || crj >= n)continue;
            if(!vis[cri][crj] && (a[cri][crj] == 'G' || a[cri][crj] == 'H' || a[cri][crj] == 'M')){
                vis[cri][crj] = true;
                dis[cri][crj] = dis[now.fi][now.se] + 1;
                q.push({cri, crj});
            }
        }
    }
    
}
int main(){
    LIFESUCKS;
   cin >> n >> s;
   
   for(int i = 0; i < n; i++){
       for(int j = 0; j < n; j++){
           dis[i][j] = 1e9;
           cin >> a[i][j];
           if(a[i][j] == 'M'){
               sti = i, stj = j;
           }if(a[i][j] == 'D'){
               eni = i; enj = j;
           }
       }
   }
   //cout << sti << ' ' << stj << " ";
   bfs();
   
   int l = 0;
   int r = 1e9;
   int ans = -1;
   while(l <= r){
       int mid = l + (r - l) / 2;
       if(check(mid)){
           ans = mid; l = mid + 1;
       }else{
           r = mid - 1;
       }
   }
   cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3412 KB Output is correct
2 Correct 4 ms 3496 KB Output is correct
3 Correct 4 ms 3496 KB Output is correct
4 Correct 4 ms 3412 KB Output is correct
5 Correct 4 ms 3412 KB Output is correct
6 Correct 4 ms 3412 KB Output is correct
7 Correct 113 ms 7212 KB Output is correct
8 Correct 4 ms 3412 KB Output is correct
9 Correct 4 ms 3412 KB Output is correct
10 Correct 4 ms 3412 KB Output is correct
11 Correct 4 ms 3416 KB Output is correct
12 Correct 5 ms 3668 KB Output is correct
13 Correct 4 ms 3668 KB Output is correct
14 Correct 4 ms 3668 KB Output is correct
15 Correct 6 ms 3540 KB Output is correct
16 Correct 4 ms 3540 KB Output is correct
17 Correct 4 ms 3540 KB Output is correct
18 Correct 4 ms 3536 KB Output is correct
19 Correct 4 ms 3540 KB Output is correct
20 Correct 4 ms 3540 KB Output is correct
21 Correct 5 ms 3540 KB Output is correct
22 Correct 5 ms 3540 KB Output is correct
23 Correct 4 ms 3668 KB Output is correct
24 Correct 4 ms 3668 KB Output is correct
25 Correct 5 ms 3668 KB Output is correct
26 Correct 4 ms 3668 KB Output is correct
27 Correct 4 ms 3668 KB Output is correct
28 Correct 5 ms 3668 KB Output is correct
29 Correct 6 ms 3672 KB Output is correct
30 Correct 6 ms 3668 KB Output is correct
31 Correct 4 ms 3676 KB Output is correct
32 Correct 5 ms 3668 KB Output is correct
33 Correct 8 ms 4948 KB Output is correct
34 Correct 8 ms 4948 KB Output is correct
35 Correct 23 ms 4852 KB Output is correct
36 Correct 11 ms 5076 KB Output is correct
37 Correct 11 ms 5148 KB Output is correct
38 Correct 32 ms 5076 KB Output is correct
39 Correct 9 ms 5332 KB Output is correct
40 Correct 9 ms 5332 KB Output is correct
41 Correct 37 ms 5344 KB Output is correct
42 Correct 11 ms 5588 KB Output is correct
43 Correct 11 ms 5648 KB Output is correct
44 Correct 46 ms 5672 KB Output is correct
45 Correct 13 ms 5884 KB Output is correct
46 Correct 12 ms 5880 KB Output is correct
47 Correct 57 ms 5912 KB Output is correct
48 Correct 14 ms 6100 KB Output is correct
49 Correct 14 ms 6108 KB Output is correct
50 Correct 72 ms 6112 KB Output is correct
51 Correct 16 ms 6352 KB Output is correct
52 Correct 17 ms 6400 KB Output is correct
53 Correct 79 ms 6356 KB Output is correct
54 Correct 17 ms 6604 KB Output is correct
55 Correct 17 ms 6652 KB Output is correct
56 Correct 100 ms 6688 KB Output is correct
57 Correct 20 ms 6860 KB Output is correct
58 Correct 19 ms 6808 KB Output is correct
59 Correct 106 ms 6916 KB Output is correct
60 Correct 21 ms 7064 KB Output is correct
61 Correct 21 ms 7068 KB Output is correct
62 Correct 119 ms 7104 KB Output is correct
63 Correct 106 ms 7000 KB Output is correct
64 Correct 182 ms 7028 KB Output is correct
65 Correct 174 ms 7108 KB Output is correct
66 Correct 120 ms 7104 KB Output is correct
67 Correct 114 ms 7100 KB Output is correct
68 Correct 49 ms 7120 KB Output is correct
69 Correct 47 ms 7132 KB Output is correct
70 Correct 34 ms 7124 KB Output is correct
71 Correct 37 ms 7136 KB Output is correct
72 Correct 34 ms 7056 KB Output is correct
73 Correct 41 ms 7320 KB Output is correct
74 Correct 62 ms 7300 KB Output is correct
75 Correct 69 ms 7384 KB Output is correct
76 Correct 65 ms 7320 KB Output is correct
77 Correct 69 ms 7388 KB Output is correct
78 Correct 86 ms 7352 KB Output is correct
79 Correct 62 ms 7360 KB Output is correct
80 Correct 66 ms 7292 KB Output is correct
81 Correct 71 ms 7256 KB Output is correct
82 Correct 60 ms 7296 KB Output is correct
83 Correct 88 ms 7320 KB Output is correct
84 Correct 75 ms 7240 KB Output is correct
85 Correct 77 ms 7312 KB Output is correct
86 Correct 77 ms 7240 KB Output is correct
87 Correct 88 ms 7316 KB Output is correct
88 Correct 80 ms 7276 KB Output is correct
89 Correct 83 ms 7208 KB Output is correct
90 Correct 88 ms 7316 KB Output is correct
91 Correct 84 ms 7204 KB Output is correct
92 Correct 92 ms 7204 KB Output is correct