Submission #798965

# Submission time Handle Problem Language Result Execution time Memory
798965 2023-07-31T08:09:10 Z yandabao Mecho (IOI09_mecho) C++14
54 / 100
230 ms 10712 KB
#include <iostream>
#include <vector>
#include <limits>
#include <map>
#include <algorithm>
#include <set>
#include <string>
#include <cmath>
#include <climits>
#include <string.h>
#include <fstream>
#include <stack>
#include <queue>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vb = vector<bool>;
using vvi= vector<vector<int> >;
using vll = vector<long long>;
using vpi = vector<pair<int, int> >;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int, int>;
#define f first
#define s second
#define mp make_pair
//cin.tie(0)->sync_with_stdio(0);

int N, S;
vi dx, dy;
pi end;
vector<vector<char> > field;
vvi fromHive;


void BFS(int a, int b, vvi& fromMecho, int ad){
    queue<pair<pair<int, int>, int> > q;
    q.push(mp(mp(a, b), 0));

    while(!q.empty()){
        pair<pi, int> curr = q.front(); q.pop();
        if(field[curr.f.f][curr.f.s] == 'T'){
            continue;
        }
        if(fromHive[curr.f.f][curr.f.s] < curr.s/S + ad + (curr.s%S == 0 ? 0 : 1)){
            continue;
        }
        if(curr.s%S == 0){
            if(fromHive[curr.f.f][curr.f.s] == curr.s/S + ad + (curr.s%S == 0 ? 0 : 1)){
                continue;
            }
        }
        if(fromMecho[curr.f.f][curr.f.s] > curr.s){
            fromMecho[curr.f.f][curr.f.s] = curr.s;
            for (int i = 0; i < 4; i++)
            {
                if(curr.f.f + dx[i] < 0 || curr.f.f + dx[i] >= N || curr.f.s + dy[i] < 0 || curr.f.s >= N){
                    continue;
                }
                q.push(mp(mp(curr.f.f + dx[i], curr.f.s + dy[i]), curr.s + 1));
            }
        }
    }
}

