Submission #611690

# Submission time Handle Problem Language Result Execution time Memory
611690 2022-07-29T06:38:03 Z Belgutei Mecho (IOI09_mecho) C++17
4 / 100
158 ms 8820 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 Incorrect 2 ms 2900 KB Output isn't correct
2 Incorrect 2 ms 2900 KB Output isn't correct
3 Incorrect 2 ms 2900 KB Output isn't correct
4 Incorrect 2 ms 2900 KB Output isn't correct
5 Incorrect 2 ms 2900 KB Output isn't correct
6 Incorrect 2 ms 2900 KB Output isn't correct
7 Incorrect 69 ms 7540 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 Incorrect 2 ms 3156 KB Output isn't correct
14 Correct 3 ms 3156 KB Output is correct
15 Incorrect 2 ms 2900 KB Output isn't correct
16 Incorrect 2 ms 2900 KB Output isn't correct
17 Incorrect 2 ms 2900 KB Output isn't correct
18 Incorrect 2 ms 2900 KB Output isn't correct
19 Incorrect 2 ms 2900 KB Output isn't correct
20 Incorrect 2 ms 3028 KB Output isn't correct
21 Incorrect 2 ms 3028 KB Output isn't correct
22 Incorrect 2 ms 3028 KB Output isn't correct
23 Incorrect 2 ms 3028 KB Output isn't correct
24 Incorrect 3 ms 3028 KB Output isn't correct
25 Incorrect 3 ms 3028 KB Output isn't correct
26 Incorrect 2 ms 3028 KB Output isn't correct
27 Incorrect 2 ms 3028 KB Output isn't correct
28 Incorrect 3 ms 3156 KB Output isn't correct
29 Incorrect 2 ms 3156 KB Output isn't correct
30 Incorrect 2 ms 3156 KB Output isn't correct
31 Incorrect 2 ms 3156 KB Output isn't correct
32 Incorrect 3 ms 3156 KB Output isn't correct
33 Incorrect 6 ms 4180 KB Output isn't correct
34 Incorrect 5 ms 4180 KB Output isn't correct
35 Incorrect 22 ms 5356 KB Output isn't correct
36 Incorrect 9 ms 4436 KB Output isn't correct
37 Incorrect 7 ms 4436 KB Output isn't correct
38 Incorrect 28 ms 5616 KB Output isn't correct
39 Incorrect 8 ms 4564 KB Output isn't correct
40 Incorrect 8 ms 4564 KB Output isn't correct
41 Incorrect 31 ms 5972 KB Output isn't correct
42 Incorrect 10 ms 4740 KB Output isn't correct
43 Incorrect 8 ms 4820 KB Output isn't correct
44 Incorrect 36 ms 6404 KB Output isn't correct
45 Incorrect 10 ms 5004 KB Output isn't correct
46 Incorrect 11 ms 5076 KB Output isn't correct
47 Incorrect 55 ms 6768 KB Output isn't correct
48 Incorrect 15 ms 5144 KB Output isn't correct
49 Incorrect 11 ms 5204 KB Output isn't correct
50 Incorrect 79 ms 7112 KB Output isn't correct
51 Incorrect 14 ms 5332 KB Output isn't correct
52 Incorrect 17 ms 5396 KB Output isn't correct
53 Incorrect 70 ms 7468 KB Output isn't correct
54 Incorrect 16 ms 5620 KB Output isn't correct
55 Incorrect 13 ms 5588 KB Output isn't correct
56 Incorrect 82 ms 7816 KB Output isn't correct
57 Incorrect 17 ms 5844 KB Output isn't correct
58 Incorrect 17 ms 5844 KB Output isn't correct
59 Incorrect 94 ms 8176 KB Output isn't correct
60 Incorrect 18 ms 5940 KB Output isn't correct
61 Incorrect 16 ms 5976 KB Output isn't correct
62 Incorrect 111 ms 8544 KB Output isn't correct
63 Incorrect 85 ms 8532 KB Output isn't correct
64 Incorrect 158 ms 8524 KB Output isn't correct
65 Incorrect 119 ms 8696 KB Output isn't correct
66 Incorrect 104 ms 8536 KB Output isn't correct
67 Correct 102 ms 8532 KB Output is correct
68 Incorrect 46 ms 8272 KB Output isn't correct
69 Incorrect 42 ms 8524 KB Output isn't correct
70 Incorrect 41 ms 8492 KB Output isn't correct
71 Incorrect 42 ms 8552 KB Output isn't correct
72 Incorrect 31 ms 6588 KB Output isn't correct
73 Incorrect 29 ms 8772 KB Output isn't correct
74 Incorrect 44 ms 8760 KB Output isn't correct
75 Incorrect 53 ms 8800 KB Output isn't correct
76 Incorrect 45 ms 8820 KB Output isn't correct
77 Incorrect 59 ms 8804 KB Output isn't correct
78 Correct 71 ms 7472 KB Output is correct
79 Incorrect 42 ms 7040 KB Output isn't correct
80 Incorrect 47 ms 7216 KB Output isn't correct
81 Incorrect 53 ms 7248 KB Output isn't correct
82 Incorrect 49 ms 7308 KB Output isn't correct
83 Incorrect 65 ms 7372 KB Output isn't correct
84 Incorrect 64 ms 7340 KB Output isn't correct
85 Incorrect 55 ms 7408 KB Output isn't correct
86 Incorrect 82 ms 7440 KB Output isn't correct
87 Incorrect 65 ms 7372 KB Output isn't correct
88 Incorrect 60 ms 7540 KB Output isn't correct
89 Incorrect 65 ms 7324 KB Output isn't correct
90 Incorrect 64 ms 7388 KB Output isn't correct
91 Incorrect 75 ms 7540 KB Output isn't correct
92 Incorrect 75 ms 7568 KB Output isn't correct