Submission #401901

# Submission time Handle Problem Language Result Execution time Memory
401901 2021-05-11T01:00:21 Z jli12345 Mecho (IOI09_mecho) C++14
84 / 100
507 ms 6516 KB
#include <bits/stdc++.h>
using namespace std;

int N, S;

char arr[810][810];

int sx, sy, ex, ey;

typedef pair<int, int> pii;
#define f first
#define s second

queue<pair<pii, int> > q1;
queue<pair<pii, int> > q2;

int vis1[810][810];
int vis2[810][810];

const int INF = 0x3f3f3f3f;

int xd[4] = {1, -1, 0, 0};
int yd[4] = {0, 0, 1, -1};

void bfs(int t){
	while (!q2.empty()){
		int x = q2.front().f.f;
		int y = q2.front().f.s;
		int w = q2.front().s;
		if (w == t)
			break;
		q2.pop();
		for (int k = 0; k < 4; k++){
			int nx = x+xd[k];
			int ny = y+yd[k];
			if (nx >= 1 && nx <= N && ny >= 1 && ny <= N && 
				arr[nx][ny] != 'T' && arr[nx][ny] != 'M' && arr[nx][ny] != 'D'){
				if (vis2[nx][ny] > vis2[x][y]+1){
					vis2[nx][ny] = vis2[x][y]+1;
					q2.push({{nx, ny}, vis2[nx][ny]});
				}
			}
		}
	}
}

bool valid(int t){
	while (!q1.empty()) q1.pop();
	while (!q2.empty()) q2.pop();
	memset(vis1, INF, sizeof(vis1));
	memset(vis2, INF, sizeof(vis2));
	for (int i = 1; i <= N; i++){
		for (int j = 1; j <= N; j++){
			if (arr[i][j] == 'H'){
				q2.push({{i, j}, 0});
				vis2[i][j] = 0;
			}
		}
	}
	q1.push({{sx, sy}, 0});
	vis1[sx][sy] = 0;
	int curtime = 1;
	while (curtime <= t){
		bfs(curtime);
		curtime++;
	}
	while (!q1.empty()){
		int x = q1.front().f.f;
		int y = q1.front().f.s;
		int w = q1.front().s;
		if (x == ex && y == ey){
			return true;
		}
		if (vis1[x][y] == (curtime-t)*S){
			bfs(curtime);
			curtime++;
		}
		q1.pop();
		if (vis2[x][y] != INF){
			continue;
		}
		for (int k = 0; k < 4; k++){
			int nx = x+xd[k];
			int ny = y+yd[k];
			if (nx >= 1 && nx <= N && ny >= 1 && ny <= N && vis2[nx][ny] == INF && 
				(arr[nx][ny] == 'D' || arr[nx][ny] == 'G')){
				if (vis1[nx][ny] > vis1[x][y]+1){
					vis1[nx][ny] = vis1[x][y]+1;
					q1.push({{nx, ny}, vis1[nx][ny]});
				}
			}
		}
	}
	return false;
}

int main(){
	cin >> N >> S;
	for (int i = 1; i <= N; i++){
		for (int j = 1; j <= N; j++){
			cin >> arr[i][j];
			if (arr[i][j] == 'M'){
				sx = i;
				sy = j;
			}
			if (arr[i][j] == 'D'){
				ex = i;
				ey = j;
			}
		}
	}
	if (!valid(0)){
		cout << -1 << "\n";
		return 0;
	}
	int l = 0, r = 320000;
	while (l != r){
		int mid = (l+r+1)/2;
		if (valid(mid)){
			l = mid;
		} else {
			r = mid-1;
		}
	}
	cout << l << "\n";
}

Compilation message

