Submission #799533

# Submission time Handle Problem Language Result Execution time Memory
799533 2023-07-31T15:41:27 Z Baytoro Mecho (IOI09_mecho) C++17
100 / 100
197 ms 6276 KB
#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl "\n"
#define ll long long
//#define int long long
void fopn(string name){
	freopen((name+".in").c_str(),"r",stdin);
	freopen((name+".out").c_str(),"w",stdout);
}
const ll INF=1e18,mod=1e9,N=1e7+5;
int n,s;
char a[805][805];
pair<int,int> cur,last;
int dist[805][805][2];
vector<int> dx={1,-1,0, 0},
			dy={0, 0,1,-1};
bool okh(int x, int y){
	return (x>0 && x<=n && y>0 && y<=n && (a[x][y]=='M' || a[x][y]=='G' || a[x][y]=='H'));
}
bool okm(int x, int y){
	return (x>0 && x<=n && y>0 && y<=n && (a[x][y]=='M' || a[x][y]=='G' || a[x][y]=='D'));
}
queue<pair<int,int>> dq;
bool check(int D){
	if(dist[cur.fr][cur.sc][0]<=D) return 0;
	while(!dq.empty()) dq.pop();
	dq.push({cur.fr,cur.sc});
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)
			dist[i][j][1]=0;
	}
	dist[cur.fr][cur.sc][1]=1;
	while(!dq.empty()){
		pair<int,int> a=dq.front();
		dq.pop();
		for(int i=0;i<4;i++){
			int x=a.fr+dx[i],y=a.sc+dy[i];
			int d=dist[a.fr][a.sc][1]+1;
			if(okm(x,y) && !dist[x][y][1]){
				if((d-1)/s+1+D<=dist[x][y][0]){
					dq.push({x,y});
					dist[x][y][1]=d;
				}
				if(x==last.fr && y==last.sc && (d-1)/s+D-((d-1)%s==0)<=dist[x][y][0]) return 1;
			}
		}
	}
	return dist[last.fr][last.sc][1];
}
void solve(){
	queue<pair<int,int>> dq;
	cin>>n>>s;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
			if(a[i][j]=='H'){
				dq.push({i,j});
				dist[i][j][0]=1;
			}
			else if(a[i][j]=='M')
				cur={i,j};
			else if(a[i][j]=='D')
				last={i,j};
		}
	}
	while(!dq.empty()){
		pair<int,int> a=dq.front();
		dq.pop();
		for(int i=0;i<4;i++){
			int x=a.fr+dx[i],y=a.sc+dy[i];
			if(okh(x,y) && !dist[x][y][0]){
				dist[x][y][0]=dist[a.fr][a.sc][0]+1;
				dq.push({x,y});
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			dist[i][j][0]-=(dist[i][j][0]>0);
		}
	}
	dist[last.fr][last.sc][0]=1e9;
	int l=-1,r=1e9;
	while(l<r){
		int md=(l)+(r-l+1)/2;
		if(check(md)) l=md;
		else r=md-1;
	}
	cout<<l<<endl;
}
main(){
	ios;
	int T=1;
	//cin>>T;
	for(int i=1;i<=T;i++){
		//cout<<"Case #"<<i<<": ";
		solve();
	}
}

Compilation message

mecho.cpp:97:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   97 | main(){
      | ^~~~
mecho.cpp: In function 'void fopn(std::string)':
mecho.cpp:13:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:14:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 102 ms 6104 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 468 KB Output is correct
20 Correct 0 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 724 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 5 ms 2772 KB Output is correct
34 Correct 5 ms 2772 KB Output is correct
35 Correct 21 ms 2772 KB Output is correct
36 Correct 5 ms 3156 KB Output is correct
37 Correct 8 ms 3156 KB Output is correct
38 Correct 39 ms 3164 KB Output is correct
39 Correct 7 ms 3412 KB Output is correct
40 Correct 6 ms 3412 KB Output is correct
41 Correct 43 ms 3492 KB Output is correct
42 Correct 11 ms 3800 KB Output is correct
43 Correct 7 ms 3836 KB Output is correct
44 Correct 46 ms 3764 KB Output is correct
45 Correct 14 ms 4356 KB Output is correct
46 Correct 13 ms 4184 KB Output is correct
47 Correct 55 ms 4180 KB Output is correct
48 Correct 11 ms 4564 KB Output is correct
49 Correct 15 ms 4464 KB Output is correct
50 Correct 66 ms 4560 KB Output is correct
51 Correct 13 ms 4820 KB Output is correct
52 Correct 15 ms 4920 KB Output is correct
53 Correct 110 ms 4944 KB Output is correct
54 Correct 15 ms 5204 KB Output is correct
55 Correct 16 ms 5204 KB Output is correct
56 Correct 96 ms 5296 KB Output is correct
57 Correct 17 ms 5628 KB Output is correct
58 Correct 15 ms 5544 KB Output is correct
59 Correct 122 ms 5640 KB Output is correct
60 Correct 27 ms 5972 KB Output is correct
61 Correct 18 ms 5968 KB Output is correct
62 Correct 173 ms 5964 KB Output is correct
63 Correct 123 ms 5988 KB Output is correct
64 Correct 185 ms 5988 KB Output is correct
65 Correct 197 ms 5984 KB Output is correct
66 Correct 117 ms 5904 KB Output is correct
67 Correct 112 ms 5988 KB Output is correct
68 Correct 49 ms 6012 KB Output is correct
69 Correct 96 ms 5964 KB Output is correct
70 Correct 33 ms 6012 KB Output is correct
71 Correct 35 ms 6028 KB Output is correct
72 Correct 43 ms 5948 KB Output is correct
73 Correct 48 ms 6276 KB Output is correct
74 Correct 62 ms 6228 KB Output is correct
75 Correct 78 ms 6264 KB Output is correct
76 Correct 62 ms 6264 KB Output is correct
77 Correct 90 ms 6268 KB Output is correct
78 Correct 74 ms 6136 KB Output is correct
79 Correct 55 ms 6220 KB Output is correct
80 Correct 76 ms 6236 KB Output is correct
81 Correct 74 ms 6244 KB Output is correct
82 Correct 60 ms 6160 KB Output is correct
83 Correct 81 ms 6196 KB Output is correct
84 Correct 73 ms 6200 KB Output is correct
85 Correct 70 ms 6200 KB Output is correct
86 Correct 86 ms 6204 KB Output is correct
87 Correct 77 ms 6164 KB Output is correct
88 Correct 82 ms 6144 KB Output is correct
89 Correct 100 ms 6152 KB Output is correct
90 Correct 89 ms 6104 KB Output is correct
91 Correct 97 ms 6144 KB Output is correct
92 Correct 83 ms 6100 KB Output is correct