답안 #113163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
113163 2019-05-24T03:48:05 Z joseacaz Mecho (IOI09_mecho) C++17
100 / 100
342 ms 12992 KB
#include <iostream>
#include <queue>
#define MAXN 805
#define INF (1LL << 40LL)
#define pii pair < lld, lld >
#define mp make_pair

using namespace std;
typedef long long lld;

lld N, S, bees[MAXN][MAXN], answer[MAXN][MAXN], Mx, My, x, y, s, e, mid, ans, ansX, ansY;
bool visited[MAXN][MAXN], val;
char forest[MAXN][MAXN];
pii aux;
queue < pii > Q;
lld dx[] = {1, -1, 0, 0};
lld dy[] = {0, 0, 1, -1};

bool valid ( lld _x, lld _y )
{
	if ( _x < 0 || _x >= N )
		return false;
	if ( _y < 0 || _y >= N )
		return false;
	if ( visited[_x][_y] || forest[_x][_y] == 'T' || forest[_x][_y] == 'D')
		return false;

	return true;
}

bool valid2 ( lld _x, lld _y )
{
	if ( _x < 0 || _x >= N )
		return false;
	if ( _y < 0 || _y >= N )
		return false;

	return true;
}

bool validM ( lld _x, lld _y )
{
	if ( _x < 0 || _x >= N )
		return false;
	if ( _y < 0 || _y >= N )
		return false;
	if ( visited[_x][_y] || forest[_x][_y] == 'T')
		return false;

	return true;
}

bool solve ( lld v )
{
	for ( lld i = 0; i < N; i++ )
		for ( lld j = 0; j < N; j++ )
			answer[i][j] = INF, visited[i][j] = 0;
	
	while ( !Q.empty() )
		Q.pop();

	Q.push ( mp ( Mx, My ) );
	answer[Mx][My] = S * v;

	while ( !Q.empty() )
	{
		aux = Q.front();
		Q.pop();

		x = aux.first;
		y = aux.second;

		if ( x == ansX && y == ansY )
			return true;

		if ( answer[x][y] < bees[x][y] )
		{
			for ( lld i = 0; i < 4; i++ )
			{
				if ( validM ( x + dx[i], y + dy[i] ) )
				{
					Q.push ( mp ( x + dx[i], y + dy[i] ) );
					answer[x + dx[i]][y + dy[i]] = answer[x][y] + 1;
					visited[x + dx[i]][y + dy[i]] = 1;
				}
			}
		}
	}
	return false;
}

