답안 #799526

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799526 2023-07-31T15:36:35 Z Baytoro Mecho (IOI09_mecho) C++17
30 / 100
1000 ms 12160 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];
queue<pair<int,int>> dq;
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'));
}
bool check(int D){
	if(dist[cur.fr][cur.sc][0]<=D) return 0;
	queue<pair<int,int>> dq;
	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(){
	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]=INF;
	int l=-1,r=1e9;
	while(l<r){
		int md=(l+r)/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:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   96 | 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 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Execution timed out 1074 ms 11864 KB Time limit exceeded
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Execution timed out 1080 ms 340 KB Time limit exceeded
11 Execution timed out 1054 ms 340 KB Time limit exceeded
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 Execution timed out 1086 ms 340 KB Time limit exceeded
16 Execution timed out 1076 ms 392 KB Time limit exceeded
17 Execution timed out 1068 ms 464 KB Time limit exceeded
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Execution timed out 1085 ms 464 KB Time limit exceeded
21 Execution timed out 1071 ms 468 KB Time limit exceeded
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Execution timed out 1071 ms 596 KB Time limit exceeded
25 Execution timed out 1063 ms 596 KB Time limit exceeded
26 Correct 1 ms 596 KB Output is correct
27 Execution timed out 1076 ms 600 KB Time limit exceeded
28 Execution timed out 1004 ms 724 KB Time limit exceeded
29 Execution timed out 1070 ms 724 KB Time limit exceeded
30 Execution timed out 1085 ms 724 KB Time limit exceeded
31 Execution timed out 1079 ms 724 KB Time limit exceeded
32 Correct 1 ms 724 KB Output is correct
33 Execution timed out 1082 ms 4052 KB Time limit exceeded
34 Correct 9 ms 4064 KB Output is correct
35 Execution timed out 1074 ms 4052 KB Time limit exceeded
36 Execution timed out 1067 ms 4816 KB Time limit exceeded
37 Execution timed out 1070 ms 4836 KB Time limit exceeded
38 Execution timed out 1072 ms 4820 KB Time limit exceeded
39 Correct 13 ms 5868 KB Output is correct
40 Execution timed out 1083 ms 5844 KB Time limit exceeded
41 Execution timed out 1084 ms 5844 KB Time limit exceeded
42 Execution timed out 1085 ms 6884 KB Time limit exceeded
43 Execution timed out 1032 ms 6872 KB Time limit exceeded
44 Execution timed out 1075 ms 6868 KB Time limit exceeded
45 Execution timed out 1064 ms 7924 KB Time limit exceeded
46 Execution timed out 1079 ms 7920 KB Time limit exceeded
47 Execution timed out 1069 ms 7892 KB Time limit exceeded
48 Execution timed out 1069 ms 8700 KB Time limit exceeded
49 Execution timed out 1070 ms 8660 KB Time limit exceeded
50 Execution timed out 1086 ms 8660 KB Time limit exceeded
51 Execution timed out 1075 ms 9428 KB Time limit exceeded
52 Correct 17 ms 9428 KB Output is correct
53 Execution timed out 1078 ms 9428 KB Time limit exceeded
54 Execution timed out 1069 ms 10068 KB Time limit exceeded
55 Correct 19 ms 10176 KB Output is correct
56 Execution timed out 1081 ms 10184 KB Time limit exceeded
57 Correct 21 ms 10836 KB Output is correct
58 Execution timed out 1084 ms 10820 KB Time limit exceeded
59 Execution timed out 1089 ms 10840 KB Time limit exceeded
60 Execution timed out 1072 ms 11604 KB Time limit exceeded
61 Execution timed out 1078 ms 11616 KB Time limit exceeded
62 Execution timed out 1062 ms 11724 KB Time limit exceeded
63 Correct 98 ms 11640 KB Output is correct
64 Correct 181 ms 11672 KB Output is correct
65 Execution timed out 1078 ms 11608 KB Time limit exceeded
66 Correct 118 ms 11668 KB Output is correct
67 Correct 107 ms 11668 KB Output is correct
68 Correct 51 ms 11692 KB Output is correct
69 Execution timed out 1070 ms 11596 KB Time limit exceeded
70 Execution timed out 1086 ms 11732 KB Time limit exceeded
71 Execution timed out 1088 ms 11596 KB Time limit exceeded
72 Correct 38 ms 11856 KB Output is correct
73 Correct 54 ms 12148 KB Output is correct
74 Execution timed out 1082 ms 12132 KB Time limit exceeded
75 Correct 72 ms 12108 KB Output is correct
76 Execution timed out 1081 ms 12144 KB Time limit exceeded
77 Execution timed out 1035 ms 12116 KB Time limit exceeded
78 Correct 75 ms 12148 KB Output is correct
79 Correct 57 ms 12160 KB Output is correct
80 Execution timed out 1087 ms 12108 KB Time limit exceeded
81 Correct 70 ms 12108 KB Output is correct
82 Execution timed out 1086 ms 12108 KB Time limit exceeded
83 Correct 77 ms 11980 KB Output is correct
84 Execution timed out 1083 ms 12028 KB Time limit exceeded
85 Execution timed out 1073 ms 11980 KB Time limit exceeded
86 Execution timed out 1071 ms 12024 KB Time limit exceeded
87 Correct 93 ms 12016 KB Output is correct
88 Correct 79 ms 11988 KB Output is correct
89 Execution timed out 1093 ms 11980 KB Time limit exceeded
90 Execution timed out 1083 ms 11980 KB Time limit exceeded
91 Execution timed out 1087 ms 11976 KB Time limit exceeded
92 Execution timed out 1086 ms 11908 KB Time limit exceeded