Submission #611686

# Submission time Handle Problem Language Result Execution time Memory
611686 2022-07-29T06:37:21 Z Belgutei Mecho (IOI09_mecho) C++17
22 / 100
152 ms 8808 KB
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int n,m;
int b[805][806];
queue<pair<int,int> > q;
int stx, sty;
int cnt[805][805];
int tmp[805][805];
bool check = 0;
char ch[805][805];

int main() {
	IOS
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= n; j ++) {
			cin >> ch[i][j];
			if(ch[i][j] == 'H') {
				b[i][j] = 1;
				q.push({i,j});
			}
			if(ch[i][j] == 'M') {
				stx = i;
				sty = j;
			}
		}
	}
	while(q.size() > 0) {
		int x = q.front().ff;
		int y = q.front().ss;
		q.pop();
		if(x > 1 && b[x - 1][y] == 0 && ch[x - 1][y] != 'T' && ch[x - 1][y] != 'D') {
			q.push({x - 1, y});
			b[x - 1][y] = b[x][y] + 1;
		}
		if(x < n && b[x + 1][y] == 0 && ch[x + 1][y] != 'T' && ch[x + 1][y] != 'D') {
			q.push({x + 1, y});
			b[x + 1][y] = b[x][y] + 1;
		}
		if(y > 1 && b[x][y - 1] == 0 && ch[x][y - 1] != 'T' && ch[x][y - 1] != 'D') {
			q.push({x, y - 1});
			b[x][y - 1] = b[x][y] + 1;
		}
		if(y < n && b[x][y + 1] == 0 && ch[x][y + 1] != 'T' && ch[x][y + 1] != 'D') {
			q.push({x, y + 1});
			b[x][y + 1] = b[x][y] + 1;
		}		
	}
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= n; j ++) {
			if(b[i][j] == 0) b[i][j] = 1e9;
		}
	} 
	int l = 0;
	int r = n * n;
	while(l < r) {
		int mid = (l + r + 1) / 2;
		memset(cnt, -1, sizeof(cnt));
		while(q.size() > 0) q.pop();
		q.push({stx,sty});
		cnt[stx][sty] = mid;
		tmp[stx][sty] = m;
		bool ok = 0;
		while(q.size() > 0) {
			int x = q.front().ff;
			int y = q.front().ss;
			q.pop();
			int num = cnt[x][y];
			int move = tmp[x][y];
			if(tmp[x][y] == m) {
				num ++;
				move = 0;
			}
			move ++;
			//
			if(x > 1 && cnt[x - 1][y] == -1 && num < b[x - 1][y] && ch[x - 1][y] != 'T') {
				if(ch[x - 1][y] == 'D') { // home location
					ok = 1; break;
				}
				cnt[x - 1][y] = num;
				tmp[x - 1][y] = move;
				q.push({x - 1, y});
			}
			//
			if(x < n && cnt[x + 1][y] == -1 && num < b[x + 1][y] && ch[x + 1][y] != 'T') {
				if(ch[x + 1][y] == 'D') { // home location
					ok = 1; break;
				}
				cnt[x + 1][y] = num;
				tmp[x + 1][y] = move;
				q.push({x + 1, y});
			}
			//
			if(y > 1 && cnt[x][y - 1] == -1 && num < b[x][y - 1] && ch[x][y - 1] != 'T') {
				if(ch[x][y - 1] == 'D') { // home location
					ok = 1; break;
				}
				cnt[x][y - 1] = num;
				tmp[x][y - 1] = move;
				q.push({x, y - 1});
			}
			//
			if(y < n && cnt[x][y + 1] == -1 && num < b[x][y + 1] && ch[x][y + 1] != 'T') {
				if(ch[x][y + 1] == 'D') { // home location
					ok = 1; break;
				}
				cnt[x][y + 1] = num;
				tmp[x][y + 1] = move;
				q.push({x, y + 1});
			}
		}
		if(ok == 1) {
			check = 1;
			l = mid;
		}
		else r = mid - 1;
	}
	if(check == 1){
		cout << l - 1;
	} 
	else cout << -1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2900 KB Output is correct
