Submission #611683

# Submission time Handle Problem Language Result Execution time Memory
611683 2022-07-29T06:36:22 Z Belgutei Mecho (IOI09_mecho) C++17
30 / 100
181 ms 8940 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() {
	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;
	} 
	else cout << -1;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2772 KB Output isn't correct
2 Incorrect 3 ms 2900 KB Output isn't correct
3 Incorrect 2 ms 2772 KB Output isn't correct
4 Incorrect 2 ms 2772 KB Output isn't correct
5 Incorrect 2 ms 2772 KB Output isn't correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 105 ms 7504 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Incorrect 3 ms 3028 KB Output isn't correct
13 Incorrect 2 ms 3028 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 Correct 2 ms 2900 KB Output is correct
17 Incorrect 2 ms 2900 KB Output isn't correct
18 Correct 2 ms 2900 KB Output is correct
19 Incorrect 2 ms 2900 KB Output isn't correct
20 Correct 2 ms 2900 KB Output is correct
21 Incorrect 3 ms 2900 KB Output isn't correct
22 Correct 3 ms 3028 KB Output is correct
23 Incorrect 3 ms 3028 KB Output isn't correct
24 Correct 2 ms 3028 KB Output is correct
25 Incorrect 2 ms 3028 KB Output isn't correct
26 Correct 3 ms 3028 KB Output is correct
27 Incorrect 2 ms 3028 KB Output isn't correct
28 Correct 3 ms 3028 KB Output is correct
29 Incorrect 4 ms 3028 KB Output isn't correct
30 Correct 3 ms 3156 KB Output is correct
31 Incorrect 3 ms 3156 KB Output isn't correct
32 Correct 4 ms 3156 KB Output is correct
33 Incorrect 11 ms 4180 KB Output isn't correct
34 Correct 11 ms 4180 KB Output is correct
35 Correct 21 ms 5332 KB Output is correct
36 Incorrect 15 ms 4436 KB Output isn't correct
37 Correct 13 ms 4364 KB Output is correct
38 Correct 29 ms 5568 KB Output is correct
39 Incorrect 16 ms 4600 KB Output isn't correct
40 Correct 15 ms 4576 KB Output is correct
41 Correct 36 ms 6020 KB Output is correct
42 Incorrect 20 ms 4832 KB Output isn't correct
43 Correct 22 ms 4800 KB Output is correct
44 Correct 43 ms 6380 KB Output is correct
45 Incorrect 22 ms 4948 KB Output isn't correct
46 Correct 22 ms 4964 KB Output is correct
47 Correct 50 ms 6732 KB Output is correct
48 Incorrect 25 ms 5196 KB Output isn't correct
49 Correct 25 ms 5240 KB Output is correct
50 Correct 67 ms 7092 KB Output is correct
51 Incorrect 32 ms 5332 KB Output isn't correct
52 Correct 34 ms 5440 KB Output is correct
53 Correct 81 ms 7432 KB Output is correct
54 Incorrect 34 ms 5560 KB Output isn't correct
55 Correct 34 ms 5576 KB Output is correct
56 Correct 93 ms 7796 KB Output is correct
57 Incorrect 41 ms 5720 KB Output isn't correct
58 Correct 43 ms 5716 KB Output is correct
59 Correct 125 ms 8080 KB Output is correct
60 Incorrect 46 ms 6020 KB Output isn't correct
61 Correct 42 ms 5964 KB Output is correct
62 Correct 149 ms 8500 KB Output is correct
63 Incorrect 122 ms 8492 KB Output isn't correct
64 Correct 181 ms 8488 KB Output is correct
65 Correct 141 ms 8512 KB Output is correct
66 Incorrect 126 ms 8508 KB Output isn't correct
67 Correct 130 ms 8512 KB Output is correct
68 Incorrect 70 ms 8216 KB Output isn't correct
69 Correct 64 ms 8548 KB Output is correct
70 Correct 67 ms 8424 KB Output is correct
71 Correct 62 ms 8492 KB Output is correct
72 Incorrect 58 ms 6576 KB Output isn't correct
73 Incorrect 53 ms 8688 KB Output isn't correct
74 Correct 78 ms 8940 KB Output is correct
75 Correct 79 ms 8768 KB Output is correct
76 Correct 74 ms 8780 KB Output is correct
77 Correct 77 ms 8756 KB Output is correct
78 Correct 85 ms 7372 KB Output is correct
79 Correct 67 ms 7116 KB Output is correct
80 Correct 73 ms 7252 KB Output is correct
81 Correct 74 ms 7320 KB Output is correct
82 Correct 74 ms 7232 KB Output is correct
83 Incorrect 99 ms 7500 KB Output isn't correct
84 Correct 111 ms 7232 KB Output is correct
85 Correct 99 ms 7368 KB Output is correct
86 Correct 105 ms 7364 KB Output is correct
87 Correct 85 ms 7404 KB Output is correct
88 Incorrect 99 ms 7500 KB Output isn't correct
89 Correct 93 ms 7324 KB Output is correct
90 Correct 95 ms 7324 KB Output is correct
91 Correct 92 ms 7484 KB Output is correct
92 Correct 98 ms 7500 KB Output is correct