답안 #693243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
693243 2023-02-02T13:43:11 Z Sunbae Mecho (IOI09_mecho) C++17
100 / 100
191 ms 6964 KB
#include <bits/stdc++.h>
#define infi INT_MAX
using namespace std;
using pii = pair<int,int>;
const int N = 801;
vector<pii>path = {{-1, 0}, {+1, 0}, {0, -1}, {0, +1}}; 
int n, s;
char grid[N][N];
int bees[N][N];
bool vis[N][N];
int dist[N][N];
int sr, sc;
int dr, dc;
 
queue<pii>Q;
queue<pair<int, pii>>q;
bool Valid(int i, int j){
    return (i>=0 && j>=0 && i<n && j<n); 
}
void initial(int type){
    if(type == 0){
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++){
                bees[i][j] = infi;
            }
        }
    }else if(type == 1){
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++){
                dist[i][j] = infi;
            }
        }
    }
}
bool check(int wait){
    if(wait >= bees[sr][sc]) return false;
    memset(vis, false, sizeof(vis));
    initial(1);
    q.push({sr, {sc, 0}}); dist[sr][sc] = wait;
    while(!q.empty()){
        int i = q.front().first;
        int j = q.front().second.first;
        int cnt = q.front().second.second;
        q.pop(); 
        if(dist[i][j] == infi) continue;
        for(pii it: path){
            int x = i + it.first; int y = j + it.second;
            int distXY = dist[i][j] + ((cnt+1)%s == 0); 
            if(Valid(x, y) && !vis[x][y] && grid[x][y] != 'T' && grid[x][y] != 'H' && distXY < bees[x][y]){
                dist[x][y] = distXY;
                vis[x][y] = true;
                q.push({x, {y, cnt+1}});
            }
        }
    }
    if(dist[dr][dc] == infi) return false;
    else return true; 
}
int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin>>n>>s;
    initial(0); memset(vis, false, sizeof(vis));
    for(int i = 0; i<n; i++){
        string s; cin>>s;
        for(int j = 0; j<n; j++){
            grid[i][j] = s[j];
            if(grid[i][j] == 'M'){
                sr = i; sc = j;
            }else if(grid[i][j] == 'D'){
                dr = i; dc= j;
            }else if(grid[i][j] == 'H'){
                Q.push({i, j}); bees[i][j] = 0; vis[i][j] = true; 
            }
        } 
    }
    while(!Q.empty()){
        int i = Q.front().first; 
        int j = Q.front().second;
        Q.pop();
        for(pii it: path){
            int x = i + it.first; int y = j + it.second;
            if(Valid(x, y) && !vis[x][y] && grid[x][y] != 'T' && grid[x][y] != 'D'){
                vis[x][y] = true;
                bees[x][y] = bees[i][j] + 1; 
                Q.push({x, y}); 
            }
        }
    }
    int l = 0, h = n*n;
    int ans = -1;
    while(l<=h){
        int mid = l + (h-l)/2;
        if(check(mid)){
            l = mid+1;
            ans = mid;
        }else{
            h = mid-1;
        }
    }
    cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 78 ms 6720 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 1364 KB Output is correct
13 Correct 1 ms 1236 KB Output is correct
14 Correct 1 ms 1364 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1236 KB Output is correct
22 Correct 1 ms 1236 KB Output is correct
23 Correct 1 ms 1236 KB Output is correct
24 Correct 1 ms 1236 KB Output is correct
25 Correct 1 ms 1364 KB Output is correct
26 Correct 1 ms 1364 KB Output is correct
27 Correct 1 ms 1364 KB Output is correct
28 Correct 1 ms 1364 KB Output is correct
29 Correct 1 ms 1364 KB Output is correct
30 Correct 1 ms 1364 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 1 ms 1492 KB Output is correct
33 Correct 5 ms 3440 KB Output is correct
34 Correct 5 ms 3412 KB Output is correct
35 Correct 18 ms 3444 KB Output is correct
36 Correct 6 ms 3804 KB Output is correct
37 Correct 7 ms 3796 KB Output is correct
38 Correct 23 ms 3816 KB Output is correct
39 Correct 7 ms 4052 KB Output is correct
40 Correct 8 ms 4052 KB Output is correct
41 Correct 34 ms 4052 KB Output is correct
42 Correct 8 ms 4436 KB Output is correct
43 Correct 10 ms 4436 KB Output is correct
44 Correct 40 ms 4504 KB Output is correct
45 Correct 15 ms 4820 KB Output is correct
46 Correct 10 ms 4872 KB Output is correct
47 Correct 45 ms 4880 KB Output is correct
48 Correct 11 ms 5136 KB Output is correct
49 Correct 12 ms 5136 KB Output is correct
50 Correct 55 ms 5216 KB Output is correct
51 Correct 14 ms 5464 KB Output is correct
52 Correct 15 ms 5460 KB Output is correct
53 Correct 76 ms 5568 KB Output is correct
54 Correct 14 ms 5916 KB Output is correct
55 Correct 15 ms 5844 KB Output is correct
56 Correct 88 ms 5940 KB Output is correct
57 Correct 16 ms 6228 KB Output is correct
58 Correct 17 ms 6292 KB Output is correct
59 Correct 107 ms 6244 KB Output is correct
60 Correct 19 ms 6612 KB Output is correct
61 Correct 20 ms 6508 KB Output is correct
62 Correct 112 ms 6636 KB Output is correct
63 Correct 97 ms 6560 KB Output is correct
64 Correct 191 ms 6612 KB Output is correct
65 Correct 139 ms 6616 KB Output is correct
66 Correct 122 ms 6612 KB Output is correct
67 Correct 107 ms 6616 KB Output is correct
68 Correct 41 ms 6580 KB Output is correct
69 Correct 50 ms 6584 KB Output is correct
70 Correct 30 ms 6576 KB Output is correct
71 Correct 34 ms 6640 KB Output is correct
72 Correct 24 ms 6576 KB Output is correct
73 Correct 23 ms 6912 KB Output is correct
74 Correct 63 ms 6840 KB Output is correct
75 Correct 70 ms 6896 KB Output is correct
76 Correct 50 ms 6964 KB Output is correct
77 Correct 66 ms 6888 KB Output is correct
78 Correct 67 ms 6812 KB Output is correct
79 Correct 44 ms 6860 KB Output is correct
80 Correct 50 ms 6808 KB Output is correct
81 Correct 60 ms 6932 KB Output is correct
82 Correct 60 ms 6860 KB Output is correct
83 Correct 63 ms 6752 KB Output is correct
84 Correct 55 ms 6828 KB Output is correct
85 Correct 70 ms 6828 KB Output is correct
86 Correct 60 ms 6776 KB Output is correct
87 Correct 71 ms 6764 KB Output is correct
88 Correct 68 ms 6772 KB Output is correct
89 Correct 69 ms 6784 KB Output is correct
90 Correct 74 ms 6772 KB Output is correct
91 Correct 64 ms 6740 KB Output is correct
92 Correct 76 ms 6768 KB Output is correct