답안 #482578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
482578 2021-10-25T16:38:07 Z BackNoob Mecho (IOI09_mecho) C++14
95 / 100
188 ms 8472 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define mask(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
using namespace std;
const int mxN = 1007;
const ll inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

int n , limit;
char a[mxN][mxN];
int bee[mxN][mxN];
int dist[mxN][mxN];
pair<int , int> mecho , home;
int dx[4] = {0 , 1 , 0 , -1};
int dy[4] = {1 , 0 , -1 , 0};

bool ok(int x)
{
	for(int i = 0 ; i <= n ; i++)
	for(int j = 0 ; j <= n ; j++) dist[i][j] = inf;
	queue<pair<int , int>> q;
	q.push(mecho);
	dist[mecho.fi][mecho.se] = limit * x;

	if(bee[mecho.fi][mecho.se] <= x) return false;

	while(!q.empty()) {
		int x = q.front().fi;
		int y = q.front().se;
		q.pop();

		for(int d = 0 ; d < 4 ; d++) {
			int newx = x + dx[d];
			int newy = y + dy[d];
			if(1 <= newx && newx <= n && 1 <= newy && newy <= n && a[newx][newy] == 'G') {
				if(dist[newx][newy] > dist[x][y] + 1 && bee[newx][newy] > (dist[x][y] + 1) / limit) {
					dist[newx][newy] = dist[x][y] + 1;
					q.push({newx , newy});
				}
			}
		}
	}

	if(dist[home.fi][home.se] != dist[0][0]) return true;
	return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen("task.inp" , "r" , stdin);
   // freopen("task.out" , "w" , stdout);
    
    cin >> n >> limit;
    for(int i = 1 ; i <= n ; i++)
    for(int j = 1 ; j <= n ; j++) cin >> a[i][j];

    memset(bee, 0x3f, sizeof bee);
    queue<pair<int , int>> q;
    for(int i = 1 ; i <= n ; i++)
    for(int j = 1 ; j <= n ; j++) {
    	if(a[i][j] == 'H') {
    		bee[i][j] = 0;
    		q.push({i , j});
    	}
    	if(a[i][j] == 'M') mecho = {i , j};
    	if(a[i][j] == 'D') home = {i , j};
    }

    a[home.fi][home.se] = 'G';
    a[mecho.fi][mecho.se] = 'G';

    while(!q.empty()) {
    	int x = q.front().fi;
    	int y = q.front().se;
    	q.pop();

    	for(int d = 0 ; d < 4 ; d++) {
    		int newx = x + dx[d];
    		int newy = y + dy[d];
    		if(1 <= newx && newx <= n && 1 <= newy && newy <= n && a[newx][newy] == 'G') {
    			if(bee[newx][newy] > bee[x][y] + 1) {
    				bee[newx][newy] = bee[x][y] + 1;
    				q.push({newx , newy});
    			}
    		}
    	}
    }

    bee[home.fi][home.se] = inf;

    int lo = 0 , hi = inf;
    while(lo + 1 < hi) {
    	int mid = (lo + hi) / 2;
    	if(ok(mid)) lo = mid;
    	else hi = mid;
    }

    if(ok(lo) == false) cout << -1;
    else cout << lo;



    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4300 KB Output is correct
2 Correct 2 ms 4248 KB Output is correct
3 Correct 2 ms 4300 KB Output is correct
4 Correct 2 ms 4300 KB Output is correct
5 Correct 2 ms 4292 KB Output is correct
6 Correct 2 ms 4300 KB Output is correct
7 Correct 94 ms 8328 KB Output is correct
8 Correct 2 ms 4300 KB Output is correct
9 Correct 2 ms 4300 KB Output is correct
10 Correct 2 ms 4300 KB Output is correct
11 Correct 2 ms 4300 KB Output is correct
12 Incorrect 2 ms 4556 KB Output isn't correct
13 Correct 3 ms 4556 KB Output is correct
14 Correct 2 ms 4556 KB Output is correct
15 Correct 2 ms 4300 KB Output is correct
16 Correct 2 ms 4300 KB Output is correct
17 Correct 2 ms 4300 KB Output is correct
18 Correct 2 ms 4300 KB Output is correct
19 Correct 2 ms 4428 KB Output is correct
20 Correct 2 ms 4428 KB Output is correct
21 Correct 2 ms 4428 KB Output is correct
22 Correct 2 ms 4428 KB Output is correct
23 Correct 3 ms 4428 KB Output is correct
24 Correct 2 ms 4428 KB Output is correct
25 Correct 2 ms 4556 KB Output is correct
26 Correct 2 ms 4556 KB Output is correct
27 Correct 2 ms 4556 KB Output is correct
28 Correct 2 ms 4556 KB Output is correct
29 Correct 2 ms 4556 KB Output is correct
30 Correct 3 ms 4556 KB Output is correct
31 Correct 2 ms 4556 KB Output is correct
32 Correct 2 ms 4556 KB Output is correct
33 Correct 10 ms 5964 KB Output is correct
34 Correct 8 ms 5964 KB Output is correct
35 Correct 24 ms 6020 KB Output is correct
36 Correct 9 ms 6220 KB Output is correct
37 Correct 8 ms 6220 KB Output is correct
38 Correct 42 ms 6220 KB Output is correct
39 Correct 11 ms 6476 KB Output is correct
40 Correct 11 ms 6428 KB Output is correct
41 Correct 48 ms 6408 KB Output is correct
42 Correct 14 ms 6732 KB Output is correct
43 Correct 14 ms 6732 KB Output is correct
44 Correct 51 ms 6752 KB Output is correct
45 Correct 13 ms 7004 KB Output is correct
46 Correct 14 ms 6948 KB Output is correct
47 Correct 58 ms 6988 KB Output is correct
48 Correct 16 ms 7192 KB Output is correct
49 Correct 16 ms 7192 KB Output is correct
50 Correct 70 ms 7144 KB Output is correct
51 Correct 22 ms 7496 KB Output is correct
52 Correct 20 ms 7496 KB Output is correct
53 Correct 84 ms 7484 KB Output is correct
54 Correct 21 ms 7628 KB Output is correct
55 Correct 24 ms 7676 KB Output is correct
56 Correct 93 ms 7628 KB Output is correct
57 Correct 22 ms 7988 KB Output is correct
58 Correct 23 ms 7996 KB Output is correct
59 Correct 111 ms 7884 KB Output is correct
60 Correct 26 ms 8156 KB Output is correct
61 Correct 25 ms 8140 KB Output is correct
62 Correct 165 ms 8228 KB Output is correct
63 Correct 102 ms 8132 KB Output is correct
64 Correct 177 ms 8140 KB Output is correct
65 Correct 188 ms 8216 KB Output is correct
66 Correct 132 ms 8220 KB Output is correct
67 Correct 118 ms 8328 KB Output is correct
68 Correct 53 ms 8140 KB Output is correct
69 Correct 60 ms 8244 KB Output is correct
70 Correct 51 ms 8248 KB Output is correct
71 Correct 42 ms 8180 KB Output is correct
72 Correct 38 ms 8176 KB Output is correct
73 Correct 44 ms 8428 KB Output is correct
74 Correct 65 ms 8420 KB Output is correct
75 Correct 73 ms 8432 KB Output is correct
76 Correct 91 ms 8436 KB Output is correct
77 Correct 69 ms 8388 KB Output is correct
78 Correct 77 ms 8412 KB Output is correct
79 Correct 61 ms 8460 KB Output is correct
80 Correct 79 ms 8468 KB Output is correct
81 Correct 70 ms 8388 KB Output is correct
82 Correct 63 ms 8472 KB Output is correct
83 Correct 75 ms 8396 KB Output is correct
84 Correct 74 ms 8372 KB Output is correct
85 Correct 74 ms 8388 KB Output is correct
86 Correct 92 ms 8428 KB Output is correct
87 Correct 78 ms 8396 KB Output is correct
88 Correct 89 ms 8396 KB Output is correct
89 Correct 88 ms 8376 KB Output is correct
90 Correct 96 ms 8376 KB Output is correct
91 Correct 96 ms 8380 KB Output is correct
92 Correct 104 ms 8320 KB Output is correct