#include <bits/stdc++.h>
using namespace std;
int N;
int S;
vector<pair<int,int>> trans = {{1,0}, {-1,0},{0,1},{0,-1}};
char clst[810][810];
int s1,s2;
int f1,f2;
queue<pair<int,int>> q[2];
queue<pair<int,int>> m[2];
vector<pair<int,int>> spood;
bool visited[810][810];
bool vt[810][810];
int cq,cm;
bool check(int i, int j){
if(i < 0) return false;
if(i >= N) return false;
if(j < 0) return false;
if(j >= N) return false;
return true;
}
void spreadhoney(int it){
while(!q[it].empty()){
pair<int,int> ii = q[it].front();
q[it].pop();
int i = ii.first;
int j = ii.second;
if(visited[i][j]) continue;
visited[i][j] = true;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = ii.first + i;
int nj = ii.second + j;
if(!check(ni,nj)) continue;
if(visited[ni][nj]) continue;
q[1 - it].push({ni,nj});
}
}
}
int main(){
scanf(" %d",&N);
scanf(" %d",&S);
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
scanf(" %c",&clst[i][j]);
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
visited[i][j] = false;
vt[i][j] = false;
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(clst[i][j] == 'T'){
visited[i][j] = true;
vt[i][j] = true;
}
else if(clst[i][j] == 'H'){
q[1].push({i,j});
}
else if(clst[i][j] == 'M'){
s1 = i;
s2 = j;
}
else if(clst[i][j] == 'D'){
f1 = i;
f2 = j;
visited[i][j] = true;
}
}
}
cq = 0;
cm = 0;
spreadhoney(1);
vt[s1][s2] = true;
for(pair<int,int> ii : trans){
int ni = s1 + ii.first;
int nj = s2 + ii.second;
if(!check(ni,nj)) continue;
m[0].push({ni,nj});
}
bool flag;
while(true){
int it = cm % 2;
bool done = false;
for(int i = 0; i < S - 1; i++, cm++){
it = cm % 2;
while(!m[it].empty()){
pair<int,int> ii = m[it].front();
m[it].pop();
int i = ii.first;
int j = ii.second;
if(i == f1 && j == f2){
done = true;
flag = true;
break;
}
if(vt[i][j]) continue;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = i + ii.first;
int nj = j + ii.second;
if(!check(ni,nj)) continue;
if(vt[ni][nj]) continue;
m[1 - it].push({ni,nj});
}
}
if(done) break;
if(m[1 - it].empty()){
done = true;
flag = false;
break;
}
}
if(done) break;
it = cq % 2;
spreadhoney(it);
cq++;
it = cm % 2;
while(!m[it].empty()){
pair<int,int> ii = m[it].front();
m[it].pop();
int i = ii.first;
int j = ii.second;
//printf("%d %d\n",i,j);
if(i == f1 && j == f2){
done = true;
flag = true;
break;
}
if(vt[i][j]) continue;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = i + ii.first;
int nj = j + ii.second;
if(!check(ni,nj)) continue;
m[1 - it].push({ni,nj});
}
}
//printf("\n");
if(done) break;
if(m[1 - it].empty()){
done = true;
flag = false;
break;
}
cm++;
if(done) break;
}
if(!flag){
printf("-1");
return 0;
}
return 0;
int s = 0;
int e = 800*800;
int m1;
while(s != e){
m1 = (s + e + 1)/2;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
visited[i][j] = false;
vt[i][j] = false;
}
}
while(!m[0].empty()) m[0].pop();
while(!m[1].empty()) m[1].pop();
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(clst[i][j] == 'T'){
visited[i][j] = true;
vt[i][j] = true;
}
else if(clst[i][j] == 'H'){
q[1].push({i,j});
}
else if(clst[i][j] == 'D'){
visited[i][j] = true;
}
}
}
cq = 0;
cm = 0;
spreadhoney(1);
vt[s1][s2] = true;
for(pair<int,int> ii : trans){
int ni = s1 + ii.first;
int nj = s2 + ii.second;
if(!check(ni,nj)) continue;
m[0].push({ni,nj});
}
for(int i = 0; i < m1; i++, cq++){
int it = cq % 2;
spreadhoney(it);
}
if(visited[s1][s2]){
e = m1 - 1;
continue;
}
/*for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%d ",visited[i][j]);
}
printf("\n");
}
printf("\n");*/
bool flag;
while(true){
int it = cm % 2;
bool done = false;
for(int i = 0; i < S - 1; i++, cm++){
it = cm % 2;
while(!m[it].empty()){
pair<int,int> ii = m[it].front();
m[it].pop();
int i = ii.first;
int j = ii.second;
//printf("%d %d\n",i,j);
if(i == f1 && j == f2){
done = true;
flag = true;
break;
}
if(vt[i][j]) continue;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = i + ii.first;
int nj = j + ii.second;
if(!check(ni,nj)) continue;
m[1 - it].push({ni,nj});
}
}
//printf("\n");
if(done) break;
if(m[1 - it].empty()){
done = true;
flag = false;
break;
}
}
if(done) break;
it = cq % 2;
spreadhoney(it);
cq++;
it = cm % 2;
while(!m[it].empty()){
pair<int,int> ii = m[it].front();
m[it].pop();
int i = ii.first;
int j = ii.second;
//printf("%d %d\n",i,j);
if(i == f1 && j == f2){
done = true;
flag = true;
break;
}
if(vt[i][j]) continue;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = i + ii.first;
int nj = j + ii.second;
if(!check(ni,nj)) continue;
m[1 - it].push({ni,nj});
}
}
//printf("\n");
if(done) break;
if(m[1 - it].empty()){
done = true;
flag = false;
break;
}
cm++;
if(done) break;
}
if(flag){
s = m1;
}
else{
e = m1 - 1;
}
}
printf("%d",s);
}
/*
7 3
TTTTTTT
TGGGGGT
THGGGGG
MGGGGGD
TGGTTTT
TGGGGGT
TTTTTHT
*/
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
mecho.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf(" %d",&S);
| ~~~~~^~~~~~~~~~
mecho.cpp:54:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf(" %c",&clst[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
7 |
Incorrect |
37 ms |
2588 KB |
Output isn't correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
33 |
Incorrect |
5 ms |
1104 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
1116 KB |
Output isn't correct |
35 |
Incorrect |
6 ms |
1064 KB |
Output isn't correct |
36 |
Incorrect |
6 ms |
1476 KB |
Output isn't correct |
37 |
Incorrect |
6 ms |
1360 KB |
Output isn't correct |
38 |
Incorrect |
7 ms |
1360 KB |
Output isn't correct |
39 |
Incorrect |
7 ms |
1528 KB |
Output isn't correct |
40 |
Incorrect |
7 ms |
1360 KB |
Output isn't correct |
41 |
Incorrect |
9 ms |
1360 KB |
Output isn't correct |
42 |
Incorrect |
10 ms |
1616 KB |
Output isn't correct |
43 |
Incorrect |
9 ms |
1616 KB |
Output isn't correct |
44 |
Incorrect |
14 ms |
1616 KB |
Output isn't correct |
45 |
Incorrect |
10 ms |
1616 KB |
Output isn't correct |
46 |
Incorrect |
10 ms |
1616 KB |
Output isn't correct |
47 |
Incorrect |
19 ms |
1616 KB |
Output isn't correct |
48 |
Incorrect |
12 ms |
1636 KB |
Output isn't correct |
49 |
Incorrect |
12 ms |
1884 KB |
Output isn't correct |
50 |
Incorrect |
15 ms |
1788 KB |
Output isn't correct |
51 |
Incorrect |
14 ms |
1872 KB |
Output isn't correct |
52 |
Incorrect |
15 ms |
1872 KB |
Output isn't correct |
53 |
Incorrect |
17 ms |
1872 KB |
Output isn't correct |
54 |
Incorrect |
21 ms |
2076 KB |
Output isn't correct |
55 |
Incorrect |
16 ms |
1872 KB |
Output isn't correct |
56 |
Incorrect |
20 ms |
1872 KB |
Output isn't correct |
57 |
Incorrect |
18 ms |
2072 KB |
Output isn't correct |
58 |
Incorrect |
19 ms |
2056 KB |
Output isn't correct |
59 |
Incorrect |
22 ms |
2128 KB |
Output isn't correct |
60 |
Incorrect |
20 ms |
2132 KB |
Output isn't correct |
61 |
Incorrect |
21 ms |
2296 KB |
Output isn't correct |
62 |
Incorrect |
27 ms |
2176 KB |
Output isn't correct |
63 |
Incorrect |
42 ms |
2120 KB |
Output isn't correct |
64 |
Incorrect |
32 ms |
2128 KB |
Output isn't correct |
65 |
Incorrect |
42 ms |
2124 KB |
Output isn't correct |
66 |
Incorrect |
47 ms |
2120 KB |
Output isn't correct |
67 |
Correct |
35 ms |
2128 KB |
Output is correct |
68 |
Incorrect |
33 ms |
2344 KB |
Output isn't correct |
69 |
Incorrect |
27 ms |
2340 KB |
Output isn't correct |
70 |
Incorrect |
38 ms |
2384 KB |
Output isn't correct |
71 |
Incorrect |
35 ms |
2344 KB |
Output isn't correct |
72 |
Incorrect |
24 ms |
2300 KB |
Output isn't correct |
73 |
Incorrect |
40 ms |
3064 KB |
Output isn't correct |
74 |
Incorrect |
32 ms |
2712 KB |
Output isn't correct |
75 |
Incorrect |
36 ms |
2896 KB |
Output isn't correct |
76 |
Incorrect |
38 ms |
2908 KB |
Output isn't correct |
77 |
Incorrect |
38 ms |
3064 KB |
Output isn't correct |
78 |
Correct |
37 ms |
2748 KB |
Output is correct |
79 |
Incorrect |
32 ms |
2552 KB |
Output isn't correct |
80 |
Incorrect |
38 ms |
2640 KB |
Output isn't correct |
81 |
Incorrect |
38 ms |
2632 KB |
Output isn't correct |
82 |
Incorrect |
36 ms |
2640 KB |
Output isn't correct |
83 |
Incorrect |
38 ms |
2640 KB |
Output isn't correct |
84 |
Incorrect |
32 ms |
2384 KB |
Output isn't correct |
85 |
Incorrect |
35 ms |
2584 KB |
Output isn't correct |
86 |
Incorrect |
39 ms |
2640 KB |
Output isn't correct |
87 |
Incorrect |
36 ms |
2588 KB |
Output isn't correct |
88 |
Incorrect |
38 ms |
2640 KB |
Output isn't correct |
89 |
Incorrect |
34 ms |
2384 KB |
Output isn't correct |
90 |
Incorrect |
37 ms |
2640 KB |
Output isn't correct |
91 |
Incorrect |
35 ms |
2640 KB |
Output isn't correct |
92 |
Incorrect |
35 ms |
2636 KB |
Output isn't correct |