int main(){
    cin >> N >> S;
    dx = {-1, 1, 0, 0};
    dy = {0, 0, -1, 1};
    vi row(N, INT_MAX);
    fromHive.rsz(N);
    field.rsz(N);
    for (int i = 0; i < N; i++)
    {
        fromHive[i] = row;
        field[i].rsz(N);
    }

    pi start, end;
    vpi hive;

    for (int i = 0; i < N; i++)
    {
        string str; cin >> str;
        for (int j = 0; j < N; j++)
        {
            field[i][j] = str[j];
            if(str[j] == 'M'){
                start = mp(i, j);
            }
            if(str[j] == 'D'){
                end = mp(i, j);
            }
            if(str[j] == 'H'){
                hive.pb(mp(i, j));
            }
        }
    }

    queue<pair<pair<int, int>, int> > q;
    for (int i = 0; i < sz(hive); i++)
    {
        q.push(mp(hive[i], 0));
    }

    while(!q.empty()){
        pair<pi, int> curr = q.front(); q.pop();
        if(field[curr.f.f][curr.f.s] != 'G' && field[curr.f.f][curr.f.s] != 'H'){
            continue;
        }
        if(fromHive[curr.f.f][curr.f.s] > curr.s){
            fromHive[curr.f.f][curr.f.s] = curr.s;
            for (int i = 0; i < 4; i++)
            {
                if(curr.f.f + dx[i] < 0 || curr.f.f + dx[i] >= N || curr.f.s + dy[i] < 0 || curr.f.s + dy[i] >= N){
                    continue;
                }
                q.push(mp(mp(curr.f.f + dx[i], curr.f.s + dy[i]), curr.s + 1));
            }
        }
    }

    int lo = 0, hi = INT_MAX/2;
    while(lo < hi){
        int mid = (lo + hi + 1)/2;
        vi row(N, INT_MAX);
        vvi fromMecho(N);
        for (int i = 0; i < N; i++)
        {
            fromMecho[i] = row;
        }
        BFS(start.f, start.s, fromMecho, mid);
        // for (int i = 0; i < sz(fromMecho); i++)
        // {
        //     for (int j = 0; j < N; j++)
        //     {
        //         cout << fromMecho[i][j]/S + mid + (fromMecho[i][j]%S == 0 ? 0 : 1) << " ";
        //     }
        //     cout << endl;
        // }
        // cout << endl;
        if(fromMecho[end.f][end.s] == INT_MAX){
            hi = mid - 1;
        } else {
            lo = mid;
        }
    }
    cout << lo << endl; 

    // for (int i = 0; i < N; i++)
    // {
    //     for (int j = 0; j < N; j++)
    //     {
    //         cout << fromHive[i][j] << " ";
    //     }
    //     cout << endl;
    // }


}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Runtime error 1 ms 340 KB Execution killed with signal 6
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 168 ms 6064 KB Output is correct
8 Incorrect 0 ms 212 KB Output isn't correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Incorrect 1 ms 340 KB Output isn't correct
13 Runtime error 1 ms 468 KB Execution killed with signal 6
14 Incorrect 1 ms 340 KB Output isn't correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 7 ms 1364 KB Output is correct
34 Correct 8 ms 1312 KB Output is correct
35 Runtime error 10 ms 2700 KB Execution killed with signal 6
36 Correct 8 ms 1752 KB Output is correct
37 Correct 10 ms 1748 KB Output is correct
38 Correct 48 ms 1864 KB Output is correct
39 Correct 10 ms 2132 KB Output is correct
40 Correct 11 ms 2004 KB Output is correct
41 Runtime error 16 ms 4116 KB Execution killed with signal 6
42 Correct 13 ms 2464 KB Output is correct
43 Correct 15 ms 2516 KB Output is correct
44 Correct 74 ms 2572 KB Output is correct
45 Correct 18 ms 3016 KB Output is correct
46 Correct 14 ms 2900 KB Output is correct
47 Runtime error 28 ms 6084 KB Execution killed with signal 6
48 Correct 17 ms 3540 KB Output is correct
49 Correct 20 ms 3540 KB Output is correct
50 Correct 114 ms 3548 KB Output is correct
51 Correct 26 ms 4032 KB Output is correct
52 Correct 22 ms 4052 KB Output is correct
53 Runtime error 40 ms 8224 KB Execution killed with signal 6
54 Correct 25 ms 4680 KB Output is correct
55 Correct 27 ms 4680 KB Output is correct
56 Correct 155 ms 4704 KB Output is correct
57 Correct 24 ms 5288 KB Output is correct
58 Correct 24 ms 5284 KB Output is correct
59 Runtime error 63 ms 10712 KB Execution killed with signal 6
60 Correct 29 ms 5932 KB Output is correct
61 Correct 30 ms 5916 KB Output is correct
62 Correct 204 ms 6124 KB Output is correct
63 Correct 145 ms 6112 KB Output is correct
64 Correct 225 ms 6172 KB Output is correct
65 Correct 228 ms 6124 KB Output is correct
66 Correct 230 ms 6112 KB Output is correct
67 Incorrect 171 ms 6104 KB Output isn't correct
68 Correct 81 ms 6148 KB Output is correct
69 Correct 69 ms 6152 KB Output is correct
70 Correct 65 ms 6132 KB Output is correct
71 Correct 63 ms 6148 KB Output is correct
72 Incorrect 60 ms 6172 KB Output isn't correct
73 Incorrect 111 ms 6172 KB Output isn't correct
74 Correct 106 ms 6160 KB Output is correct
75 Correct 109 ms 6116 KB Output is correct
76 Correct 103 ms 6120 KB Output is correct
77 Correct 109 ms 6120 KB Output is correct
78 Incorrect 128 ms 6112 KB Output isn't correct
79 Correct 88 ms 6196 KB Output is correct
80 Correct 103 ms 6176 KB Output is correct
81 Correct 115 ms 6104 KB Output is correct
82 Correct 105 ms 6184 KB Output is correct
83 Correct 134 ms 6152 KB Output is correct
84 Correct 123 ms 6172 KB Output is correct
85 Correct 118 ms 6152 KB Output is correct
86 Correct 128 ms 6184 KB Output is correct
87 Correct 122 ms 6096 KB Output is correct
88 Correct 141 ms 6116 KB Output is correct
89 Correct 134 ms 6164 KB Output is correct
90 Correct 143 ms 6152 KB Output is correct
91 Correct 137 ms 6160 KB Output is correct
92 Correct 140 ms 6148 KB Output is correct