2 Correct 2 ms 2900 KB Output is correct
3 Correct 2 ms 2900 KB Output is correct
4 Correct 2 ms 2900 KB Output is correct
5 Incorrect 2 ms 2900 KB Output isn't correct
6 Incorrect 2 ms 2900 KB Output isn't correct
7 Incorrect 59 ms 7536 KB Output isn't correct
8 Correct 2 ms 2900 KB Output is correct
9 Incorrect 2 ms 2900 KB Output isn't correct
10 Incorrect 2 ms 2900 KB Output isn't correct
11 Incorrect 2 ms 2900 KB Output isn't correct
12 Incorrect 2 ms 3028 KB Output isn't correct
13 Correct 2 ms 3156 KB Output is correct
14 Correct 3 ms 3156 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
16 Incorrect 2 ms 2900 KB Output isn't correct
17 Correct 4 ms 2900 KB Output is correct
18 Incorrect 3 ms 3000 KB Output isn't correct
19 Correct 3 ms 2900 KB Output is correct
20 Incorrect 2 ms 2900 KB Output isn't correct
21 Correct 2 ms 3028 KB Output is correct
22 Incorrect 3 ms 3116 KB Output isn't correct
23 Correct 2 ms 3028 KB Output is correct
24 Incorrect 2 ms 3028 KB Output isn't correct
25 Correct 2 ms 3028 KB Output is correct
26 Incorrect 2 ms 3028 KB Output isn't correct
27 Correct 2 ms 3028 KB Output is correct
28 Incorrect 4 ms 3156 KB Output isn't correct
29 Correct 3 ms 3168 KB Output is correct
30 Incorrect 3 ms 3156 KB Output isn't correct
31 Correct 3 ms 3156 KB Output is correct
32 Incorrect 3 ms 3156 KB Output isn't correct
33 Correct 7 ms 4308 KB Output is correct
34 Incorrect 8 ms 4180 KB Output isn't correct
35 Incorrect 17 ms 5364 KB Output isn't correct
36 Correct 8 ms 4436 KB Output is correct
37 Incorrect 7 ms 4436 KB Output isn't correct
38 Incorrect 23 ms 5716 KB Output isn't correct
39 Correct 8 ms 4564 KB Output is correct
40 Incorrect 7 ms 4564 KB Output isn't correct
41 Incorrect 36 ms 6068 KB Output isn't correct
42 Correct 11 ms 4756 KB Output is correct
43 Incorrect 9 ms 4820 KB Output isn't correct
44 Incorrect 33 ms 6404 KB Output isn't correct
45 Correct 12 ms 4964 KB Output is correct
46 Incorrect 9 ms 5076 KB Output isn't correct
47 Incorrect 42 ms 6772 KB Output isn't correct
48 Correct 11 ms 5216 KB Output is correct
49 Incorrect 11 ms 5260 KB Output isn't correct
50 Incorrect 53 ms 7112 KB Output isn't correct
51 Correct 12 ms 5348 KB Output is correct
52 Incorrect 14 ms 5460 KB Output isn't correct
53 Incorrect 84 ms 7464 KB Output isn't correct
54 Correct 16 ms 5560 KB Output is correct
55 Incorrect 14 ms 5672 KB Output isn't correct
56 Incorrect 82 ms 7816 KB Output isn't correct
57 Correct 18 ms 5776 KB Output is correct
58 Incorrect 15 ms 5844 KB Output isn't correct
59 Incorrect 96 ms 8176 KB Output isn't correct
60 Correct 18 ms 5936 KB Output is correct
61 Incorrect 16 ms 6064 KB Output isn't correct
62 Incorrect 131 ms 8528 KB Output isn't correct
63 Incorrect 87 ms 8528 KB Output isn't correct
64 Incorrect 152 ms 8536 KB Output isn't correct
65 Incorrect 116 ms 8532 KB Output isn't correct
66 Correct 115 ms 8536 KB Output is correct
67 Correct 109 ms 8512 KB Output is correct
68 Incorrect 42 ms 8256 KB Output isn't correct
69 Incorrect 41 ms 8524 KB Output isn't correct
70 Incorrect 48 ms 8652 KB Output isn't correct
71 Incorrect 48 ms 8636 KB Output isn't correct
72 Correct 35 ms 6676 KB Output is correct
73 Correct 37 ms 8748 KB Output is correct
74 Incorrect 54 ms 8808 KB Output isn't correct
75 Incorrect 51 ms 8732 KB Output isn't correct
76 Incorrect 47 ms 8800 KB Output isn't correct
77 Incorrect 50 ms 8808 KB Output isn't correct
78 Correct 60 ms 7484 KB Output is correct
79 Incorrect 39 ms 7040 KB Output isn't correct
80 Incorrect 47 ms 7128 KB Output isn't correct
81 Incorrect 53 ms 7368 KB Output isn't correct
82 Incorrect 58 ms 7248 KB Output isn't correct
83 Incorrect 72 ms 7460 KB Output isn't correct
84 Incorrect 55 ms 7244 KB Output isn't correct
85 Incorrect 57 ms 7392 KB Output isn't correct
86 Incorrect 58 ms 7396 KB Output isn't correct
87 Incorrect 55 ms 7320 KB Output isn't correct
88 Incorrect 74 ms 7584 KB Output isn't correct
89 Incorrect 77 ms 7380 KB Output isn't correct
90 Incorrect 70 ms 7444 KB Output isn't correct
91 Incorrect 76 ms 7544 KB Output isn't correct
92 Incorrect 89 ms 7576 KB Output isn't correct