답안 #799536

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799536 2023-07-31T15:42:01 Z Baytoro Mecho (IOI09_mecho) C++17
100 / 100
200 ms 6272 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;
	ll l=-1,r=1e9;
	while(l<r){
		int md=(l+r+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);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 110 ms 6104 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 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 1 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 564 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 7 ms 2744 KB Output is correct
34 Correct 5 ms 2772 KB Output is correct
35 Correct 28 ms 2784 KB Output is correct
36 Correct 7 ms 3156 KB Output is correct
37 Correct 8 ms 3156 KB Output is correct
38 Correct 33 ms 3160 KB Output is correct
39 Correct 7 ms 3412 KB Output is correct
40 Correct 11 ms 3524 KB Output is correct
41 Correct 39 ms 3508 KB Output is correct
42 Correct 11 ms 3844 KB Output is correct
43 Correct 8 ms 3796 KB Output is correct
44 Correct 61 ms 3864 KB Output is correct
45 Correct 13 ms 4240 KB Output is correct
46 Correct 9 ms 4180 KB Output is correct
47 Correct 74 ms 4224 KB Output is correct
48 Correct 12 ms 4564 KB Output is correct
49 Correct 13 ms 4564 KB Output is correct
50 Correct 92 ms 4576 KB Output is correct
51 Correct 16 ms 4836 KB Output is correct
52 Correct 15 ms 4820 KB Output is correct
53 Correct 91 ms 4928 KB Output is correct
54 Correct 15 ms 5176 KB Output is correct
55 Correct 15 ms 5224 KB Output is correct
56 Correct 124 ms 5288 KB Output is correct
57 Correct 20 ms 5588 KB Output is correct
58 Correct 15 ms 5600 KB Output is correct
59 Correct 128 ms 5640 KB Output is correct
60 Correct 21 ms 5984 KB Output is correct
61 Correct 29 ms 5960 KB Output is correct
62 Correct 162 ms 5976 KB Output is correct
63 Correct 127 ms 5996 KB Output is correct
64 Correct 200 ms 5908 KB Output is correct
65 Correct 175 ms 5988 KB Output is correct
66 Correct 118 ms 5984 KB Output is correct
67 Correct 103 ms 5988 KB Output is correct
68 Correct 47 ms 6028 KB Output is correct
69 Correct 47 ms 6016 KB Output is correct
70 Correct 44 ms 6016 KB Output is correct
71 Correct 36 ms 6036 KB Output is correct
72 Correct 30 ms 6032 KB Output is correct
73 Correct 37 ms 6228 KB Output is correct
74 Correct 64 ms 6272 KB Output is correct
75 Correct 68 ms 6272 KB Output is correct
76 Correct 85 ms 6260 KB Output is correct
77 Correct 91 ms 6268 KB Output is correct
78 Correct 80 ms 6236 KB Output is correct
79 Correct 55 ms 6240 KB Output is correct
80 Correct 78 ms 6240 KB Output is correct
81 Correct 81 ms 6240 KB Output is correct
82 Correct 58 ms 6160 KB Output is correct
83 Correct 84 ms 6148 KB Output is correct
84 Correct 81 ms 6244 KB Output is correct
85 Correct 70 ms 6196 KB Output is correct
86 Correct 78 ms 6116 KB Output is correct
87 Correct 75 ms 6204 KB Output is correct
88 Correct 84 ms 6124 KB Output is correct
89 Correct 99 ms 6148 KB Output is correct
90 Correct 103 ms 6220 KB Output is correct
91 Correct 83 ms 6144 KB Output is correct
92 Correct 89 ms 6144 KB Output is correct