답안 #533132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
533132 2022-03-04T22:57:23 Z Bungmint Mecho (IOI09_mecho) C++17
38 / 100
160 ms 4444 KB
//Copyright © 2022 Youngmin Park. All rights reserved.
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;

#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define REP(a) F0R(_, a)

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
inline bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out()
{
    cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif

inline namespace RecursiveLambda{
	template <typename Fun>
	struct y_combinator_result{
		Fun fun_;
		template <typename T> 
		explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)){}
		template <typename...Args>
		decltype(auto) operator()(Args &&...args){
			return fun_(ref(*this), forward<Args>(args)...);
		}
	};
	template <typename Fun>
	decltype(auto) y_combinator(Fun &&fun){
		return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
	}
};

void setIO(string s) // USACO
{
	#ifndef LOCAL
	    freopen((s + ".in").c_str(), "r", stdin);
	    freopen((s + ".out").c_str(), "w", stdout);
	#endif
}

bool vis[800][800];

void solve()
{
	int n, S;
	cin >> n >> S;
	vector<string> grid(n);
	for (auto &s : grid) cin >> s;
	pii st, en{};
	vector<vi> distHive(n, vi(n, INF));
	queue<pii> bee;
	F0R(i, n) F0R(j, n) {
		if (grid[i][j] == 'M') {
			st = {i, j};
		}
		if (grid[i][j] == 'H') {
			distHive[i][j] = 0;
			bee.push({i, j});
		}
		if (grid[i][j] == 'D') {
			en = {i, j};
		}
	}
	while (sz(bee)) {
		auto [r, c] = bee.front();
		bee.pop();
		F0R(i, 4) {
			int nr = r + dx[i], nc = c + dy[i];
			if (nr < 0 || nc < 0 || nr >= n || nc >= n) continue;
			if (grid[nr][nc] == 'T' || grid[nr][nc] == 'D') continue;
			if (ckmin(distHive[nr][nc], distHive[r][c] + 1)) {
				bee.push({nr, nc});
			}
		}
	}
	int l = 0, r = n * n, ans = -1;
	while (l <= r) {
		int mid = (l + r) / 2;
		int tt = mid;
		queue<pair<int, pii>> q, nxt;
		memset(vis, 0, sizeof(bool) * 800 * 800);
		q.push({0, st});
		vis[st.fi][st.se] = 1;
		while (sz(q)) {
			while (sz(q)) {
				auto [x, y] = q.front().se;
				auto d = q.front().fi;
				q.pop();
				if (d == S) {
					nxt.push({0, {x, y}});
					continue;
				}
				F0R(i, 4) {
					int nx = x + dx[i], ny = y + dy[i];
					if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
					if (grid[nx][ny] == 'T') continue;
					if (distHive[nx][ny] <= tt) continue;
					if (!vis[nx][ny]) {
						vis[nx][ny] = 1;
						q.push({d + 1, {nx, ny}});
					}
				}
			}
			while (sz(nxt)) q.push({0, nxt.front().se}), nxt.pop();
			tt++;
		}
		if (vis[en.fi][en.se]) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	cout << ans;
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    int testcase=1;
    // cin >> testcase;
    while (testcase--)
    {
        solve();
    }
}

Compilation message

mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:94:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |      freopen((s + ".in").c_str(), "r", stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |      freopen((s + ".out").c_str(), "w", stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 844 KB Output isn't correct
2 Incorrect 1 ms 896 KB Output isn't correct
3 Incorrect 1 ms 844 KB Output isn't correct
4 Incorrect 1 ms 844 KB Output isn't correct
5 Correct 1 ms 844 KB Output is correct
6 Correct 1 ms 844 KB Output is correct
7 Correct 65 ms 4272 KB Output is correct
8 Incorrect 1 ms 844 KB Output isn't correct
9 Correct 1 ms 844 KB Output is correct
10 Correct 1 ms 844 KB Output is correct
11 Correct 1 ms 844 KB Output is correct
12 Incorrect 1 ms 844 KB Output isn't correct
13 Incorrect 1 ms 972 KB Output isn't correct
14 Correct 1 ms 972 KB Output is correct
15 Incorrect 1 ms 844 KB Output isn't correct
16 Correct 1 ms 844 KB Output is correct
17 Incorrect 1 ms 844 KB Output isn't correct
18 Correct 1 ms 844 KB Output is correct
19 Incorrect 1 ms 844 KB Output isn't correct
20 Correct 1 ms 844 KB Output is correct
21 Incorrect 1 ms 844 KB Output isn't correct
22 Correct 1 ms 844 KB Output is correct
23 Incorrect 1 ms 844 KB Output isn't correct
24 Correct 1 ms 844 KB Output is correct
25 Incorrect 1 ms 972 KB Output isn't correct
26 Correct 1 ms 972 KB Output is correct
27 Incorrect 1 ms 972 KB Output isn't correct
28 Correct 1 ms 972 KB Output is correct
29 Incorrect 1 ms 972 KB Output isn't correct
30 Correct 1 ms 972 KB Output is correct
31 Incorrect 1 ms 972 KB Output isn't correct
32 Correct 1 ms 972 KB Output is correct
33 Incorrect 4 ms 1484 KB Output isn't correct
34 Correct 4 ms 1484 KB Output is correct
35 Correct 17 ms 1584 KB Output is correct
36 Incorrect 5 ms 1740 KB Output isn't correct
37 Correct 5 ms 1740 KB Output is correct
38 Correct 23 ms 1740 KB Output is correct
39 Incorrect 7 ms 1868 KB Output isn't correct
40 Correct 7 ms 2044 KB Output is correct
41 Correct 32 ms 2000 KB Output is correct
42 Incorrect 8 ms 2124 KB Output isn't correct
43 Correct 6 ms 2124 KB Output is correct
44 Correct 37 ms 2124 KB Output is correct
45 Incorrect 9 ms 2484 KB Output isn't correct
46 Correct 8 ms 2380 KB Output is correct
47 Correct 41 ms 2380 KB Output is correct
48 Incorrect 10 ms 2764 KB Output isn't correct
49 Correct 9 ms 2764 KB Output is correct
50 Correct 48 ms 2764 KB Output is correct
51 Incorrect 12 ms 3020 KB Output isn't correct
52 Correct 10 ms 2984 KB Output is correct
53 Correct 92 ms 3092 KB Output is correct
54 Incorrect 14 ms 3404 KB Output isn't correct
55 Correct 11 ms 3404 KB Output is correct
56 Correct 73 ms 3404 KB Output is correct
57 Incorrect 16 ms 3692 KB Output isn't correct
58 Correct 13 ms 3660 KB Output is correct
59 Correct 86 ms 3788 KB Output is correct
60 Incorrect 17 ms 4172 KB Output isn't correct
61 Correct 16 ms 4072 KB Output is correct
62 Correct 101 ms 4172 KB Output is correct
63 Correct 100 ms 4076 KB Output is correct
64 Correct 160 ms 4072 KB Output is correct
65 Correct 133 ms 4176 KB Output is correct
66 Incorrect 120 ms 4168 KB Output isn't correct
67 Correct 123 ms 4160 KB Output is correct
68 Correct 37 ms 4172 KB Output is correct
69 Correct 35 ms 4204 KB Output is correct
70 Correct 27 ms 4204 KB Output is correct
71 Correct 33 ms 4204 KB Output is correct
72 Incorrect 22 ms 4100 KB Output isn't correct
73 Incorrect 30 ms 4428 KB Output isn't correct
74 Correct 41 ms 4440 KB Output is correct
75 Correct 57 ms 4352 KB Output is correct
76 Correct 56 ms 4444 KB Output is correct
77 Correct 47 ms 4436 KB Output is correct
78 Correct 64 ms 4300 KB Output is correct
79 Correct 36 ms 4428 KB Output is correct
80 Correct 49 ms 4328 KB Output is correct
81 Correct 56 ms 4328 KB Output is correct
82 Correct 47 ms 4300 KB Output is correct
83 Correct 71 ms 4328 KB Output is correct
84 Correct 53 ms 4372 KB Output is correct
85 Correct 60 ms 4292 KB Output is correct
86 Correct 59 ms 4288 KB Output is correct
87 Correct 58 ms 4372 KB Output is correct
88 Correct 63 ms 4236 KB Output is correct
89 Correct 62 ms 4320 KB Output is correct
90 Correct 65 ms 4324 KB Output is correct
91 Correct 70 ms 4328 KB Output is correct
92 Correct 70 ms 4328 KB Output is correct