Submission #100602

# Submission time Handle Problem Language Result Execution time Memory
100602 2019-03-12T18:38:31 Z luciocf Mecho (IOI09_mecho) C++14
100 / 100
218 ms 5112 KB
// IOI 2009 - Mecho
// Lúcio Cardoso

#include <bits/stdc++.h>

using namespace std;

const int maxn = 810;

typedef pair<int, int> pii;

int n, s, ox, oy, dx, dy;
int dist[maxn][maxn];

char tab[maxn][maxn];

bool mark[maxn][maxn];

int linha[] = {-1, 1, 0, 0};
int coluna[] = {0, 0, -1, 1};

void bfs(void)
{
	queue<pii> fila;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (tab[i][j] == 'H')
				dist[i][j] = 0, fila.push({i, j});

	dist[dx][dy] = 1e9+10;

	while (!fila.empty())
	{
		int x = fila.front().first, y = fila.front().second;
		fila.pop();

		for (int i = 0; i < 4; i++)
		{
			int a = x+linha[i], b = y+coluna[i];
			if (a < 1 || a > n || b < 1 || b > n || tab[a][b] == 'T' || tab[a][b] == 'D' || dist[a][b] != -1) continue;

			dist[a][b] = dist[x][y] + s;
			fila.push({a, b});
		}
	}
}

bool ok(int t)
{
	if (t*s >= dist[ox][oy]) return false;

	queue<pair<int, pii> > fila;

	memset(mark, 0, sizeof mark);

	mark[ox][oy] = 1;
	fila.push({t*s, {ox, oy}});

	while (!fila.empty())
	{
		int d = fila.front().first;
		int x = fila.front().second.first, y = fila.front().second.second;
		fila.pop();

		for (int i = 0; i < 4; i++)
		{
			int a = x+linha[i], b = y+coluna[i];
			if (a < 1 || a > n || b < 1 || b > n || tab[a][b] == 'T' || d+1 >= dist[a][b] || mark[a][b]) continue;

			if (tab[a][b] == 'D') return true;

			mark[a][b] = 1;
			fila.push({d+1, {a, b}});
		}
	}

	return false;
}

int busca(void)
{
	int ini = 0, fim = 640000*2, ans = -1;

	while (ini <= fim)
	{
		int mid = (ini+fim)>>1;

		if (ok(mid)) ini = mid+1, ans = mid;
		else fim = mid-1;
	}

	return ans;
}

int main(void)
{
	cin >> n >> s;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> tab[i][j];

			if (tab[i][j] == 'M') ox = i, oy = j;
			if (tab[i][j] == 'D') dx = i, dy = j;
		}
	}

	memset(dist, -1, sizeof dist);
	bfs();

	cout << busca() << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3712 KB Output is correct
