답안 #1092624

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1092624 2024-09-24T15:19:48 Z 4QT0R Mecho (IOI09_mecho) C++17
100 / 100
109 ms 6996 KB
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>

char tab[802][802];
int xcor[4]={0,1,0,-1};
int ycor[4]={1,0,-1,0};

int dist[802][802];
int oo=1e9;
int n,s;

void prep(){
	queue<pii> q;
	for (int i = 1; i<=n; i++){
		for (int j = 1; j<=n; j++){
			dist[i][j]=oo;
			if (tab[i][j]=='H'){
				q.push({i,j});
				dist[i][j]=0;
			}
		}
	}
	while(q.size()){
		auto [x,y]=q.front();
		q.pop();
		for (int i = 0; i<4; i++){
			int u=x+xcor[i];
			int v=y+ycor[i];
			if ((tab[u][v]=='G'||tab[u][v]=='M') && dist[u][v]>dist[x][y]+1){
				dist[u][v]=dist[x][y]+1;
				q.push({u,v});
			}
		}
	}
}

int vis[802][802];
int iter=0;
pii st,nd;

bool bfs(int waiting){
	queue<pair<pii,int>> q;
	if (dist[st.first][st.second]<=waiting)return false;
	q.push({st,0});
	while(q.size()){
		auto [pol,d]=q.front();
		if (pol==nd)return true;
		q.pop();
		for (int i = 0; i<4; i++){
			int u=pol.first+xcor[i];
			int v=pol.second+ycor[i];
			if ((tab[u][v]=='G'||tab[u][v]=='D') && vis[u][v]!=iter && (dist[u][v]>waiting+(d+1)/s)){
				vis[u][v]=iter;
				q.push({{u,v},d+1});
			}
		}
	}
	return false;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> s;
	for (int i = 1; i<=n; i++){
		for (int j = 1; j<=n; j++){
			cin >> tab[i][j];
			if (tab[i][j]=='M')st={i,j};
			if (tab[i][j]=='D')nd={i,j};
		}
	}
	prep();
	int l=-1,p=1e6,md;
	while(l<p){
		md=(l+p+1)/2;
		iter++;
		if (bfs(md))l=md;
		else p=md-1;
	}
	cout << l << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 372 KB Output is correct
7 Correct 54 ms 5716 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 716 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 0 ms 472 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 856 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 3 ms 1868 KB Output is correct
34 Correct 3 ms 1884 KB Output is correct
35 Correct 13 ms 2908 KB Output is correct
36 Correct 3 ms 2140 KB Output is correct
37 Correct 3 ms 2140 KB Output is correct
38 Correct 16 ms 3476 KB Output is correct
39 Correct 6 ms 2412 KB Output is correct
40 Correct 5 ms 2396 KB Output is correct
41 Correct 21 ms 3668 KB Output is correct
42 Correct 5 ms 2652 KB Output is correct
43 Correct 5 ms 2696 KB Output is correct
44 Correct 32 ms 4108 KB Output is correct
45 Correct 6 ms 2788 KB Output is correct
46 Correct 6 ms 2944 KB Output is correct
47 Correct 31 ms 4456 KB Output is correct
48 Correct 7 ms 3176 KB Output is correct
49 Correct 6 ms 3168 KB Output is correct
50 Correct 41 ms 4968 KB Output is correct
51 Correct 8 ms 3432 KB Output is correct
52 Correct 8 ms 3436 KB Output is correct
53 Correct 46 ms 5248 KB Output is correct
54 Correct 9 ms 3676 KB Output is correct
55 Correct 10 ms 3676 KB Output is correct
56 Correct 52 ms 5724 KB Output is correct
57 Correct 10 ms 3932 KB Output is correct
58 Correct 9 ms 3772 KB Output is correct
59 Correct 60 ms 6232 KB Output is correct
60 Correct 13 ms 4184 KB Output is correct
61 Correct 10 ms 4084 KB Output is correct
62 Correct 79 ms 6624 KB Output is correct
63 Correct 71 ms 6736 KB Output is correct
64 Correct 109 ms 6736 KB Output is correct
65 Correct 98 ms 6552 KB Output is correct
66 Correct 86 ms 6748 KB Output is correct
67 Correct 70 ms 6484 KB Output is correct
68 Correct 39 ms 6740 KB Output is correct
69 Correct 33 ms 6740 KB Output is correct
70 Correct 29 ms 6560 KB Output is correct
71 Correct 25 ms 6744 KB Output is correct
72 Correct 21 ms 4944 KB Output is correct
73 Correct 27 ms 6800 KB Output is correct
74 Correct 38 ms 6912 KB Output is correct
75 Correct 41 ms 6992 KB Output is correct
76 Correct 39 ms 6992 KB Output is correct
77 Correct 40 ms 6996 KB Output is correct
78 Correct 43 ms 5456 KB Output is correct
79 Correct 40 ms 5252 KB Output is correct
80 Correct 38 ms 5468 KB Output is correct
81 Correct 44 ms 5468 KB Output is correct
82 Correct 32 ms 5204 KB Output is correct
83 Correct 48 ms 5464 KB Output is correct
84 Correct 44 ms 5456 KB Output is correct
85 Correct 43 ms 5452 KB Output is correct
86 Correct 49 ms 5472 KB Output is correct
87 Correct 45 ms 5456 KB Output is correct
88 Correct 48 ms 5716 KB Output is correct
89 Correct 48 ms 5712 KB Output is correct
90 Correct 49 ms 5712 KB Output is correct
91 Correct 51 ms 5468 KB Output is correct
92 Correct 49 ms 5672 KB Output is correct