Submission #980551

# Submission time Handle Problem Language Result Execution time Memory
980551 2024-05-12T08:39:29 Z BF001 Mecho (IOI09_mecho) C++17
69 / 100
143 ms 6492 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 = 1, 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;
}     
# Verdict Execution time Memory 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 Incorrect 1 ms 4444 KB Output isn't correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 73 ms 6236 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 2 ms 4440 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 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 2 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 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 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 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 4 ms 4700 KB Output is correct
34 Correct 5 ms 4560 KB Output is correct
35 Correct 21 ms 4700 KB Output is correct
36 Correct 5 ms 4696 KB Output is correct
37 Correct 6 ms 4696 KB Output is correct
38 Correct 22 ms 4700 KB Output is correct
39 Correct 7 ms 4956 KB Output is correct
40 Correct 8 ms 4956 KB Output is correct
41 Correct 33 ms 5048 KB Output is correct
42 Correct 8 ms 4956 KB Output is correct
43 Correct 9 ms 4952 KB Output is correct
44 Correct 38 ms 5212 KB Output is correct
45 Correct 11 ms 5212 KB Output is correct
46 Correct 10 ms 5208 KB Output is correct
47 Correct 45 ms 5212 KB Output is correct
48 Correct 13 ms 5724 KB Output is correct
49 Correct 12 ms 5468 KB Output is correct
50 Correct 55 ms 5468 KB Output is correct
51 Correct 13 ms 5464 KB Output is correct
52 Correct 14 ms 5464 KB Output is correct
53 Correct 65 ms 5664 KB Output is correct
54 Correct 15 ms 5724 KB Output is correct
55 Correct 16 ms 5724 KB Output is correct
56 Correct 80 ms 5820 KB Output is correct
57 Correct 19 ms 5976 KB Output is correct
58 Correct 18 ms 5980 KB Output is correct
59 Correct 88 ms 5984 KB Output is correct
60 Correct 20 ms 5976 KB Output is correct
61 Correct 21 ms 6152 KB Output is correct
62 Correct 97 ms 5980 KB Output is correct
63 Incorrect 75 ms 6132 KB Output isn't correct
64 Correct 143 ms 6136 KB Output is correct
65 Correct 108 ms 5976 KB Output is correct
66 Correct 100 ms 6132 KB Output is correct
67 Correct 88 ms 5980 KB Output is correct
68 Incorrect 40 ms 5976 KB Output isn't correct
69 Correct 36 ms 5980 KB Output is correct
70 Correct 37 ms 6176 KB Output is correct
71 Correct 33 ms 5980 KB Output is correct
72 Incorrect 34 ms 5980 KB Output isn't correct
73 Incorrect 37 ms 6492 KB Output isn't correct
74 Correct 55 ms 6416 KB Output is correct
75 Correct 62 ms 6424 KB Output is correct
76 Correct 59 ms 6236 KB Output is correct
77 Correct 61 ms 6236 KB Output is correct
78 Correct 70 ms 6400 KB Output is correct
79 Correct 62 ms 6312 KB Output is correct
80 Correct 52 ms 6232 KB Output is correct
81 Correct 60 ms 6236 KB Output is correct
82 Correct 53 ms 6236 KB Output is correct
83 Incorrect 83 ms 6352 KB Output isn't correct
84 Correct 63 ms 6232 KB Output is correct
85 Correct 68 ms 6232 KB Output is correct
86 Correct 68 ms 6236 KB Output is correct
87 Correct 62 ms 6232 KB Output is correct
88 Incorrect 70 ms 6236 KB Output isn't correct
89 Correct 73 ms 6232 KB Output is correct
90 Correct 76 ms 6300 KB Output is correct
91 Correct 72 ms 6232 KB Output is correct
92 Correct 68 ms 6236 KB Output is correct