Submission #401900

# Submission time Handle Problem Language Result Execution time Memory
401900 2021-05-11T00:57:20 Z jli12345 Mecho (IOI09_mecho) C++14
57 / 100
356 ms 6548 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 = 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 5 ms 5324 KB Output is correct
2 Correct 5 ms 5324 KB Output is correct
3 Correct 5 ms 5324 KB Output is correct
4 Correct 5 ms 5324 KB Output is correct
5 Correct 5 ms 5324 KB Output is correct
6 Correct 6 ms 5324 KB Output is correct
7 Correct 318 ms 6104 KB Output is correct
8 Correct 3 ms 5324 KB Output is correct
9 Correct 5 ms 5452 KB Output is correct
10 Correct 5 ms 5452 KB Output is correct
11 Correct 6 ms 5452 KB Output is correct
12 Incorrect 6 ms 5452 KB Output isn't correct
13 Incorrect 8 ms 5452 KB Output isn't correct
14 Correct 4 ms 5452 KB Output is correct
15 Correct 6 ms 5448 KB Output is correct
16 Correct 7 ms 5452 KB Output is correct
17 Correct 8 ms 5452 KB Output is correct
18 Correct 6 ms 5452 KB Output is correct
19 Correct 6 ms 5452 KB Output is correct
20 Correct 6 ms 5452 KB Output is correct
21 Correct 6 ms 5452 KB Output is correct
22 Correct 6 ms 5452 KB Output is correct
23 Correct 6 ms 5476 KB Output is correct
24 Correct 8 ms 5468 KB Output is correct
25 Correct 7 ms 5452 KB Output is correct
26 Correct 8 ms 5580 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 7 ms 5452 KB Output isn't correct
30 Correct 7 ms 5452 KB Output is correct
31 Incorrect 7 ms 5452 KB Output isn't correct
32 Correct 6 ms 5452 KB Output is correct
33 Incorrect 16 ms 5704 KB Output isn't correct
34 Incorrect 21 ms 5708 KB Output isn't correct
35 Correct 45 ms 5668 KB Output is correct
36 Incorrect 18 ms 5732 KB Output isn't correct
37 Incorrect 19 ms 5720 KB Output isn't correct
38 Correct 56 ms 5668 KB Output is correct
39 Incorrect 22 ms 5876 KB Output isn't correct
40 Incorrect 25 ms 5768 KB Output isn't correct
41 Correct 69 ms 5772 KB Output is correct
42 Incorrect 26 ms 5836 KB Output isn't correct
43 Incorrect 26 ms 5836 KB Output isn't correct
44 Correct 88 ms 5728 KB Output is correct
45 Incorrect 33 ms 5852 KB Output isn't correct
46 Incorrect 31 ms 5752 KB Output isn't correct
47 Correct 108 ms 5988 KB Output is correct
48 Incorrect 37 ms 5844 KB Output isn't correct
49 Incorrect 36 ms 5876 KB Output isn't correct
50 Correct 118 ms 5904 KB Output is correct
51 Incorrect 38 ms 5868 KB Output isn't correct
52 Incorrect 40 ms 5864 KB Output isn't correct
53 Correct 146 ms 6000 KB Output is correct
54 Incorrect 44 ms 5912 KB Output isn't correct
55 Incorrect 45 ms 5972 KB Output isn't correct
56 Correct 159 ms 5984 KB Output is correct
57 Incorrect 54 ms 5924 KB Output isn't correct
58 Incorrect 51 ms 5952 KB Output isn't correct
59 Correct 208 ms 6124 KB Output is correct
60 Incorrect 56 ms 5972 KB Output isn't correct
61 Incorrect 56 ms 5988 KB Output isn't correct
62 Correct 211 ms 6180 KB Output is correct
63 Correct 233 ms 6160 KB Output is correct
64 Correct 259 ms 6060 KB Output is correct
65 Correct 259 ms 6140 KB Output is correct
66 Correct 308 ms 6068 KB Output is correct
67 Correct 61 ms 5948 KB Output is correct
68 Correct 272 ms 6096 KB Output is correct
69 Correct 206 ms 6100 KB Output is correct
70 Correct 246 ms 6092 KB Output is correct
71 Correct 222 ms 6044 KB Output is correct
72 Incorrect 295 ms 6100 KB Output isn't correct
73 Incorrect 350 ms 6448 KB Output isn't correct
74 Correct 289 ms 6548 KB Output is correct
75 Correct 309 ms 6460 KB Output is correct
76 Correct 295 ms 6464 KB Output is correct
77 Correct 320 ms 6464 KB Output is correct
78 Correct 65 ms 6340 KB Output is correct
79 Correct 286 ms 6528 KB Output is correct
80 Correct 293 ms 6340 KB Output is correct
81 Correct 299 ms 6424 KB Output is correct
82 Correct 318 ms 6420 KB Output is correct
83 Correct 283 ms 6304 KB Output is correct
84 Correct 337 ms 6384 KB Output is correct
85 Correct 296 ms 6472 KB Output is correct
86 Correct 315 ms 6376 KB Output is correct
87 Correct 300 ms 6360 KB Output is correct
88 Correct 287 ms 6296 KB Output is correct
89 Correct 287 ms 6260 KB Output is correct
90 Correct 299 ms 6356 KB Output is correct
91 Correct 318 ms 6300 KB Output is correct
92 Correct 356 ms 6300 KB Output is correct