# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1102871 |
2024-10-19T05:53:42 Z |
salmon |
Mecho (IOI09_mecho) |
C++14 |
|
371 ms |
3068 KB |
#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;
}
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]);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
336 KB |
Output is correct |
2 |
Correct |
4 ms |
336 KB |
Output is correct |
3 |
Correct |
3 ms |
336 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
472 KB |
Output is correct |
6 |
Correct |
4 ms |
336 KB |
Output is correct |
7 |
Correct |
332 ms |
2444 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
3 ms |
336 KB |
Output is correct |
10 |
Correct |
3 ms |
336 KB |
Output is correct |
11 |
Correct |
3 ms |
336 KB |
Output is correct |
12 |
Correct |
4 ms |
592 KB |
Output is correct |
13 |
Correct |
4 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
4 ms |
336 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
4 ms |
516 KB |
Output is correct |
18 |
Correct |
4 ms |
336 KB |
Output is correct |
19 |
Correct |
5 ms |
336 KB |
Output is correct |
20 |
Correct |
4 ms |
336 KB |
Output is correct |
21 |
Correct |
4 ms |
336 KB |
Output is correct |
22 |
Correct |
4 ms |
336 KB |
Output is correct |
23 |
Correct |
5 ms |
336 KB |
Output is correct |
24 |
Correct |
4 ms |
336 KB |
Output is correct |
25 |
Correct |
4 ms |
604 KB |
Output is correct |
26 |
Correct |
4 ms |
592 KB |
Output is correct |
27 |
Correct |
4 ms |
420 KB |
Output is correct |
28 |
Correct |
4 ms |
592 KB |
Output is correct |
29 |
Correct |
4 ms |
592 KB |
Output is correct |
30 |
Correct |
4 ms |
592 KB |
Output is correct |
31 |
Correct |
5 ms |
500 KB |
Output is correct |
32 |
Correct |
4 ms |
592 KB |
Output is correct |
33 |
Correct |
30 ms |
1276 KB |
Output is correct |
34 |
Correct |
25 ms |
1104 KB |
Output is correct |
35 |
Correct |
49 ms |
1104 KB |
Output is correct |
36 |
Correct |
35 ms |
1408 KB |
Output is correct |
37 |
Correct |
31 ms |
1360 KB |
Output is correct |
38 |
Correct |
68 ms |
1400 KB |
Output is correct |
39 |
Correct |
43 ms |
1512 KB |
Output is correct |
40 |
Correct |
38 ms |
1532 KB |
Output is correct |
41 |
Correct |
96 ms |
1360 KB |
Output is correct |
42 |
Correct |
53 ms |
1616 KB |
Output is correct |
43 |
Correct |
44 ms |
1616 KB |
Output is correct |
44 |
Correct |
94 ms |
1644 KB |
Output is correct |
45 |
Correct |
59 ms |
1616 KB |
Output is correct |
46 |
Correct |
55 ms |
1616 KB |
Output is correct |
47 |
Correct |
113 ms |
1616 KB |
Output is correct |
48 |
Correct |
75 ms |
1872 KB |
Output is correct |
49 |
Correct |
65 ms |
1872 KB |
Output is correct |
50 |
Correct |
134 ms |
1880 KB |
Output is correct |
51 |
Correct |
85 ms |
1872 KB |
Output is correct |
52 |
Correct |
75 ms |
1872 KB |
Output is correct |
53 |
Correct |
172 ms |
1864 KB |
Output is correct |
54 |
Correct |
95 ms |
1872 KB |
Output is correct |
55 |
Correct |
85 ms |
2112 KB |
Output is correct |
56 |
Correct |
192 ms |
2112 KB |
Output is correct |
57 |
Correct |
122 ms |
2140 KB |
Output is correct |
58 |
Correct |
93 ms |
2128 KB |
Output is correct |
59 |
Correct |
216 ms |
2240 KB |
Output is correct |
60 |
Correct |
125 ms |
2268 KB |
Output is correct |
61 |
Correct |
112 ms |
2344 KB |
Output is correct |
62 |
Correct |
248 ms |
2376 KB |
Output is correct |
63 |
Correct |
260 ms |
2348 KB |
Output is correct |
64 |
Correct |
289 ms |
2124 KB |
Output is correct |
65 |
Correct |
250 ms |
2332 KB |
Output is correct |
66 |
Correct |
268 ms |
2128 KB |
Output is correct |
67 |
Correct |
33 ms |
2364 KB |
Output is correct |
68 |
Correct |
252 ms |
2348 KB |
Output is correct |
69 |
Correct |
208 ms |
2344 KB |
Output is correct |
70 |
Correct |
252 ms |
2384 KB |
Output is correct |
71 |
Correct |
249 ms |
2384 KB |
Output is correct |
72 |
Correct |
239 ms |
2384 KB |
Output is correct |
73 |
Correct |
276 ms |
2924 KB |
Output is correct |
74 |
Correct |
336 ms |
2896 KB |
Output is correct |
75 |
Correct |
361 ms |
2896 KB |
Output is correct |
76 |
Correct |
357 ms |
3068 KB |
Output is correct |
77 |
Correct |
371 ms |
2896 KB |
Output is correct |
78 |
Correct |
34 ms |
2640 KB |
Output is correct |
79 |
Correct |
316 ms |
2860 KB |
Output is correct |
80 |
Correct |
326 ms |
2640 KB |
Output is correct |
81 |
Correct |
329 ms |
2640 KB |
Output is correct |
82 |
Correct |
329 ms |
2640 KB |
Output is correct |
83 |
Correct |
341 ms |
2640 KB |
Output is correct |
84 |
Correct |
363 ms |
2644 KB |
Output is correct |
85 |
Correct |
359 ms |
2640 KB |
Output is correct |
86 |
Correct |
370 ms |
2632 KB |
Output is correct |
87 |
Correct |
355 ms |
2640 KB |
Output is correct |
88 |
Correct |
356 ms |
2632 KB |
Output is correct |
89 |
Correct |
370 ms |
2448 KB |
Output is correct |
90 |
Correct |
358 ms |
2688 KB |
Output is correct |
91 |
Correct |
340 ms |
2680 KB |
Output is correct |
92 |
Correct |
341 ms |
2556 KB |
Output is correct |