Submission #898284

# Submission time Handle Problem Language Result Execution time Memory
898284 2024-01-04T12:36:12 Z lalig777 Mecho (IOI09_mecho) C++14
50 / 100
186 ms 9636 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int iini, ifin, jini, jfin, n, s;

bool way_home(int m, vector<vector<int> >&bemoment, vector<vector<char> >&grid){
	queue<pair<int,int> >mecho;
	vector<vector<pair<int,int> > >mechomoment(n, vector<pair<int,int> >(n, make_pair(1e9, 0)));
	mecho.push(make_pair(iini, jini));
	mechomoment[iini][jini]=make_pair(m, 1);
	while (!mecho.empty()){
		int i=mecho.front().first;
		int j=mecho.front().second;
		mecho.pop();
		int minut=mechomoment[i][j].first, steps=mechomoment[i][j].second;
		if (steps==s){
			minut++;
			steps=0;
		}steps++;
		if ((i!=0 && grid[i-1][j]=='D')||(j!=0 && grid[i][j-1]=='D')||(i!=n-1 && grid[i+1][j]=='D')||((j!=n-1 && grid[i][j+1]=='D'))){
			return true;
		}
		if (i!=0 and bemoment[i-1][j]>minut and mechomoment[i-1][j].first==1e9){
			mechomoment[i-1][j]=make_pair(minut, steps);
			mecho.push(make_pair(i-1, j));
		}if (j!=0 and bemoment[i][j-1]>minut and mechomoment[i][j-1].first==1e9){
			mechomoment[i][j-1]=make_pair(minut, steps);
			mecho.push(make_pair(i, j-1));
		}if (i!=n-1 and bemoment[i+1][j]>minut and mechomoment[i+1][j].first==1e9){
			mechomoment[i+1][j]=make_pair(minut, steps);
			mecho.push(make_pair(i+1, j));
		}if (j!=n-1 and bemoment[i][j+1]>minut and mechomoment[i][j+1].first==1e9){
			mechomoment[i][j+1]=make_pair(minut, steps);
			mecho.push(make_pair(i, j+1));
		}
	}return false;

}

