답안 #953297

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
953297 2024-03-25T19:33:47 Z sandry24 Mecho (IOI09_mecho) C++17
100 / 100
618 ms 7356 KB
#include <bits/stdc++.h>
using namespace std;
 
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

int n, s;
pi st, en;
char a[805][805];
bool visited[805][805];
int dist[805][805];
int dist2[805][805];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

bool is_possible(pi temp, int type, int time){
    if(temp.f <= 0 || temp.s <= 0 || temp.f > n || temp.s > n)
        return false;
    else if(visited[temp.f][temp.s] || a[temp.f][temp.s] == 'T' || a[temp.f][temp.s] == 'H')
        return false;
    if(type && dist[temp.f][temp.s] <= time)
        return false;
    if(!type && a[temp.f][temp.s] == 'D')
        return false;
    return true;
}

void bfs(vector<pi> b, int type, int time){ //type = 0 if bees
    queue<pi> q;
    for(auto start : b){
        q.push(start);
        visited[start.f][start.s] = 1;
        if(!type)
            dist[start.f][start.s] = 0;
        else 
            dist2[start.f][start.s] = time;
    }
    while(!q.empty()){
        pi current = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            pi temp = {current.f + dx[i], current.s + dy[i]};
            if(is_possible(temp, type, dist2[current.f][current.s] + 1)){
                q.push(temp);
                visited[temp.f][temp.s] = 1;
                if(!type)
                    dist[temp.f][temp.s] = dist[current.f][current.s] + 1;
                else 
                    dist2[temp.f][temp.s] = dist2[current.f][current.s] + 1;
            }
        }
    }
}

void clear(){
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            dist2[i][j] = 0;
            visited[i][j] = 0;
        }
    }
}

void solve(){
    cin >> n >> s;
    vector<pi> bees;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> a[i][j];
            if(a[i][j] == 'H')
                bees.pb({i, j});
            if(a[i][j] == 'M')
                st = {i, j};
            if(a[i][j] == 'D')
                en = {i, j};
        }
    }
    bfs(bees, 0, 0);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            dist[i][j] = s * dist[i][j]; //1 step for bees = s for bear
    if(!visited[st.f][st.s]){ //bees cant reach bear
        cout << -1 << '\n';
        return;
    }
    dist[en.f][en.s] = 1e9; //bees cant go inside home
    //binary search for how much time to eat honey
    int current = -1;
    for(int i = 1e9; i > 0; i /= 2){
        while(true){
            if(dist[st.f][st.s] <= current+i)
                break;
            clear();
            bfs({st}, 1, current+i);
            if(visited[en.f][en.s])
                current += i;
            else
                break;
        }
    }
    if(current == -1){ //bear cant reach home
        cout << -1 << '\n';
        return;
    }
    cout << current / s << '\n';
}   
 
int main(){
    //freopen("infasuratoare.in", "r", stdin);
    //freopen("infasuratoare.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 337 ms 6900 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 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 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Correct 1 ms 4808 KB Output is correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 7 ms 5468 KB Output is correct
34 Correct 7 ms 5468 KB Output is correct
35 Correct 100 ms 5468 KB Output is correct
36 Correct 7 ms 5724 KB Output is correct
37 Correct 8 ms 5596 KB Output is correct
38 Correct 137 ms 5724 KB Output is correct
39 Correct 10 ms 5988 KB Output is correct
40 Correct 12 ms 5976 KB Output is correct
41 Correct 173 ms 5980 KB Output is correct
42 Correct 10 ms 5976 KB Output is correct
43 Correct 17 ms 6140 KB Output is correct
44 Correct 230 ms 6124 KB Output is correct
45 Correct 14 ms 6236 KB Output is correct
46 Correct 16 ms 6108 KB Output is correct
47 Correct 256 ms 6288 KB Output is correct
48 Correct 17 ms 6364 KB Output is correct
49 Correct 21 ms 6456 KB Output is correct
50 Correct 337 ms 6612 KB Output is correct
51 Correct 20 ms 6492 KB Output is correct
52 Correct 20 ms 6488 KB Output is correct
53 Correct 398 ms 6740 KB Output is correct
54 Correct 25 ms 6744 KB Output is correct
55 Correct 25 ms 6760 KB Output is correct
56 Correct 451 ms 6760 KB Output is correct
57 Correct 23 ms 6488 KB Output is correct
58 Correct 31 ms 6736 KB Output is correct
59 Correct 618 ms 6776 KB Output is correct
60 Correct 26 ms 6748 KB Output is correct
61 Correct 25 ms 6740 KB Output is correct
62 Correct 552 ms 6876 KB Output is correct
63 Correct 160 ms 6624 KB Output is correct
64 Correct 513 ms 6784 KB Output is correct
65 Correct 376 ms 6740 KB Output is correct
66 Correct 227 ms 6788 KB Output is correct
67 Correct 168 ms 6632 KB Output is correct
68 Correct 106 ms 6648 KB Output is correct
69 Correct 80 ms 6652 KB Output is correct
70 Correct 60 ms 6736 KB Output is correct
71 Correct 59 ms 6740 KB Output is correct
72 Correct 45 ms 6652 KB Output is correct
73 Correct 76 ms 6992 KB Output is correct
74 Correct 220 ms 6992 KB Output is correct
75 Correct 194 ms 7356 KB Output is correct
76 Correct 182 ms 7108 KB Output is correct
77 Correct 184 ms 7104 KB Output is correct
78 Correct 187 ms 7064 KB Output is correct
79 Correct 196 ms 6900 KB Output is correct
80 Correct 186 ms 7060 KB Output is correct
81 Correct 233 ms 7156 KB Output is correct
82 Correct 207 ms 7068 KB Output is correct
83 Correct 200 ms 7016 KB Output is correct
84 Correct 324 ms 7020 KB Output is correct
85 Correct 250 ms 6992 KB Output is correct
86 Correct 252 ms 7028 KB Output is correct
87 Correct 295 ms 7020 KB Output is correct
88 Correct 258 ms 6992 KB Output is correct
89 Correct 418 ms 6748 KB Output is correct
90 Correct 286 ms 6956 KB Output is correct
91 Correct 312 ms 6796 KB Output is correct
92 Correct 297 ms 6740 KB Output is correct