답안 #947345

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
947345 2024-03-15T22:30:20 Z narsuk Mecho (IOI09_mecho) C++17
100 / 100
687 ms 9896 KB
#include <bits/stdc++.h>
using namespace std;
//
#define ll long long int
 
#define endl "\n"; 
 
const int maxn = 801;

// array<int,n>
// pq 


int n,st;

pair<int,int>mv[4]={ {0,1},{0,-1} , {1,0}, {-1,0} };

char g[maxn][maxn];
char g1[maxn][maxn];
char g2[maxn][maxn];

bool val(int x , int y){
	if(x<0 || x>=n || y<0 || y>=n || g[x][y]=='T' )return 0;
	return 1;
}
 

int xc,yc;
int xcc;
int ycc;
int beetime[maxn][maxn];

void bbfs(){
	int vis[n][n];
	queue<pair<pair<int,int>,int>>q;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			beetime[i][j]=INT_MAX;
			if(g1[i][j]=='H'){
				q.push({{i,j},0}); 
			}
			vis[i][j]=0;
		}
	}
	while(!q.empty()){
		pair<int,int>cur;
		cur=q.front().first;
		int dt = q.front().second;
		q.pop();
		if(vis[cur.first][cur.second])continue;
		vis[cur.first][cur.second]=1;

		if(g1[cur.first][cur.second]=='D' || g1[cur.first][cur.second]=='T'){
			continue;
		} 
		beetime[cur.first][cur.second]=dt;
		// cout<<cur.first<<" "<<cur.second<<endl;
		for(auto ele : mv){
			int xx = cur.first+ele.first;
			int yy = cur.second+ele.second;
			if(val(xx,yy)){
				q.push({{xx,yy},dt+1});
			}
		}
	}

}




// int mbfs(int x){
bool tme(int marc , int bee){
	int ccur = marc/st;
	// if(ccur*st!=marc){
	// 	return (marc/st)-1<bee;
	// }
	return marc/st <bee;
}

 

bool check(int x){

	bbfs();


	queue<pair<pair<int,int>,int>>q;
	bool vis[n][n];
	int dist[n][n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			vis[i][j]=0;
			dist[i][j]=INT_MAX;
		}
	}

	q.push({{xc,yc},0});
	if(beetime[xc][yc]<=x)q.pop();
	while(!q.empty()){
		pair<int,int>cur=q.front().first;
		int dt = q.front().second;
		dist[cur.first][cur.second]=dt;
		q.pop();
		if(vis[cur.first][cur.second])continue;
		vis[cur.first][cur.second]=1;
		if(g[cur.first][cur.second]=='D'){
			return 1;
		}

		for(auto ele :mv){
			int xx = cur.first+ele.first;
			int yy = cur.second+ele.second;

			if(val(xx,yy) && !vis[xx][yy] && tme(dt+1 , beetime[xx][yy]-x) && g[xx][yy]!='H'){
				q.push({{xx,yy} , dt+1});
			}
		}

	}
	// for(int i=0;i<n;i++){
	// 	for(int j=0;j<n;j++){
	// 		cout<<vis[i][j]<<" ";
	// 	}
	// 	cout<<endl;
	// }

	for(auto ele : mv){
		int xx = xcc+ele.first;
		int yy = ycc+ele.second;

		if(val(xx,yy) && vis[xx][yy]==1 && tme(dist[xx][yy] ,beetime[xx][yy]-x)){
		// cout<<xx<<" "<<yy<<endl;
			return 1;
		}
	}

	return 0;
}

void solu(){
	cin>>n>>st;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>g[i][j];
			g1[i][j]=g[i][j];
			if(g[i][j]=='M'){
				xc=i;
				yc=j;
			}
			if(g[i][j]=='D'){
				xcc = i;
				ycc = j;
			}
		}
	}
	// cout<<endl;
	int l=0;
	int r = n*n;
	int ans=0;
	while(l<r){
		int mid = l + (r-l+1)/2;
		// cout<<mid<<endl;
		if(check(mid)){
			ans=max(mid,ans);
			l=mid;
		}else{
			r=mid-1;
		}
	}
	if(!check(0)){
		cout<<"-1"<<endl;
		return;
	}
	// cout<<check(2)<<endl;
	// if(ans==0){
	// 	cout<<"-1"<<endl;
	// 	return;
	// }
	cout<<ans<<endl;


}
int main(){
		// ios_base::sync_with_stdio(0);
		// cin.tie(0);
		// int cass;
		// cin>>cass; 
		// for(int i=0;i<cass;i++)
		solu();
    return 0;
}

