답안 #212401

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212401 2020-03-22T21:39:29 Z mode149256 Mecho (IOI09_mecho) C++14
68 / 100
225 ms 6500 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);

	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);
				}
			}
		}
	}

	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 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 135 ms 6200 KB Output is correct
8 Incorrect 5 ms 384 KB Output isn't correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Incorrect 5 ms 384 KB Output isn't correct
13 Incorrect 5 ms 384 KB Output isn't correct
14 Incorrect 5 ms 384 KB Output isn't correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 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 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 14 ms 1504 KB Output is correct
34 Correct 13 ms 1504 KB Output is correct
35 Correct 30 ms 1500 KB Output is correct
36 Correct 15 ms 1920 KB Output is correct
37 Correct 15 ms 1792 KB Output is correct
38 Correct 42 ms 1924 KB Output is correct
39 Correct 18 ms 2216 KB Output is correct
40 Correct 18 ms 2216 KB Output is correct
41 Correct 47 ms 2220 KB Output is correct
42 Correct 22 ms 2648 KB Output is correct
43 Correct 21 ms 2656 KB Output is correct
44 Correct 81 ms 2752 KB Output is correct
45 Correct 25 ms 3104 KB Output is correct
46 Correct 25 ms 3104 KB Output is correct
47 Correct 83 ms 3208 KB Output is correct
48 Correct 29 ms 3728 KB Output is correct
49 Correct 30 ms 3648 KB Output is correct
50 Correct 111 ms 3844 KB Output is correct
51 Correct 35 ms 4272 KB Output is correct
52 Correct 35 ms 4184 KB Output is correct
53 Correct 125 ms 4176 KB Output is correct
54 Correct 38 ms 4880 KB Output is correct
55 Correct 39 ms 4872 KB Output is correct
56 Correct 139 ms 4976 KB Output is correct
57 Correct 46 ms 5508 KB Output is correct
58 Correct 46 ms 5504 KB Output is correct
59 Correct 142 ms 5476 KB Output is correct
60 Correct 54 ms 6184 KB Output is correct
61 Correct 55 ms 6440 KB Output is correct
62 Correct 174 ms 6232 KB Output is correct
63 Correct 131 ms 6184 KB Output is correct
64 Correct 225 ms 6240 KB Output is correct
65 Correct 221 ms 6232 KB Output is correct
66 Correct 159 ms 6240 KB Output is correct
67 Incorrect 154 ms 6232 KB Output isn't correct
68 Correct 86 ms 6324 KB Output is correct
69 Correct 83 ms 6212 KB Output is correct
70 Correct 76 ms 6216 KB Output is correct
71 Correct 65 ms 6336 KB Output is correct
72 Incorrect 64 ms 6216 KB Output isn't correct
73 Incorrect 82 ms 6500 KB Output isn't correct
74 Correct 101 ms 6224 KB Output is correct
75 Correct 120 ms 6224 KB Output is correct
76 Correct 115 ms 6224 KB Output is correct
77 Correct 115 ms 6344 KB Output is correct
78 Incorrect 117 ms 6272 KB Output isn't correct
79 Correct 92 ms 6212 KB Output is correct
80 Correct 103 ms 6260 KB Output is correct
81 Correct 119 ms 6312 KB Output is correct
82 Correct 97 ms 6224 KB Output is correct
83 Correct 135 ms 6216 KB Output is correct
84 Correct 132 ms 6220 KB Output is correct
85 Correct 114 ms 6208 KB Output is correct
86 Correct 126 ms 6224 KB Output is correct
87 Correct 122 ms 6208 KB Output is correct
88 Correct 126 ms 6200 KB Output is correct
89 Correct 136 ms 6200 KB Output is correct
90 Correct 145 ms 6300 KB Output is correct
91 Correct 130 ms 6188 KB Output is correct
92 Correct 131 ms 6200 KB Output is correct