int main(){
	cin>>n>>s;
	vector<vector<char> >grid(n, vector<char>(n));
	vector<vector<int> >bemoment(n, vector<int>(n, 1e9));
	queue<pair<int,int> >grassy;
	for (int i=0; i<n; i++){
		for (int j=0; j<n; j++){
			cin>>grid[i][j];
			if (grid[i][j]=='H'){
				grassy.push(make_pair(i, j));
				bemoment[i][j]=0;
			}else if (grid[i][j]=='M'){
				iini=i;
				jini=j;
			}else if (grid[i][j]=='D'){
				ifin=i;
				jfin=j;
				bemoment[i][j]=-1;
			}else if (grid[i][j]=='T') bemoment[i][j]=-1;
		}
	}while (!grassy.empty()){
		int i=grassy.front().first;
		int j=grassy.front().second;
		grassy.pop();
		if (i!=0 and bemoment[i-1][j]==1e9){
			bemoment[i-1][j]=bemoment[i][j]+1;
			grassy.push(make_pair(i-1, j));
		}if (j!=0 and bemoment[i][j-1]==1e9){
			bemoment[i][j-1]=bemoment[i][j]+1;
			grassy.push(make_pair(i, j-1));
		}if (i!=n-1 and bemoment[i+1][j]==1e9){
			bemoment[i+1][j]=bemoment[i][j]+1;
			grassy.push(make_pair(i+1, j));
		}if (j!=n-1 and bemoment[i][j+1]==1e9){
			bemoment[i][j+1]=bemoment[i][j]+1;
			grassy.push(make_pair(i, j+1));
		}
	}int ans=-1;
	int l=0, r=bemoment[iini][ifin]-1, m;
	while (l<=r){
		m=(r-l)/2+l;
		if (l+1==r){
			if (way_home(r, bemoment, grid)){
				ans=r;
				l+=2;
			}else if (way_home(l, bemoment, grid)){
				ans=l;
				r-=2;
			}else ans=-1;
			break;
		}else if (l==r){
			if (way_home(r, bemoment, grid)){
				ans=r;
				l++;
			}else ans=-1;
			break;
		}
		if (way_home(m, bemoment, grid)) l=m;
		else r=m-1;
	}cout<<ans<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 344 KB Output is correct
5 Incorrect 0 ms 348 KB Output isn't correct
6 Correct 0 ms 436 KB Output is correct
7 Correct 93 ms 9168 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Incorrect 1 ms 344 KB Output isn't correct
11 Incorrect 1 ms 344 KB Output isn't correct
12 Correct 0 ms 348 KB Output is correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Correct 1 ms 348 KB Output is correct
15 Incorrect 1 ms 540 KB Output isn't correct
16 Correct 1 ms 348 KB Output is correct
17 Incorrect 0 ms 348 KB Output isn't correct
18 Correct 0 ms 348 KB Output is correct
19 Incorrect 1 ms 348 KB Output isn't correct
20 Correct 0 ms 348 KB Output is correct
21 Incorrect 0 ms 436 KB Output isn't correct
22 Correct 0 ms 348 KB Output is correct
23 Incorrect 1 ms 348 KB Output isn't correct
24 Correct 1 ms 600 KB Output is correct
25 Incorrect 1 ms 348 KB Output isn't correct
26 Correct 1 ms 348 KB Output is correct
27 Incorrect 1 ms 348 KB Output isn't correct
28 Correct 1 ms 348 KB Output is correct
29 Incorrect 1 ms 444 KB Output isn't correct
30 Correct 1 ms 348 KB Output is correct
31 Incorrect 1 ms 436 KB Output isn't correct
32 Correct 1 ms 348 KB Output is correct
33 Incorrect 5 ms 1116 KB Output isn't correct
34 Correct 10 ms 2184 KB Output is correct
35 Correct 18 ms 2168 KB Output is correct
36 Incorrect 9 ms 1372 KB Output isn't correct
37 Correct 13 ms 2720 KB Output is correct
38 Correct 24 ms 2672 KB Output is correct
39 Incorrect 9 ms 1628 KB Output isn't correct
40 Correct 17 ms 3224 KB Output is correct
41 Correct 34 ms 3268 KB Output is correct
42 Incorrect 11 ms 1884 KB Output isn't correct
43 Correct 20 ms 3972 KB Output is correct
44 Correct 43 ms 3916 KB Output is correct
45 Incorrect 13 ms 2140 KB Output isn't correct
46 Correct 26 ms 4636 KB Output is correct
47 Correct 49 ms 4640 KB Output is correct
48 Incorrect 15 ms 2396 KB Output isn't correct
49 Correct 29 ms 5536 KB Output is correct
50 Correct 54 ms 5416 KB Output is correct
51 Incorrect 18 ms 3104 KB Output isn't correct
52 Correct 34 ms 6328 KB Output is correct
53 Correct 91 ms 6268 KB Output is correct
54 Incorrect 21 ms 3164 KB Output isn't correct
55 Correct 40 ms 7316 KB Output is correct
56 Correct 82 ms 7472 KB Output is correct
57 Incorrect 25 ms 3676 KB Output isn't correct
58 Correct 47 ms 8340 KB Output is correct
59 Correct 103 ms 8232 KB Output is correct
60 Incorrect 27 ms 4068 KB Output isn't correct
61 Correct 53 ms 9336 KB Output is correct
62 Correct 104 ms 9296 KB Output is correct
63 Correct 121 ms 9288 KB Output is correct
64 Correct 186 ms 9572 KB Output is correct
65 Correct 185 ms 9288 KB Output is correct
66 Correct 144 ms 9300 KB Output is correct
67 Correct 124 ms 9288 KB Output is correct
68 Incorrect 34 ms 4028 KB Output isn't correct
69 Incorrect 41 ms 4600 KB Output isn't correct
70 Incorrect 34 ms 3960 KB Output isn't correct
71 Incorrect 36 ms 4132 KB Output isn't correct
72 Incorrect 56 ms 9296 KB Output isn't correct
73 Incorrect 32 ms 4188 KB Output isn't correct
74 Incorrect 31 ms 4184 KB Output isn't correct
75 Incorrect 31 ms 4188 KB Output isn't correct
76 Incorrect 33 ms 4176 KB Output isn't correct
77 Incorrect 35 ms 4176 KB Output isn't correct
78 Correct 80 ms 9352 KB Output is correct
79 Correct 63 ms 9364 KB Output is correct
80 Correct 69 ms 9564 KB Output is correct
81 Correct 76 ms 9328 KB Output is correct
82 Correct 65 ms 9304 KB Output is correct
83 Correct 95 ms 9332 KB Output is correct
84 Correct 74 ms 9352 KB Output is correct
85 Correct 70 ms 9352 KB Output is correct
86 Correct 77 ms 9432 KB Output is correct
87 Correct 100 ms 9360 KB Output is correct
88 Correct 84 ms 9336 KB Output is correct
89 Correct 89 ms 9340 KB Output is correct
90 Correct 102 ms 9636 KB Output is correct
91 Correct 85 ms 9296 KB Output is correct
92 Correct 85 ms 9580 KB Output is correct