Compilation message

mecho.cpp: In function 'bool tme(int, int)':
mecho.cpp:74:6: warning: unused variable 'ccur' [-Wunused-variable]
   74 |  int ccur = marc/st;
      |      ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2472 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2652 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 621 ms 8836 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 4536 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 3 ms 4700 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 4952 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 2 ms 4700 KB Output is correct
28 Correct 2 ms 4700 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 2 ms 4700 KB Output is correct
31 Correct 2 ms 4696 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 31 ms 5296 KB Output is correct
34 Correct 29 ms 5212 KB Output is correct
35 Correct 74 ms 5212 KB Output is correct
36 Correct 40 ms 5468 KB Output is correct
37 Correct 47 ms 5464 KB Output is correct
38 Correct 98 ms 5492 KB Output is correct
39 Correct 51 ms 5676 KB Output is correct
40 Correct 50 ms 5684 KB Output is correct
41 Correct 136 ms 5720 KB Output is correct
42 Correct 65 ms 5916 KB Output is correct
43 Correct 72 ms 5724 KB Output is correct
44 Correct 163 ms 5960 KB Output is correct
45 Correct 93 ms 5980 KB Output is correct
46 Correct 81 ms 5980 KB Output is correct
47 Correct 192 ms 6184 KB Output is correct
48 Correct 92 ms 6232 KB Output is correct
49 Correct 88 ms 6448 KB Output is correct
50 Correct 237 ms 6488 KB Output is correct
51 Correct 121 ms 6756 KB Output is correct
52 Correct 109 ms 6744 KB Output is correct
53 Correct 290 ms 6736 KB Output is correct
54 Correct 157 ms 7336 KB Output is correct
55 Correct 125 ms 7128 KB Output is correct
56 Correct 325 ms 7164 KB Output is correct
57 Correct 151 ms 7516 KB Output is correct
58 Correct 145 ms 7512 KB Output is correct
59 Correct 386 ms 7556 KB Output is correct
60 Correct 185 ms 8016 KB Output is correct
61 Correct 174 ms 7932 KB Output is correct
62 Correct 449 ms 7972 KB Output is correct
63 Correct 445 ms 7764 KB Output is correct
64 Correct 545 ms 8120 KB Output is correct
65 Correct 483 ms 7944 KB Output is correct
66 Correct 457 ms 7952 KB Output is correct
67 Correct 464 ms 8020 KB Output is correct
68 Correct 444 ms 8028 KB Output is correct
69 Correct 464 ms 8120 KB Output is correct
70 Correct 466 ms 8272 KB Output is correct
71 Correct 471 ms 7860 KB Output is correct
72 Correct 457 ms 7912 KB Output is correct
73 Correct 598 ms 9476 KB Output is correct
74 Correct 678 ms 9700 KB Output is correct
75 Correct 669 ms 9720 KB Output is correct
76 Correct 685 ms 9828 KB Output is correct
77 Correct 687 ms 9896 KB Output is correct
78 Correct 603 ms 9416 KB Output is correct
79 Correct 623 ms 9408 KB Output is correct
80 Correct 615 ms 9628 KB Output is correct
81 Correct 630 ms 9556 KB Output is correct
82 Correct 603 ms 9444 KB Output is correct
83 Correct 604 ms 9156 KB Output is correct
84 Correct 653 ms 9208 KB Output is correct
85 Correct 652 ms 9420 KB Output is correct
86 Correct 643 ms 9204 KB Output is correct
87 Correct 624 ms 9416 KB Output is correct
88 Correct 637 ms 8928 KB Output is correct
89 Correct 671 ms 8912 KB Output is correct
90 Correct 679 ms 8896 KB Output is correct
91 Correct 631 ms 9024 KB Output is correct
92 Correct 630 ms 8904 KB Output is correct