Submission #401903

# Submission time Handle Problem Language Result Execution time Memory
401903 2021-05-11T01:01:14 Z jli12345 Mecho (IOI09_mecho) C++14
84 / 100
554 ms 6596 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 = 640000;
	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 12 ms 5436 KB Output is correct
2 Correct 11 ms 5436 KB Output is correct
3 Correct 11 ms 5440 KB Output is correct
4 Correct 13 ms 5444 KB Output is correct
5 Correct 12 ms 5436 KB Output is correct
6 Correct 12 ms 5324 KB Output is correct
7 Correct 464 ms 6200 KB Output is correct
8 Correct 3 ms 5452 KB Output is correct
9 Correct 11 ms 5440 KB Output is correct
10 Correct 10 ms 5452 KB Output is correct
11 Correct 11 ms 5452 KB Output is correct
12 Incorrect 77 ms 5452 KB Output isn't correct
13 Incorrect 13 ms 5480 KB Output isn't correct
14 Correct 4 ms 5452 KB Output is correct
15 Correct 11 ms 5452 KB Output is correct
16 Correct 11 ms 5324 KB Output is correct
17 Correct 13 ms 5452 KB Output is correct
18 Correct 11 ms 5460 KB Output is correct
19 Correct 11 ms 5356 KB Output is correct
20 Correct 11 ms 5452 KB Output is correct
21 Correct 11 ms 5460 KB Output is correct
22 Correct 12 ms 5452 KB Output is correct
23 Correct 12 ms 5476 KB Output is correct
24 Correct 12 ms 5452 KB Output is correct
25 Correct 12 ms 5476 KB Output is correct
26 Correct 12 ms 5452 KB Output is correct
27 Correct 12 ms 5484 KB Output is correct
28 Correct 13 ms 5452 KB Output is correct
29 Correct 12 ms 5452 KB Output is correct
30 Correct 13 ms 5452 KB Output is correct
31 Correct 13 ms 5488 KB Output is correct
32 Correct 12 ms 5452 KB Output is correct
33 Correct 60 ms 5708 KB Output is correct
34 Correct 55 ms 5708 KB Output is correct
35 Correct 74 ms 5708 KB Output is correct
36 Correct 68 ms 5748 KB Output is correct
37 Correct 63 ms 5708 KB Output is correct
38 Correct 93 ms 5740 KB Output is correct
39 Correct 83 ms 5776 KB Output is correct
40 Correct 89 ms 5708 KB Output is correct
41 Correct 110 ms 5744 KB Output is correct
42 Correct 103 ms 5808 KB Output is correct
43 Correct 92 ms 5816 KB Output is correct
44 Correct 140 ms 5812 KB Output is correct
45 Correct 111 ms 5976 KB Output is correct
46 Correct 112 ms 5848 KB Output is correct
47 Correct 162 ms 5864 KB Output is correct
48 Correct 152 ms 5836 KB Output is correct
49 Correct 137 ms 5788 KB Output is correct
50 Correct 192 ms 5956 KB Output is correct
51 Correct 162 ms 5940 KB Output is correct
52 Correct 146 ms 5932 KB Output is correct
53 Correct 228 ms 5948 KB Output is correct
54 Correct 180 ms 5880 KB Output is correct
55 Correct 182 ms 5964 KB Output is correct
56 Correct 257 ms 5964 KB Output is correct
57 Correct 215 ms 6012 KB Output is correct
58 Correct 191 ms 5904 KB Output is correct
59 Correct 302 ms 6084 KB Output is correct
60 Correct 222 ms 5956 KB Output is correct
61 Correct 218 ms 6048 KB Output is correct
62 Correct 336 ms 6068 KB Output is correct
63 Correct 399 ms 6124 KB Output is correct
64 Correct 432 ms 6056 KB Output is correct
65 Correct 400 ms 5980 KB Output is correct
66 Correct 435 ms 6052 KB Output is correct
67 Correct 61 ms 5968 KB Output is correct
68 Correct 447 ms 6196 KB Output is correct
69 Correct 389 ms 6100 KB Output is correct
70 Correct 469 ms 6160 KB Output is correct
71 Correct 464 ms 6096 KB Output is correct
72 Incorrect 554 ms 6092 KB Output isn't correct
73 Incorrect 474 ms 6468 KB Output isn't correct
74 Correct 463 ms 6472 KB Output is correct
75 Correct 517 ms 6464 KB Output is correct
76 Correct 502 ms 6464 KB Output is correct
77 Correct 505 ms 6468 KB Output is correct
78 Correct 65 ms 6340 KB Output is correct
79 Correct 460 ms 6320 KB Output is correct
80 Correct 488 ms 6424 KB Output is correct
81 Correct 466 ms 6596 KB Output is correct
82 Correct 479 ms 6348 KB Output is correct
83 Correct 473 ms 6292 KB Output is correct
84 Correct 491 ms 6320 KB Output is correct
85 Correct 499 ms 6368 KB Output is correct
86 Correct 505 ms 6468 KB Output is correct
87 Correct 475 ms 6364 KB Output is correct
88 Correct 484 ms 6296 KB Output is correct
89 Correct 485 ms 6368 KB Output is correct
90 Correct 500 ms 6408 KB Output is correct
91 Correct 474 ms 6300 KB Output is correct
92 Correct 463 ms 6296 KB Output is correct