int main ()
{
	cin >> N >> S;
	for ( lld i = 0; i < N; i++ )
	{
		for ( lld j = 0; j < N; j++ )
		{
			bees[i][j] = INF;
			cin >> forest[i][j];
			if ( forest[i][j] == 'H' )
			{
				Q.push ( mp ( i, j ) );
				bees[i][j] = 0;
				visited[i][j] = 1;
			}

			if ( forest[i][j] == 'M' )
				Mx = i, My = j;

			if ( forest[i][j] == 'D' )
				ansX = i, ansY = j;
		}
	}

	while ( !Q.empty() )
	{
		aux = Q.front();
		Q.pop();

		x = aux.first;
		y = aux.second;

		for ( lld i = 0; i < 4; i++ )
		{
			if ( valid ( x + dx[i], y + dy[i] ) )
			{
				Q.push ( mp ( x + dx[i], y + dy[i] ) );
				bees[x + dx[i]][y + dy[i]] = bees[x][y] + S;
				visited[x + dx[i]][y + dy[i]] = 1;
			}
		}
	}

	ans = -1;
	s = 0, e = (1<<20);
	while ( s <= e )
	{
		mid = (s + e) / 2;
		val = solve ( mid );

		if ( val )
		{
			ans = mid;
			s = mid + 1;
		}
		else
			e = mid - 1;
	}

	cout << ans << "\n";

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 404 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 249 ms 12664 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 3 ms 896 KB Output is correct
13 Correct 3 ms 768 KB Output is correct
14 Correct 3 ms 896 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 640 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 2 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 2 ms 768 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 3 ms 768 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 2 ms 896 KB Output is correct
26 Correct 3 ms 896 KB Output is correct
27 Correct 3 ms 1024 KB Output is correct
28 Correct 3 ms 1016 KB Output is correct
29 Correct 3 ms 1152 KB Output is correct
30 Correct 3 ms 1024 KB Output is correct
31 Correct 3 ms 1152 KB Output is correct
32 Correct 3 ms 1152 KB Output is correct
33 Correct 18 ms 5376 KB Output is correct
34 Correct 18 ms 5376 KB Output is correct
35 Correct 39 ms 5496 KB Output is correct
36 Correct 21 ms 6144 KB Output is correct
37 Correct 21 ms 6144 KB Output is correct
38 Correct 50 ms 6116 KB Output is correct
39 Correct 27 ms 6912 KB Output is correct
40 Correct 30 ms 6904 KB Output is correct
41 Correct 87 ms 6912 KB Output is correct
42 Correct 34 ms 7672 KB Output is correct
43 Correct 34 ms 7672 KB Output is correct
44 Correct 114 ms 7724 KB Output is correct
45 Correct 40 ms 8372 KB Output is correct
46 Correct 40 ms 8440 KB Output is correct
47 Correct 152 ms 8492 KB Output is correct
48 Correct 45 ms 9208 KB Output is correct
49 Correct 44 ms 9208 KB Output is correct
50 Correct 163 ms 9208 KB Output is correct
51 Correct 50 ms 10004 KB Output is correct
52 Correct 58 ms 9976 KB Output is correct
53 Correct 200 ms 10204 KB Output is correct
54 Correct 60 ms 10872 KB Output is correct
55 Correct 62 ms 10744 KB Output is correct
56 Correct 262 ms 10872 KB Output is correct
57 Correct 77 ms 11644 KB Output is correct
58 Correct 71 ms 11516 KB Output is correct
59 Correct 278 ms 11640 KB Output is correct
60 Correct 71 ms 12280 KB Output is correct
61 Correct 73 ms 12280 KB Output is correct
62 Correct 323 ms 12408 KB Output is correct
63 Correct 228 ms 12408 KB Output is correct
64 Correct 342 ms 12440 KB Output is correct
65 Correct 332 ms 12408 KB Output is correct
66 Correct 256 ms 12280 KB Output is correct
67 Correct 248 ms 12380 KB Output is correct
68 Correct 137 ms 12408 KB Output is correct
69 Correct 132 ms 12392 KB Output is correct
70 Correct 120 ms 12452 KB Output is correct
71 Correct 105 ms 12408 KB Output is correct
72 Correct 100 ms 12408 KB Output is correct
73 Correct 113 ms 12736 KB Output is correct
74 Correct 167 ms 12864 KB Output is correct
75 Correct 183 ms 12808 KB Output is correct
76 Correct 181 ms 12864 KB Output is correct
77 Correct 173 ms 12992 KB Output is correct
78 Correct 206 ms 12912 KB Output is correct
79 Correct 150 ms 12888 KB Output is correct
80 Correct 186 ms 12968 KB Output is correct
81 Correct 195 ms 12916 KB Output is correct
82 Correct 173 ms 12912 KB Output is correct
83 Correct 237 ms 12792 KB Output is correct
84 Correct 204 ms 12920 KB Output is correct
85 Correct 198 ms 12880 KB Output is correct
86 Correct 195 ms 12792 KB Output is correct
87 Correct 195 ms 12892 KB Output is correct
88 Correct 209 ms 12664 KB Output is correct
89 Correct 211 ms 12660 KB Output is correct
90 Correct 214 ms 12664 KB Output is correct
91 Correct 218 ms 12704 KB Output is correct
92 Correct 227 ms 12632 KB Output is correct