Submission #522407

# Submission time Handle Problem Language Result Execution time Memory
522407 2022-02-04T22:50:44 Z AndresTL Mecho (IOI09_mecho) C++11
100 / 100
210 ms 6856 KB
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

int n, s;
int x, y, x2, y2;
char mapa[802][802];

int TB[802][802], TM[802][802];
int mov[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };

bool valideBee( int r, int c ){
	if( !r || !c || r > n || c > n )
		return false;
	if( TB[r][c] != -1 )
		return false;
	if( mapa[r][c] == 'T' || mapa[r][c] == 'D' )
		return false;
	return true;
}

void BeeFS(){
	
	queue< pair< int, int > > Q;
	pair< int, int > act;
	int r, c;
	
	for( int i = 1; i <= n; i++ )
		for( int j = 1; j <= n; j++ ){
			if( mapa[i][j] == 'H' )
				Q.push( make_pair( i, j ) );
			else
				TB[i][j] = -1;
		}
	
	while( !Q.empty() ){
		act = Q.front();
		Q.pop();
		for( int i = 0; i < 4; i++ ){
			r = act.first + mov[i][0]; c = act.second + mov[i][1];
			if( valideBee( r, c ) ){
				TB[r][c] = TB[act.first][act.second] + s;
				Q.push( make_pair( r, c ) );
			}
		}
	}
	
}

bool valideMecho( int r, int c, int t ){
	
	if( !r || !c || r > n || c > n )
		return false;
	if( TM[r][c] != -1 )
		return false;
	if( mapa[r][c] == 'T' )
		return false;
	if( mapa[r][c] == 'D' )
		return true;
	if( t >= TB[r][c] )
		return false;
	return true;
}

bool possible( int wait ){
	
	queue< pair< int, int > > Q;
	pair< int, int > act;
	int r, c, t;
	
	for( int i = 1; i <= n; i++ )
		for( int j = 1; j <= n; j++ )
			TM[i][j] = -1;
	TM[x][y] = wait * s;
	if( TB[x][y] <= TM[x][y] )
		return false;
	Q.push( make_pair( x, y ) );
	
	while( !Q.empty() ){
		act = Q.front();
		Q.pop();
		t = TM[act.first][act.second] + 1;
		for( int i = 0; i < 4; i++ ){
			r = act.first + mov[i][0]; c = act.second + mov[i][1];
			if( valideMecho( r, c, t ) ){
				TM[r][c] = t;
				Q.push( make_pair( r, c ) );
			}
		}
	}

	return TM[x2][y2] != -1;
	
}

int main(){

	cin >> n >> s;
	for( int i = 1; i <= n; i++ )
		for( int j = 1; j <= n; j++ ){
			cin >> mapa[i][j];
			if( mapa[i][j] == 'M' ){
				x = i; y = j;
			}
			if( mapa[i][j] == 'D' ){
				x2 = i; y2 = j;
			}
		}
	BeeFS();
	if( !possible( 0 ) ){
		cout << "-1\n";
		return 0;
	}		
	
	int b = 0, e = 640000, m;
	while( b != e ){
		m = (b + e) / 2 + 1;
		if( possible( m ) )
			b = m;
		else
			e = m - 1;
	}
	cout << b << "\n";
	return 0;	
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 296 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 121 ms 6676 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
13 Correct 1 ms 560 KB Output is correct
14 Correct 1 ms 688 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 688 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 1 ms 844 KB Output is correct
32 Correct 1 ms 844 KB Output is correct
33 Correct 11 ms 2892 KB Output is correct
34 Correct 12 ms 2876 KB Output is correct
35 Correct 37 ms 2892 KB Output is correct
36 Correct 16 ms 3268 KB Output is correct
37 Correct 17 ms 3256 KB Output is correct
38 Correct 48 ms 3288 KB Output is correct
39 Correct 17 ms 3628 KB Output is correct
40 Correct 19 ms 3656 KB Output is correct
41 Correct 49 ms 3592 KB Output is correct
42 Correct 23 ms 4044 KB Output is correct
43 Correct 25 ms 3972 KB Output is correct
44 Correct 60 ms 4084 KB Output is correct
45 Correct 27 ms 4420 KB Output is correct
46 Correct 36 ms 4468 KB Output is correct
47 Correct 67 ms 4480 KB Output is correct
48 Correct 31 ms 4772 KB Output is correct
49 Correct 39 ms 4800 KB Output is correct
50 Correct 83 ms 4824 KB Output is correct
51 Correct 35 ms 5312 KB Output is correct
52 Correct 37 ms 5312 KB Output is correct
53 Correct 100 ms 5312 KB Output is correct
54 Correct 43 ms 5692 KB Output is correct
55 Correct 46 ms 5648 KB Output is correct
56 Correct 115 ms 5720 KB Output is correct
57 Correct 47 ms 6140 KB Output is correct
58 Correct 52 ms 6136 KB Output is correct
59 Correct 140 ms 6148 KB Output is correct
60 Correct 56 ms 6488 KB Output is correct
61 Correct 60 ms 6556 KB Output is correct
62 Correct 151 ms 6564 KB Output is correct
63 Correct 146 ms 6484 KB Output is correct
64 Correct 210 ms 6612 KB Output is correct
65 Correct 210 ms 6652 KB Output is correct
66 Correct 159 ms 6468 KB Output is correct
67 Correct 61 ms 6536 KB Output is correct
68 Correct 84 ms 6508 KB Output is correct
69 Correct 75 ms 6580 KB Output is correct
70 Correct 65 ms 6508 KB Output is correct
71 Correct 64 ms 6596 KB Output is correct
72 Correct 62 ms 6576 KB Output is correct
73 Correct 63 ms 6724 KB Output is correct
74 Correct 93 ms 6848 KB Output is correct
75 Correct 98 ms 6856 KB Output is correct
76 Correct 86 ms 6764 KB Output is correct
77 Correct 92 ms 6844 KB Output is correct
78 Correct 62 ms 6720 KB Output is correct
79 Correct 97 ms 6736 KB Output is correct
80 Correct 94 ms 6844 KB Output is correct
81 Correct 92 ms 6808 KB Output is correct
82 Correct 91 ms 6736 KB Output is correct
83 Correct 106 ms 6708 KB Output is correct
84 Correct 113 ms 6824 KB Output is correct
85 Correct 106 ms 6744 KB Output is correct
86 Correct 104 ms 6780 KB Output is correct
87 Correct 100 ms 6704 KB Output is correct
88 Correct 108 ms 6724 KB Output is correct
89 Correct 118 ms 6724 KB Output is correct
90 Correct 106 ms 6724 KB Output is correct
91 Correct 113 ms 6648 KB Output is correct
92 Correct 108 ms 6696 KB Output is correct