답안 #533120

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
533120 2022-03-04T21:44:18 Z Bungmint Mecho (IOI09_mecho) C++17
68 / 100
1000 ms 9584 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>
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
}

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));
	vector<vi> mecho(n, vi(n, INF)), time(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;
		F0R(i, n) fill(all(mecho[i]), INF), fill(all(time[i]), INF);
		mecho[st.fi][st.se] = 0;
		time[st.fi][st.se] = mid;
		queue<pii> q;
		q.push(st);
		while (sz(q)) {
			auto [r, c] = q.front();
			q.pop();
			if (r == 3 && c == 3) dbg(time[r][c], distHive[r][c]);
			if (time[r][c] >= distHive[r][c]) continue;
			queue<pii> s;
			dbg(r, c);
			s.push({r, c});
			while (sz(s)) {
				auto [x, y] = s.front();
				s.pop();
				if (mecho[x][y] == mecho[r][c] + S) {
					q.push({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] <= time[r][c]) continue;
					if (ckmin(mecho[nx][ny], mecho[x][y] + 1)) {
						s.push({nx, ny});
						time[nx][ny] = time[r][c] + 1;
					}
				}
			}
		}
		dbg(mecho);
		if (time[en.fi][en.se] < INF) 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 solve()':
mecho.cpp:71:18: warning: statement has no effect [-Wunused-value]
   71 | #define dbg(...) 42
      |                  ^~
mecho.cpp:144:26: note: in expansion of macro 'dbg'
  144 |    if (r == 3 && c == 3) dbg(time[r][c], distHive[r][c]);
      |                          ^~~
mecho.cpp:71:18: warning: statement has no effect [-Wunused-value]
   71 | #define dbg(...) 42
      |                  ^~
mecho.cpp:147:4: note: in expansion of macro 'dbg'
  147 |    dbg(r, c);
      |    ^~~
mecho.cpp:71:18: warning: statement has no effect [-Wunused-value]
   71 | #define dbg(...) 42
      |                  ^~
mecho.cpp:168:3: note: in expansion of macro 'dbg'
  168 |   dbg(mecho);
      |   ^~~
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 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Execution timed out 1016 ms 9348 KB Time limit exceeded
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 320 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 2 ms 316 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 316 KB Output is correct
31 Correct 1 ms 316 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 9 ms 1988 KB Output is correct
34 Correct 6 ms 2048 KB Output is correct
35 Correct 428 ms 2100 KB Output is correct
36 Correct 15 ms 2500 KB Output is correct
37 Correct 9 ms 2440 KB Output is correct
38 Correct 533 ms 2568 KB Output is correct
39 Correct 17 ms 3140 KB Output is correct
40 Correct 10 ms 3164 KB Output is correct
41 Correct 970 ms 3160 KB Output is correct
42 Correct 16 ms 3804 KB Output is correct
43 Correct 13 ms 3780 KB Output is correct
44 Execution timed out 1077 ms 3780 KB Time limit exceeded
45 Correct 18 ms 4420 KB Output is correct
46 Correct 14 ms 4556 KB Output is correct
47 Execution timed out 1088 ms 4448 KB Time limit exceeded
48 Correct 26 ms 5444 KB Output is correct
49 Correct 17 ms 5320 KB Output is correct
50 Execution timed out 1070 ms 5320 KB Time limit exceeded
51 Correct 26 ms 6220 KB Output is correct
52 Correct 20 ms 6208 KB Output is correct
53 Execution timed out 1090 ms 6220 KB Time limit exceeded
54 Correct 29 ms 7116 KB Output is correct
55 Correct 23 ms 7156 KB Output is correct
56 Execution timed out 1089 ms 7092 KB Time limit exceeded
57 Correct 33 ms 8132 KB Output is correct
58 Correct 26 ms 8140 KB Output is correct
59 Execution timed out 1069 ms 8116 KB Time limit exceeded
60 Correct 40 ms 9160 KB Output is correct
61 Correct 30 ms 9164 KB Output is correct
62 Execution timed out 1046 ms 9156 KB Time limit exceeded
63 Correct 174 ms 9236 KB Output is correct
64 Correct 220 ms 9236 KB Output is correct
65 Correct 208 ms 9284 KB Output is correct
66 Correct 173 ms 9232 KB Output is correct
67 Correct 186 ms 9156 KB Output is correct
68 Correct 54 ms 9264 KB Output is correct
69 Correct 66 ms 9272 KB Output is correct
70 Correct 42 ms 9268 KB Output is correct
71 Correct 48 ms 9164 KB Output is correct
72 Correct 35 ms 9268 KB Output is correct
73 Correct 63 ms 9512 KB Output is correct
74 Correct 459 ms 9512 KB Output is correct
75 Correct 260 ms 9508 KB Output is correct
76 Correct 179 ms 9512 KB Output is correct
77 Correct 311 ms 9524 KB Output is correct
78 Correct 348 ms 9484 KB Output is correct
79 Correct 294 ms 9584 KB Output is correct
80 Correct 241 ms 9484 KB Output is correct
81 Correct 240 ms 9484 KB Output is correct
82 Correct 374 ms 9476 KB Output is correct
83 Correct 326 ms 9436 KB Output is correct
84 Execution timed out 1083 ms 9416 KB Time limit exceeded
85 Correct 491 ms 9420 KB Output is correct
86 Correct 365 ms 9440 KB Output is correct
87 Correct 761 ms 9444 KB Output is correct
88 Correct 514 ms 9396 KB Output is correct
89 Execution timed out 1086 ms 9400 KB Time limit exceeded
90 Correct 925 ms 9408 KB Output is correct
91 Execution timed out 1074 ms 9420 KB Time limit exceeded
92 Correct 733 ms 9284 KB Output is correct