Submission #401897

# Submission time Handle Problem Language Result Execution time Memory
401897 2021-05-11T00:51:27 Z jli12345 Mecho (IOI09_mecho) C++11
46 / 100
337 ms 7116 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'){
				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 = 2000;
	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 6 ms 5324 KB Output is correct
2 Incorrect 6 ms 5324 KB Output isn't correct
3 Correct 6 ms 5428 KB Output is correct
4 Incorrect 7 ms 5324 KB Output isn't correct
5 Correct 6 ms 5428 KB Output is correct
6 Incorrect 5 ms 5452 KB Output isn't correct
7 Incorrect 307 ms 6968 KB Output isn't correct
8 Correct 4 ms 5452 KB Output is correct
9 Correct 8 ms 5324 KB Output is correct
10 Correct 6 ms 5452 KB Output is correct
11 Correct 6 ms 5452 KB Output is correct
12 Incorrect 7 ms 5480 KB Output isn't correct
13 Incorrect 8 ms 5580 KB Output isn't correct
14 Correct 4 ms 5452 KB Output is correct
15 Correct 6 ms 5452 KB Output is correct
16 Correct 7 ms 5452 KB Output is correct
17 Correct 6 ms 5452 KB Output is correct
18 Correct 6 ms 5452 KB Output is correct
19 Correct 7 ms 5428 KB Output is correct
20 Correct 6 ms 5452 KB Output is correct
21 Correct 7 ms 5452 KB Output is correct
22 Correct 7 ms 5464 KB Output is correct
23 Correct 7 ms 5408 KB Output is correct
24 Correct 7 ms 5452 KB Output is correct
25 Correct 6 ms 5480 KB Output is correct
26 Correct 7 ms 5452 KB Output is correct
27 Incorrect 6 ms 5452 KB Output isn't correct
28 Correct 6 ms 5452 KB Output is correct
29 Incorrect 8 ms 5408 KB Output isn't correct
30 Correct 7 ms 5452 KB Output is correct
31 Incorrect 8 ms 5452 KB Output isn't correct
32 Correct 7 ms 5452 KB Output is correct
33 Incorrect 16 ms 5828 KB Output isn't correct
34 Incorrect 17 ms 5836 KB Output isn't correct
35 Correct 48 ms 5836 KB Output is correct
36 Incorrect 19 ms 5864 KB Output isn't correct
37 Incorrect 28 ms 5908 KB Output isn't correct
38 Correct 59 ms 5908 KB Output is correct
39 Incorrect 22 ms 5964 KB Output isn't correct
40 Incorrect 26 ms 5944 KB Output isn't correct
41 Correct 74 ms 5980 KB Output is correct
42 Incorrect 26 ms 6048 KB Output isn't correct
43 Incorrect 25 ms 5956 KB Output isn't correct
44 Correct 106 ms 6060 KB Output is correct
45 Incorrect 28 ms 6160 KB Output isn't correct
46 Incorrect 30 ms 6084 KB Output isn't correct
47 Correct 103 ms 6148 KB Output is correct
48 Incorrect 33 ms 6212 KB Output isn't correct
49 Incorrect 35 ms 6296 KB Output isn't correct
50 Correct 123 ms 6212 KB Output is correct
51 Incorrect 36 ms 6304 KB Output isn't correct
52 Incorrect 39 ms 6244 KB Output isn't correct
53 Correct 138 ms 6308 KB Output is correct
54 Incorrect 41 ms 6424 KB Output isn't correct
55 Incorrect 43 ms 6340 KB Output isn't correct
56 Correct 165 ms 6576 KB Output is correct
57 Incorrect 48 ms 6552 KB Output isn't correct
58 Incorrect 49 ms 6512 KB Output isn't correct
59 Correct 187 ms 6468 KB Output is correct
60 Incorrect 54 ms 6620 KB Output isn't correct
61 Incorrect 56 ms 6648 KB Output isn't correct
62 Correct 224 ms 6728 KB Output is correct
63 Correct 223 ms 6664 KB Output is correct
64 Correct 262 ms 6724 KB Output is correct
65 Correct 251 ms 6620 KB Output is correct
66 Correct 296 ms 6692 KB Output is correct
67 Correct 62 ms 6648 KB Output is correct
68 Correct 233 ms 6740 KB Output is correct
69 Correct 238 ms 6668 KB Output is correct
70 Correct 256 ms 6852 KB Output is correct
71 Correct 240 ms 6816 KB Output is correct
72 Incorrect 295 ms 6720 KB Output isn't correct
73 Incorrect 311 ms 7092 KB Output isn't correct
74 Correct 295 ms 6980 KB Output is correct
75 Correct 337 ms 7116 KB Output is correct
76 Correct 288 ms 7108 KB Output is correct
77 Correct 299 ms 7092 KB Output is correct
78 Correct 69 ms 7108 KB Output is correct
79 Correct 287 ms 7052 KB Output is correct
80 Correct 298 ms 6980 KB Output is correct
81 Correct 302 ms 7104 KB Output is correct
82 Correct 298 ms 6980 KB Output is correct
83 Correct 276 ms 6996 KB Output is correct
84 Correct 294 ms 6920 KB Output is correct
85 Correct 288 ms 6888 KB Output is correct
86 Correct 301 ms 6996 KB Output is correct
87 Correct 300 ms 6992 KB Output is correct
88 Correct 279 ms 6852 KB Output is correct
89 Correct 289 ms 6940 KB Output is correct
90 Correct 296 ms 6940 KB Output is correct
91 Correct 297 ms 6972 KB Output is correct
92 Correct 299 ms 6932 KB Output is correct