2 Correct 5 ms 3584 KB Output is correct
3 Correct 5 ms 3584 KB Output is correct
4 Correct 6 ms 3712 KB Output is correct
5 Correct 5 ms 3584 KB Output is correct
6 Correct 5 ms 3584 KB Output is correct
7 Correct 147 ms 4856 KB Output is correct
8 Correct 4 ms 3572 KB Output is correct
9 Correct 4 ms 3584 KB Output is correct
10 Correct 5 ms 3584 KB Output is correct
11 Correct 5 ms 3584 KB Output is correct
12 Correct 5 ms 3584 KB Output is correct
13 Correct 5 ms 3584 KB Output is correct
14 Correct 5 ms 3584 KB Output is correct
15 Correct 7 ms 3584 KB Output is correct
16 Correct 5 ms 3584 KB Output is correct
17 Correct 5 ms 3584 KB Output is correct
18 Correct 5 ms 3584 KB Output is correct
19 Correct 6 ms 3584 KB Output is correct
20 Correct 5 ms 3584 KB Output is correct
21 Correct 5 ms 3584 KB Output is correct
22 Correct 5 ms 3584 KB Output is correct
23 Correct 5 ms 3584 KB Output is correct
24 Correct 6 ms 3584 KB Output is correct
25 Correct 7 ms 3584 KB Output is correct
26 Correct 7 ms 3584 KB Output is correct
27 Correct 5 ms 3584 KB Output is correct
28 Correct 5 ms 3584 KB Output is correct
29 Correct 5 ms 3584 KB Output is correct
30 Correct 5 ms 3584 KB Output is correct
31 Correct 5 ms 3584 KB Output is correct
32 Correct 5 ms 3584 KB Output is correct
33 Correct 13 ms 4004 KB Output is correct
34 Correct 15 ms 3968 KB Output is correct
35 Correct 41 ms 3968 KB Output is correct
36 Correct 16 ms 3968 KB Output is correct
37 Correct 16 ms 4096 KB Output is correct
38 Correct 42 ms 4088 KB Output is correct
39 Correct 18 ms 4088 KB Output is correct
40 Correct 19 ms 4096 KB Output is correct
41 Correct 49 ms 4088 KB Output is correct
42 Correct 23 ms 4220 KB Output is correct
43 Correct 28 ms 4216 KB Output is correct
44 Correct 65 ms 4344 KB Output is correct
45 Correct 25 ms 4224 KB Output is correct
46 Correct 26 ms 4344 KB Output is correct
47 Correct 78 ms 4352 KB Output is correct
48 Correct 29 ms 4344 KB Output is correct
49 Correct 29 ms 4392 KB Output is correct
50 Correct 99 ms 4344 KB Output is correct
51 Correct 34 ms 4508 KB Output is correct
52 Correct 31 ms 4472 KB Output is correct
53 Correct 97 ms 4600 KB Output is correct
54 Correct 36 ms 4572 KB Output is correct
55 Correct 38 ms 4600 KB Output is correct
56 Correct 108 ms 4664 KB Output is correct
57 Correct 42 ms 4856 KB Output is correct
58 Correct 42 ms 4728 KB Output is correct
59 Correct 149 ms 4728 KB Output is correct
60 Correct 49 ms 4884 KB Output is correct
61 Correct 47 ms 4856 KB Output is correct
62 Correct 201 ms 4852 KB Output is correct
63 Correct 148 ms 4984 KB Output is correct
64 Correct 218 ms 4856 KB Output is correct
65 Correct 165 ms 4856 KB Output is correct
66 Correct 139 ms 4856 KB Output is correct
67 Correct 144 ms 4884 KB Output is correct
68 Correct 73 ms 4856 KB Output is correct
69 Correct 74 ms 4856 KB Output is correct
70 Correct 61 ms 4856 KB Output is correct
71 Correct 68 ms 4984 KB Output is correct
72 Correct 58 ms 4856 KB Output is correct
73 Correct 61 ms 4976 KB Output is correct
74 Correct 81 ms 4960 KB Output is correct
75 Correct 103 ms 4960 KB Output is correct
76 Correct 83 ms 4960 KB Output is correct
77 Correct 85 ms 4884 KB Output is correct
78 Correct 99 ms 4860 KB Output is correct
79 Correct 80 ms 5000 KB Output is correct
80 Correct 81 ms 4860 KB Output is correct
81 Correct 112 ms 4988 KB Output is correct
82 Correct 103 ms 5112 KB Output is correct
83 Correct 127 ms 4896 KB Output is correct
84 Correct 110 ms 4984 KB Output is correct
85 Correct 110 ms 5024 KB Output is correct
86 Correct 118 ms 4896 KB Output is correct
87 Correct 113 ms 4904 KB Output is correct
88 Correct 119 ms 4948 KB Output is correct
89 Correct 175 ms 5072 KB Output is correct
90 Correct 139 ms 4992 KB Output is correct
91 Correct 115 ms 5032 KB Output is correct
92 Correct 118 ms 5020 KB Output is correct