#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
int INF = 1e9;
int n, s;
vvi FloodFill (vector<pair<int, int>>& V, vvc& CharMap){
vvi ans(n, vi(n, INF));
queue<pair<int, int>> Q;
for(pair<int, int> start : V){
Q.push(start);
ans[start.first][start.second] = 0;
}
while(!Q.empty()){
pair<int, int> act = Q.front(); Q.pop();
int Xact = act.first;
int Yact = act.second;
if(Xact > 0 and ans[Xact-1][Yact] == INF and CharMap[Xact-1][Yact] == 'G'){
ans[Xact-1][Yact] = ans[Xact][Yact]+1;
Q.push(make_pair(Xact-1, Yact));
}
if (Yact > 0 and ans[Xact][Yact-1]== INF and CharMap[Xact][Yact-1] == 'G'){
ans[Xact][Yact-1] = ans[Xact][Yact]+1;
Q.push(make_pair(Xact, Yact-1));
}
if(Xact < (n-1) and ans[Xact+1][Yact] == INF and CharMap[Xact+1][Yact] == 'G'){
ans[Xact+1][Yact] = ans[Xact][Yact]+1;
Q.push(make_pair(Xact+1, Yact));
}
if(Yact < (n-1) and ans[Xact][Yact+1] == INF and CharMap[Xact][Yact+1] == 'G'){
ans[Xact][Yact+1] = ans[Xact][Yact]+1;
Q.push(make_pair(Xact, Yact+1));
}
}
return ans;
}
bool bfsMecho (pair<int, int> StartPos, int initialTime, vvi& listaAdy, vvc& CharMap){
vvi visited (n, vi(n, 0));
visited[StartPos.first][StartPos.second] = 1;
queue<pair<int, pair<int, int>>> Q;
Q.push(make_pair(initialTime*s, StartPos));
while(!Q.empty()){
pair<int, pair<int, int>> act = Q.front(); Q.pop();
int PasosAct = act.first;
int MinuteAct = PasosAct/s;
int x = act.second.first;
int y = act.second.second;
if(CharMap[x][y] == 'D') return true;
if(x > 0 and MinuteAct < listaAdy[x-1][y] and (CharMap[x-1][y] == 'G' or CharMap[x-1][y] == 'D') and !visited[x-1][y] ){
Q.push(make_pair(PasosAct+1, make_pair(x-1, y)));
visited[x-1][y] = 1;
}
if(y > 0 and MinuteAct < listaAdy[x][y-1] and (CharMap[x][y-1] == 'G' or CharMap[x][y-1] == 'D') and !visited[x][y-1]){
Q.push(make_pair(PasosAct+1, make_pair(x, y-1)));
visited[x][y-1] = 1;
}
if(x < (n-1) and MinuteAct < listaAdy[x+1][y] and (CharMap[x+1][y] == 'G' or CharMap[x+1][y] == 'D') and !visited[x+1][y]){
Q.push(make_pair(PasosAct+1, make_pair(x+1, y)));
visited[x+1][y] = 1;
}
if(y < (n-1) and MinuteAct < listaAdy[x][y+1] and (CharMap[x][y+1] == 'G' or CharMap[x][y+1] == 'D') and !visited[x][y+1] ){
Q.push(make_pair(PasosAct+1, make_pair(x, y+1)));
visited[x][y+1] = 1;
}
}
return false;
}
int main(){
char c;
cin >> n >> s;
vector<pair<int, int>> IniciosAbejas;
pair<int, int> InicioMecho;
vvc mapa(n, vc(n));
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
cin >> c;
//T = tree --> no one can pass
//M = mecho initially
//D = target
//G = grass
//H = hive
if(c == 'M') InicioMecho = make_pair(i, j);
else if (c == 'H') IniciosAbejas.push_back(make_pair(i, j));
mapa[i][j] = c;
}
}
vvi Matrix = FloodFill(IniciosAbejas, mapa);
int hi = n*n;
int lo = 0;
while(hi >= lo){
int mid = lo + (hi-lo)/2;
if(bfsMecho(InicioMecho, mid, Matrix, mapa)) lo = mid+1;
else hi = mid-1;
}
if(hi < 0) cout << -1 << endl;
else cout << hi << endl;
/*
for(int i = 0; i < n; ++i){
cout << Matrix[i][0];
for(int j = 1; j < n; ++j){
cout << " " << Matrix[i][j];
}
cout << endl;
}*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
116 ms |
6732 KB |
Output is correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
30 |
Correct |
1 ms |
304 KB |
Output is correct |
31 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Incorrect |
11 ms |
1532 KB |
Output isn't correct |
34 |
Correct |
11 ms |
1588 KB |
Output is correct |
35 |
Correct |
27 ms |
1524 KB |
Output is correct |
36 |
Incorrect |
17 ms |
1876 KB |
Output isn't correct |
37 |
Correct |
18 ms |
1876 KB |
Output is correct |
38 |
Correct |
40 ms |
1904 KB |
Output is correct |
39 |
Incorrect |
20 ms |
2320 KB |
Output isn't correct |
40 |
Correct |
20 ms |
2364 KB |
Output is correct |
41 |
Correct |
47 ms |
2324 KB |
Output is correct |
42 |
Incorrect |
27 ms |
2784 KB |
Output isn't correct |
43 |
Correct |
24 ms |
2800 KB |
Output is correct |
44 |
Correct |
65 ms |
2812 KB |
Output is correct |
45 |
Incorrect |
29 ms |
3364 KB |
Output isn't correct |
46 |
Correct |
30 ms |
3420 KB |
Output is correct |
47 |
Correct |
65 ms |
3308 KB |
Output is correct |
48 |
Incorrect |
37 ms |
3936 KB |
Output isn't correct |
49 |
Correct |
37 ms |
3956 KB |
Output is correct |
50 |
Correct |
87 ms |
3924 KB |
Output is correct |
51 |
Incorrect |
41 ms |
4484 KB |
Output isn't correct |
52 |
Correct |
41 ms |
4544 KB |
Output is correct |
53 |
Correct |
113 ms |
4504 KB |
Output is correct |
54 |
Incorrect |
46 ms |
5220 KB |
Output isn't correct |
55 |
Correct |
46 ms |
5216 KB |
Output is correct |
56 |
Correct |
123 ms |
5192 KB |
Output is correct |
57 |
Incorrect |
60 ms |
5920 KB |
Output isn't correct |
58 |
Correct |
55 ms |
5908 KB |
Output is correct |
59 |
Correct |
139 ms |
5908 KB |
Output is correct |
60 |
Incorrect |
66 ms |
6700 KB |
Output isn't correct |
61 |
Correct |
67 ms |
6744 KB |
Output is correct |
62 |
Correct |
151 ms |
6704 KB |
Output is correct |
63 |
Correct |
152 ms |
6664 KB |
Output is correct |
64 |
Correct |
237 ms |
6748 KB |
Output is correct |
65 |
Correct |
191 ms |
6712 KB |
Output is correct |
66 |
Incorrect |
181 ms |
6744 KB |
Output isn't correct |
67 |
Correct |
161 ms |
6688 KB |
Output is correct |
68 |
Correct |
95 ms |
6748 KB |
Output is correct |
69 |
Correct |
91 ms |
6720 KB |
Output is correct |
70 |
Correct |
77 ms |
6680 KB |
Output is correct |
71 |
Correct |
92 ms |
6792 KB |
Output is correct |
72 |
Incorrect |
76 ms |
6728 KB |
Output isn't correct |
73 |
Incorrect |
104 ms |
6716 KB |
Output isn't correct |
74 |
Correct |
99 ms |
6720 KB |
Output is correct |
75 |
Correct |
103 ms |
6716 KB |
Output is correct |
76 |
Correct |
97 ms |
6736 KB |
Output is correct |
77 |
Correct |
103 ms |
6716 KB |
Output is correct |
78 |
Correct |
128 ms |
6712 KB |
Output is correct |
79 |
Correct |
87 ms |
6772 KB |
Output is correct |
80 |
Correct |
94 ms |
6720 KB |
Output is correct |
81 |
Correct |
107 ms |
6748 KB |
Output is correct |
82 |
Correct |
93 ms |
6692 KB |
Output is correct |
83 |
Correct |
119 ms |
6712 KB |
Output is correct |
84 |
Correct |
110 ms |
6832 KB |
Output is correct |
85 |
Correct |
114 ms |
6724 KB |
Output is correct |
86 |
Correct |
112 ms |
6720 KB |
Output is correct |
87 |
Correct |
113 ms |
6760 KB |
Output is correct |
88 |
Correct |
127 ms |
6700 KB |
Output is correct |
89 |
Correct |
123 ms |
6712 KB |
Output is correct |
90 |
Correct |
119 ms |
6700 KB |
Output is correct |
91 |
Correct |
113 ms |
6772 KB |
Output is correct |
92 |
Correct |
127 ms |
6700 KB |
Output is correct |