답안 #212403

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212403 2020-03-22T21:43:36 Z mode149256 Mecho (IOI09_mecho) C++14
100 / 100
249 ms 6400 KB
/*input
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;

#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"

const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;

template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }

template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}

using vc = vector<char>;
int N, S;
vector<vc> sk;

vii hiveDis;

int dx[] = {0, 1, 0, -1};
int dy[] = { -1, 0, 1, 0};
pi dxy[] = {
	{dx[0], dy[0]},
	{dx[1], dy[1]},
	{dx[2], dy[2]},
	{dx[3], dy[3]}
};
pi mecho;
pi home;
vpi hives;

bool valid(pi a) {
	return a.x >= 0 and a.y >= 0 and a.x < N and a.y < N;
}

bool gali(int time) {
	vii dis(N, vi(N, MOD));

	dis[mecho.x][mecho.y] = 0;
	queue<pi> q;
	q.push(mecho);

	if (time >= hiveDis[mecho.x][mecho.y]) return false;

	while (q.size()) {
		pi c = q.front(); q.pop();

		for (int i = 0; i < 4; ++i)
		{
			pi n = c + dxy[i];
			if (valid(n) and sk[n.x][n.y] != 'T') {
				if (sk[n.x][n.y] == 'D')
					return true;

				int newDis = dis[c.x][c.y] + 1;

				if (time + newDis / S < hiveDis[n.x][n.y] and newDis < dis[n.x][n.y]) {
					dis[n.x][n.y] = newDis;
					q.push(n);
				}
			}
		}
	}

	return false;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N >> S;
	sk = vector<vc>(N, vc(N));
	hiveDis = vii(N, vi(N, MOD));


	for (int i = 0; i < N; ++i)
	{
		for (int j = 0; j < N; ++j)
		{
			cin >> sk[i][j];
			if (sk[i][j] == 'M') mecho = {i, j};
			else if (sk[i][j] == 'D') home = {i, j};
			else if (sk[i][j] == 'H') hives.emplace_back(i, j);
		}
	}

	queue<pi> q;
	for (auto u : hives) {
		hiveDis[u.x][u.y] = 0;
		q.push(u);
	}

	while (q.size()) {
		pi c = q.front(); q.pop();

		for (int i = 0; i < 4; ++i)
		{
			pi nc = c + dxy[i];
			if (valid(nc) and sk[nc.x][nc.y] != 'T' and nc != home) {
				if (hiveDis[c.x][c.y] + 1 < hiveDis[nc.x][nc.y]) {
					hiveDis[nc.x][nc.y] = hiveDis[c.x][c.y] + 1;
					q.push(nc);
				}
			}
		}
	}

	if (!gali(0)) {
		return !printf("-1\n");
	}
	int l = 0;
	int h = MOD;
	int m;

	while (l < h) {
		m = (l + h + 1) / 2;
		if (gali(m))
			l = m;
		else
			h = m - 1;
	}
	printf("%d\n", l);
}

/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 173 ms 6344 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 7 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 13 ms 1532 KB Output is correct
34 Correct 13 ms 1500 KB Output is correct
35 Correct 32 ms 1600 KB Output is correct
36 Correct 17 ms 1792 KB Output is correct
37 Correct 17 ms 1792 KB Output is correct
38 Correct 42 ms 1916 KB Output is correct
39 Correct 18 ms 2216 KB Output is correct
40 Correct 19 ms 2248 KB Output is correct
41 Correct 51 ms 2220 KB Output is correct
42 Correct 22 ms 2776 KB Output is correct
43 Correct 23 ms 2768 KB Output is correct
44 Correct 69 ms 2680 KB Output is correct
45 Correct 25 ms 3104 KB Output is correct
46 Correct 26 ms 3104 KB Output is correct
47 Correct 81 ms 3104 KB Output is correct
48 Correct 39 ms 3728 KB Output is correct
49 Correct 29 ms 3728 KB Output is correct
50 Correct 99 ms 3632 KB Output is correct
51 Correct 34 ms 4272 KB Output is correct
52 Correct 34 ms 4176 KB Output is correct
53 Correct 117 ms 4180 KB Output is correct
54 Correct 44 ms 5056 KB Output is correct
55 Correct 39 ms 4876 KB Output is correct
56 Correct 141 ms 4856 KB Output is correct
57 Correct 44 ms 5484 KB Output is correct
58 Correct 43 ms 5512 KB Output is correct
59 Correct 161 ms 5488 KB Output is correct
60 Correct 52 ms 6320 KB Output is correct
61 Correct 49 ms 6216 KB Output is correct
62 Correct 187 ms 6184 KB Output is correct
63 Correct 149 ms 6184 KB Output is correct
64 Correct 249 ms 6220 KB Output is correct
65 Correct 226 ms 6240 KB Output is correct
66 Correct 176 ms 6184 KB Output is correct
67 Correct 43 ms 6016 KB Output is correct
68 Correct 82 ms 6208 KB Output is correct
69 Correct 90 ms 6296 KB Output is correct
70 Correct 84 ms 6204 KB Output is correct
71 Correct 76 ms 6252 KB Output is correct
72 Correct 65 ms 6212 KB Output is correct
73 Correct 78 ms 6228 KB Output is correct
74 Correct 119 ms 6304 KB Output is correct
75 Correct 125 ms 6224 KB Output is correct
76 Correct 112 ms 6232 KB Output is correct
77 Correct 134 ms 6400 KB Output is correct
78 Correct 43 ms 6136 KB Output is correct
79 Correct 106 ms 6236 KB Output is correct
80 Correct 112 ms 6280 KB Output is correct
81 Correct 120 ms 6204 KB Output is correct
82 Correct 113 ms 6356 KB Output is correct
83 Correct 125 ms 6216 KB Output is correct
84 Correct 135 ms 6252 KB Output is correct
85 Correct 126 ms 6208 KB Output is correct
86 Correct 143 ms 6352 KB Output is correct
87 Correct 132 ms 6208 KB Output is correct
88 Correct 133 ms 6296 KB Output is correct
89 Correct 148 ms 6200 KB Output is correct
90 Correct 156 ms 6272 KB Output is correct
91 Correct 157 ms 6188 KB Output is correct
92 Correct 143 ms 6200 KB Output is correct