답안 #980550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
980550 2024-05-12T08:36:36 Z BF001 Mecho (IOI09_mecho) C++17
84 / 100
155 ms 7004 KB
#include <bits/stdc++.h>
using namespace std;

#define N 805
#define fi first
#define se second

typedef pair<int, int> ii;

int n, d[N][N], d1[N][N], xs, ys, xe, ye, s; 
vector<ii> o;
char a[N][N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

bool in(int u, int v){
	return u >= 1 && u <= n && v >= 1 && v <= n && a[u][v] != 'T'; 
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
        
    queue<ii> q;

    cin >> n >> s;
    
    for (int i = 1; i <= n; i++){
    	for (int j = 1; j <= n; j++){
    		d1[i][j] = d[i][j] = 1e9;
    	}
    }

    for (int i = 1; i <= n; i++){
    	for (int j = 1; j <= n; j++){
    		cin >> a[i][j];
    		if (a[i][j] == 'H') {
    			d1[i][j] = 0;
    			q.push({i, j});
    		}
    		if (a[i][j] == 'D'){
    			xe = i;
    			ye = j;
    		} 
    		if (a[i][j] == 'M'){
    			xs = i;
    			ys = j;
    		}
    	}
    }

    while (!q.empty()){
    	int u = q.front().fi;
    	int v = q.front().se;
    	q.pop();
    	for (int i = 0; i <= 3; i++){
    		int newu = u + dx[i];
    		int newv = v + dy[i];
    		if (!in(newu, newv)) continue;
    		if (newu == xe && newv == ye) continue;
    		if (d1[newu][newv] > d1[u][v] + 1){
    			d1[newu][newv] = d1[u][v] + 1;
    			q.push({newu, newv});
    		}
    	}
    }

    int l = 0, r = n * n, rt = -1;
    while (l <= r){
    	int mid = (l + r) >> 1;
    	for (int i = 1; i <= n; i++){
    		for (int j = 1; j <= n; j++) d[i][j] = 1e9;
    	}
    	d[xs][ys] = 0;
    	q.push({xs, ys});
		while (!q.empty()){
    		int u = q.front().fi;
    		int v = q.front().se;
    		q.pop();
    		for (int i = 0; i <= 3; i++){
    			int newu = u + dx[i];
    			int newv = v + dy[i];
    			if (!in(newu, newv)) continue;
    			int newd = mid + (d[u][v] + 1) / s;
    			if (d1[newu][newv] <= newd) continue;
    			if (d[newu][newv] > d[u][v] + 1){
    				d[newu][newv] = d[u][v] + 1;
    				q.push({newu, newv});
    			}
    		}
    	}
    	if (d[xe][ye] != 1e9) {rt = mid; l = mid + 1;}
    	else r = mid - 1;
    }

    cout << rt;

    return 0;
}     
# 결과 실행 시간 메모리 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 4560 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 75 ms 6300 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Incorrect 1 ms 4444 KB Output isn't correct
13 Incorrect 1 ms 4444 KB Output isn't correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4468 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 4444 KB Output is correct
21 Correct 1 ms 4440 KB Output is correct
22 Correct 2 ms 4600 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
25 Correct 1 ms 4456 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 1 ms 4440 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 4 ms 4652 KB Output is correct
34 Correct 5 ms 4700 KB Output is correct
35 Correct 23 ms 4572 KB Output is correct
36 Correct 6 ms 5208 KB Output is correct
37 Correct 6 ms 4952 KB Output is correct
38 Correct 24 ms 4956 KB Output is correct
39 Correct 7 ms 4956 KB Output is correct
40 Correct 8 ms 4956 KB Output is correct
41 Correct 32 ms 5192 KB Output is correct
42 Correct 9 ms 5208 KB Output is correct
43 Correct 9 ms 5212 KB Output is correct
44 Correct 38 ms 5352 KB Output is correct
45 Correct 10 ms 5468 KB Output is correct
46 Correct 10 ms 5720 KB Output is correct
47 Correct 46 ms 5468 KB Output is correct
48 Correct 12 ms 5720 KB Output is correct
49 Correct 13 ms 5724 KB Output is correct
50 Correct 52 ms 5724 KB Output is correct
51 Correct 14 ms 5980 KB Output is correct
52 Correct 15 ms 5724 KB Output is correct
53 Correct 69 ms 5980 KB Output is correct
54 Correct 16 ms 5976 KB Output is correct
55 Correct 17 ms 6120 KB Output is correct
56 Correct 76 ms 6144 KB Output is correct
57 Correct 18 ms 6236 KB Output is correct
58 Correct 19 ms 6236 KB Output is correct
59 Correct 93 ms 6356 KB Output is correct
60 Correct 21 ms 6488 KB Output is correct
61 Correct 23 ms 6492 KB Output is correct
62 Correct 116 ms 6568 KB Output is correct
63 Correct 82 ms 6488 KB Output is correct
64 Correct 155 ms 6504 KB Output is correct
65 Correct 116 ms 6492 KB Output is correct
66 Correct 95 ms 6492 KB Output is correct
67 Correct 90 ms 6492 KB Output is correct
68 Correct 49 ms 6748 KB Output is correct
69 Correct 40 ms 6492 KB Output is correct
70 Correct 32 ms 6588 KB Output is correct
71 Correct 35 ms 6624 KB Output is correct
72 Incorrect 27 ms 6492 KB Output isn't correct
73 Incorrect 38 ms 6748 KB Output isn't correct
74 Correct 50 ms 7004 KB Output is correct
75 Correct 62 ms 6744 KB Output is correct
76 Correct 55 ms 6804 KB Output is correct
77 Correct 53 ms 6852 KB Output is correct
78 Correct 70 ms 6792 KB Output is correct
79 Correct 42 ms 6748 KB Output is correct
80 Correct 53 ms 6744 KB Output is correct
81 Correct 62 ms 6748 KB Output is correct
82 Correct 54 ms 6744 KB Output is correct
83 Correct 68 ms 6748 KB Output is correct
84 Correct 60 ms 6488 KB Output is correct
85 Correct 66 ms 6748 KB Output is correct
86 Correct 64 ms 6744 KB Output is correct
87 Correct 77 ms 6744 KB Output is correct
88 Correct 68 ms 6748 KB Output is correct
89 Correct 68 ms 6684 KB Output is correct
90 Correct 71 ms 6748 KB Output is correct
91 Correct 72 ms 6744 KB Output is correct
92 Correct 79 ms 6756 KB Output is correct