#include<bits/stdc++.h>
using namespace std;
const int N = 805, inf = 1e9;
string a[N];
int d[N][N], dis[N][N], n, s;
bool vis[N][N];
pair<int, int> st, en;
int movex[] = {1, 0, -1, 0};
int movey[] = {0, 1, 0, -1};
void fill(){
for(int i = 0; i < 801; i++){
for(int j = 0; j < 801; j++){
d[i][j] = inf;
}
}
}
bool check(int i, int j){
if(i >= 0 && i < n && j >= 0 && j < n && (a[i][j] == 'G' || a[i][j] == 'D') && !vis[i][j]){
return 1;
}
return 0;
}
void bfs(int i, int j){
memset(vis, 0, sizeof vis);
memset(dis, 0, sizeof dis);
queue<pair<int, int>> q;
dis[i][j] = 0;
vis[i][j] = 1;
q.push({i, j});
while(!q.empty()){
pair<int, int> u = q.front(); q.pop();
for(int i1 = 0; i1 < 4; i1++){
int x = movex[i1], y = movey[i1];
if(!vis[u.first + x][u.second + y] && check(u.first + x, u.second + y)){
vis[u.first + x][u.second + y] = 1;
dis[u.first + x][u.second + y] = dis[u.first][u.second] + 1;
d[u.first + x][u.second + y] = min(d[u.first + x][u.second + y], dis[u.first + x][u.second + y]);
q.push({u.first + x, u.second + y});
}
}
}
}
bool possible(int x){
int cur = x;
memset(vis, 0, sizeof vis);
queue<pair<pair<int, int>, pair<int,int>>> q;
q.push({{st.first, st.second}, {cur, s}});
vis[st.first][st.second] = 1;
while(!q.empty()){
auto k = q.front(); q.pop();
int u = k.first.first, v = k.first.second, chargesleft = k.second.second;
int now = k.second.first;
for(int i = 0; i < 4; i++){
if(check(u + movex[i], v + movey[i])){
if(d[u + movex[i]][v + movey[i]] > now){
if(chargesleft > 1){
q.push({{u + movex[i], v + movey[i]}, {now, chargesleft - 1}});
}else{
q.push({{u + movex[i], v + movey[i]}, {now + 1, s}});
}
vis[u + movex[i]][v + movey[i]] = 1;
}
}
}
}
/*cout << "x : " << x << '\n';
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << vis[i][j];
}
cout << '\n';
}*/
if(vis[en.first][en.second])return 1;
return 0;
}
int main(){
cin >> n >> s;
fill();
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(a[i][j] == 'H'){
bfs(i, j);
}
if(a[i][j] == 'M'){
st.first = i;
st.second = j;
}
if(a[i][j] == 'D'){
en.first = i;
en.second = j;
}
}
}
/*for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(d[i][j] != inf)cout << d[i][j];
else cout << 0;
}
cout << '\n';
}*/
int l = 0, r = inf;
while(l != r){
int mid = l + (r - l + 1) / 2;
if(possible(mid)){
l = mid;
}else{
r = mid - 1;
}
}
cout << l << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
2 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
3 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
4 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
5 |
Correct |
8 ms |
6008 KB |
Output is correct |
6 |
Incorrect |
9 ms |
6008 KB |
Output isn't correct |
7 |
Execution timed out |
1067 ms |
7444 KB |
Time limit exceeded |
8 |
Incorrect |
10 ms |
6008 KB |
Output isn't correct |
9 |
Correct |
9 ms |
6008 KB |
Output is correct |
10 |
Correct |
10 ms |
6004 KB |
Output is correct |
11 |
Correct |
10 ms |
6140 KB |
Output is correct |
12 |
Incorrect |
9 ms |
6044 KB |
Output isn't correct |
13 |
Incorrect |
42 ms |
6008 KB |
Output isn't correct |
14 |
Incorrect |
40 ms |
6008 KB |
Output isn't correct |
15 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
16 |
Correct |
9 ms |
6008 KB |
Output is correct |
17 |
Incorrect |
8 ms |
5980 KB |
Output isn't correct |
18 |
Correct |
8 ms |
6008 KB |
Output is correct |
19 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
20 |
Correct |
8 ms |
6008 KB |
Output is correct |
21 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
22 |
Correct |
8 ms |
6008 KB |
Output is correct |
23 |
Incorrect |
8 ms |
6008 KB |
Output isn't correct |
24 |
Correct |
9 ms |
6008 KB |
Output is correct |
25 |
Incorrect |
10 ms |
6008 KB |
Output isn't correct |
26 |
Correct |
10 ms |
6008 KB |
Output is correct |
27 |
Incorrect |
10 ms |
6136 KB |
Output isn't correct |
28 |
Correct |
10 ms |
6136 KB |
Output is correct |
29 |
Incorrect |
10 ms |
6136 KB |
Output isn't correct |
30 |
Correct |
10 ms |
6136 KB |
Output is correct |
31 |
Incorrect |
9 ms |
6136 KB |
Output isn't correct |
32 |
Correct |
9 ms |
6136 KB |
Output is correct |
33 |
Incorrect |
19 ms |
6392 KB |
Output isn't correct |
34 |
Correct |
22 ms |
6264 KB |
Output is correct |
35 |
Correct |
50 ms |
6392 KB |
Output is correct |
36 |
Incorrect |
22 ms |
6392 KB |
Output isn't correct |
37 |
Correct |
28 ms |
6392 KB |
Output is correct |
38 |
Correct |
58 ms |
6392 KB |
Output is correct |
39 |
Incorrect |
26 ms |
6480 KB |
Output isn't correct |
40 |
Correct |
31 ms |
6392 KB |
Output is correct |
41 |
Correct |
86 ms |
6508 KB |
Output is correct |
42 |
Incorrect |
28 ms |
6776 KB |
Output isn't correct |
43 |
Correct |
35 ms |
6844 KB |
Output is correct |
44 |
Correct |
110 ms |
6948 KB |
Output is correct |
45 |
Incorrect |
33 ms |
6780 KB |
Output isn't correct |
46 |
Correct |
39 ms |
6780 KB |
Output is correct |
47 |
Correct |
137 ms |
6904 KB |
Output is correct |
48 |
Incorrect |
38 ms |
6908 KB |
Output isn't correct |
49 |
Correct |
46 ms |
6904 KB |
Output is correct |
50 |
Correct |
151 ms |
7032 KB |
Output is correct |
51 |
Incorrect |
43 ms |
7036 KB |
Output isn't correct |
52 |
Correct |
69 ms |
7016 KB |
Output is correct |
53 |
Correct |
168 ms |
7032 KB |
Output is correct |
54 |
Incorrect |
51 ms |
7288 KB |
Output isn't correct |
55 |
Correct |
63 ms |
7236 KB |
Output is correct |
56 |
Correct |
197 ms |
7376 KB |
Output is correct |
57 |
Incorrect |
52 ms |
7288 KB |
Output isn't correct |
58 |
Correct |
63 ms |
7300 KB |
Output is correct |
59 |
Correct |
231 ms |
7360 KB |
Output is correct |
60 |
Incorrect |
59 ms |
7544 KB |
Output isn't correct |
61 |
Correct |
72 ms |
7412 KB |
Output is correct |
62 |
Correct |
286 ms |
7500 KB |
Output is correct |
63 |
Execution timed out |
1058 ms |
7544 KB |
Time limit exceeded |
64 |
Execution timed out |
1088 ms |
7416 KB |
Time limit exceeded |
65 |
Execution timed out |
1087 ms |
7416 KB |
Time limit exceeded |
66 |
Execution timed out |
1084 ms |
7416 KB |
Time limit exceeded |
67 |
Execution timed out |
1090 ms |
7416 KB |
Time limit exceeded |
68 |
Execution timed out |
1055 ms |
7416 KB |
Time limit exceeded |
69 |
Execution timed out |
1063 ms |
7420 KB |
Time limit exceeded |
70 |
Execution timed out |
1071 ms |
7416 KB |
Time limit exceeded |
71 |
Execution timed out |
1075 ms |
7416 KB |
Time limit exceeded |
72 |
Execution timed out |
1065 ms |
7544 KB |
Time limit exceeded |
73 |
Execution timed out |
1071 ms |
7416 KB |
Time limit exceeded |
74 |
Execution timed out |
1063 ms |
7416 KB |
Time limit exceeded |
75 |
Execution timed out |
1082 ms |
7416 KB |
Time limit exceeded |
76 |
Execution timed out |
1068 ms |
7416 KB |
Time limit exceeded |
77 |
Execution timed out |
1063 ms |
7416 KB |
Time limit exceeded |
78 |
Execution timed out |
1046 ms |
7372 KB |
Time limit exceeded |
79 |
Execution timed out |
1064 ms |
7416 KB |
Time limit exceeded |
80 |
Execution timed out |
1066 ms |
7436 KB |
Time limit exceeded |
81 |
Execution timed out |
1082 ms |
7416 KB |
Time limit exceeded |
82 |
Execution timed out |
1087 ms |
7416 KB |
Time limit exceeded |
83 |
Execution timed out |
1074 ms |
7416 KB |
Time limit exceeded |
84 |
Execution timed out |
1071 ms |
7480 KB |
Time limit exceeded |
85 |
Execution timed out |
1078 ms |
7416 KB |
Time limit exceeded |
86 |
Execution timed out |
1065 ms |
7416 KB |
Time limit exceeded |
87 |
Execution timed out |
1078 ms |
7416 KB |
Time limit exceeded |
88 |
Execution timed out |
1071 ms |
7416 KB |
Time limit exceeded |
89 |
Execution timed out |
1087 ms |
7544 KB |
Time limit exceeded |
90 |
Execution timed out |
1075 ms |
7416 KB |
Time limit exceeded |
91 |
Execution timed out |
1070 ms |
7416 KB |
Time limit exceeded |
92 |
Execution timed out |
1067 ms |
7416 KB |
Time limit exceeded |