mecho.cpp: In function 'bool valid(int)':
mecho.cpp:70:7: warning: unused variable 'w' [-Wunused-variable]
   70 |   int w = q1.front().s;
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5324 KB Output is correct
2 Correct 9 ms 5436 KB Output is correct
3 Correct 9 ms 5324 KB Output is correct
4 Correct 9 ms 5432 KB Output is correct
5 Correct 9 ms 5436 KB Output is correct
6 Correct 9 ms 5324 KB Output is correct
7 Correct 430 ms 6224 KB Output is correct
8 Correct 3 ms 5452 KB Output is correct
9 Correct 11 ms 5452 KB Output is correct
10 Correct 11 ms 5452 KB Output is correct
11 Correct 10 ms 5456 KB Output is correct
12 Incorrect 44 ms 5452 KB Output isn't correct
13 Incorrect 13 ms 5476 KB Output isn't correct
14 Correct 3 ms 5452 KB Output is correct
15 Correct 10 ms 5452 KB Output is correct
16 Correct 8 ms 5452 KB Output is correct
17 Correct 9 ms 5452 KB Output is correct
18 Correct 9 ms 5452 KB Output is correct
19 Correct 9 ms 5452 KB Output is correct
20 Correct 9 ms 5460 KB Output is correct
21 Correct 10 ms 5468 KB Output is correct
22 Correct 11 ms 5468 KB Output is correct
23 Correct 10 ms 5472 KB Output is correct
24 Correct 13 ms 5472 KB Output is correct
25 Correct 10 ms 5452 KB Output is correct
26 Correct 10 ms 5480 KB Output is correct
27 Correct 11 ms 5452 KB Output is correct
28 Correct 10 ms 5452 KB Output is correct
29 Correct 11 ms 5452 KB Output is correct
30 Correct 12 ms 5452 KB Output is correct
31 Correct 11 ms 5452 KB Output is correct
32 Correct 12 ms 5488 KB Output is correct
33 Correct 55 ms 5692 KB Output is correct
34 Correct 49 ms 5580 KB Output is correct
35 Correct 74 ms 5720 KB Output is correct
36 Correct 63 ms 5744 KB Output is correct
37 Correct 62 ms 5624 KB Output is correct
38 Correct 84 ms 5740 KB Output is correct
39 Correct 79 ms 5768 KB Output is correct
40 Correct 78 ms 5668 KB Output is correct
41 Correct 112 ms 5776 KB Output is correct
42 Correct 97 ms 5808 KB Output is correct
43 Correct 91 ms 5816 KB Output is correct
44 Correct 132 ms 5820 KB Output is correct
45 Correct 112 ms 5852 KB Output is correct
46 Correct 106 ms 5832 KB Output is correct
47 Correct 155 ms 5836 KB Output is correct
48 Correct 141 ms 5900 KB Output is correct
49 Correct 145 ms 5888 KB Output is correct
50 Correct 178 ms 6028 KB Output is correct
51 Correct 156 ms 5932 KB Output is correct
52 Correct 137 ms 5928 KB Output is correct
53 Correct 223 ms 6064 KB Output is correct
54 Correct 177 ms 5972 KB Output is correct
55 Correct 161 ms 5872 KB Output is correct
56 Correct 257 ms 5984 KB Output is correct
57 Correct 213 ms 5956 KB Output is correct
58 Correct 212 ms 5960 KB Output is correct
59 Correct 299 ms 6024 KB Output is correct
60 Correct 221 ms 5976 KB Output is correct
61 Correct 234 ms 6052 KB Output is correct
62 Correct 342 ms 6148 KB Output is correct
63 Correct 398 ms 6044 KB Output is correct
64 Correct 486 ms 6056 KB Output is correct
65 Correct 381 ms 6052 KB Output is correct
66 Correct 441 ms 5956 KB Output is correct
67 Correct 61 ms 5956 KB Output is correct
68 Correct 453 ms 6096 KB Output is correct
69 Correct 363 ms 6088 KB Output is correct
70 Correct 441 ms 6100 KB Output is correct
71 Correct 421 ms 6104 KB Output is correct
72 Incorrect 502 ms 6092 KB Output isn't correct
73 Incorrect 507 ms 6468 KB Output isn't correct
74 Correct 445 ms 6464 KB Output is correct
75 Correct 478 ms 6516 KB Output is correct
76 Correct 480 ms 6452 KB Output is correct
77 Correct 470 ms 6468 KB Output is correct
78 Correct 66 ms 6376 KB Output is correct
79 Correct 421 ms 6424 KB Output is correct
80 Correct 454 ms 6340 KB Output is correct
81 Correct 465 ms 6420 KB Output is correct
82 Correct 454 ms 6340 KB Output is correct
83 Correct 456 ms 6360 KB Output is correct
84 Correct 464 ms 6356 KB Output is correct
85 Correct 487 ms 6364 KB Output is correct
86 Correct 496 ms 6360 KB Output is correct
87 Correct 449 ms 6340 KB Output is correct
88 Correct 459 ms 6296 KB Output is correct
89 Correct 456 ms 6172 KB Output is correct
90 Correct 463 ms 6396 KB Output is correct
91 Correct 449 ms 6208 KB Output is correct
92 Correct 444 ms 6400 KB Output is correct