답안 #790757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
790757 2023-07-23T07:39:32 Z Gray Mecho (IOI09_mecho) C++17
21 / 100
1000 ms 6836 KB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define ln "\n"
#define ff first
#define ss second
#define INF 2e9
using namespace std;

void solve(){
	ll n, s; cin >> n >> s;
	vector<vector<char>> gr(n, vector<char>(n));
	vector<pair<ll, ll>> hvs;
	pair<ll, ll> bcor, hcor;
	for (ll i=0; i<n; i++){
		for (ll j=0; j<n; j++){
			cin >> gr[i][j];
			if (gr[i][j]=='H'){
				hvs.push_back({i, j});
			}else if (gr[i][j]=='M'){
				bcor = {i, j};
			}else if (gr[i][j]=='D'){
				hcor = {i, j};
			}
		}
	}
	vector<vector<ll>> hpath(n, vector<ll>(n, INF));

	auto bfs1 = [&](pair<ll, ll> cord){
		hpath[cord.ff][cord.ss]=1;
		vector<vector<ll>> vi(n, vector<ll>(n, INF));
		queue<pair<ll, ll>> q;
		q.push(cord);
		vi[cord.ff][cord.ss]=1;
		while (!q.empty()){
			pair<ll, ll> cur = q.front();
			q.pop();
			if (cur.ff>0 and vi[cur.ff-1][cur.ss]==INF and gr[cur.ff-1][cur.ss]!='T' and gr[cur.ff-1][cur.ss]!='D' and gr[cur.ff-1][cur.ss]!='H'){
				vi[cur.ff-1][cur.ss]=vi[cur.ff][cur.ss]+1;
				hpath[cur.ff-1][cur.ss]=min(hpath[cur.ff-1][cur.ss], vi[cur.ff-1][cur.ss]);
				q.push({cur.ff-1, cur.ss});
			}
			if (cur.ss>0 and vi[cur.ff][cur.ss-1]==INF and gr[cur.ff][cur.ss-1]!='T' and gr[cur.ff][cur.ss-1]!='D' and gr[cur.ff][cur.ss-1]!='H'){
				vi[cur.ff][cur.ss-1]=vi[cur.ff][cur.ss]+1;
				hpath[cur.ff][cur.ss-1]=min(hpath[cur.ff][cur.ss-1], vi[cur.ff][cur.ss-1]);
				q.push({cur.ff, cur.ss-1});
			}
			if (cur.ff+1<n and vi[cur.ff+1][cur.ss]==INF and gr[cur.ff+1][cur.ss]!='T' and gr[cur.ff+1][cur.ss]!='D' and gr[cur.ff+1][cur.ss]!='H'){
				vi[cur.ff+1][cur.ss]=vi[cur.ff][cur.ss]+1;
				hpath[cur.ff+1][cur.ss]=min(hpath[cur.ff+1][cur.ss], vi[cur.ff+1][cur.ss]);
				q.push({cur.ff+1, cur.ss});
			}
			if (cur.ss+1<n and vi[cur.ff][cur.ss+1]==INF and gr[cur.ff][cur.ss+1]!='T' and gr[cur.ff][cur.ss+1]!='D' and gr[cur.ff][cur.ss+1]!='H'){
				vi[cur.ff][cur.ss+1]=vi[cur.ff][cur.ss]+1;
				hpath[cur.ff][cur.ss+1]=min(hpath[cur.ff][cur.ss+1], vi[cur.ff][cur.ss+1]);
				q.push({cur.ff, cur.ss+1});
			}
		}
	};
	auto bfs2 = [&](ll wt) -> bool{
		pair<ll, ll> cord = bcor;
		if (hpath[cord.ff][cord.ss]-1<wt) {
			// cout << "Fail at start: " << hpath[cord.ff][cord.ss] << " <: " << cord.ff << "," << cord.ss << " -> " << wt << ln; 
			return 0;
		}
		vector<vector<ll>> vi(n, vector<ll>(n, INF));
		queue<pair<ll, ll>> q;
		q.push(cord);
		vi[cord.ff][cord.ss]=wt;
		while (!q.empty()){
			pair<ll, ll> cur = q.front();
			q.pop();
			// cout << "(" << cur.ss << "," << cur.ff << ") -> hpath=" << hpath[cur.ff][cur.ss] << "; mdist=" << vi[cur.ff][cur.ss]-wt << "; required=" << ((vi[cur.ff][cur.ss]-wt)/s+wt) << "; " << " possible=" << hpath[cur.ff][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1 << ln;
			if (cur.ff>0 and vi[cur.ff-1][cur.ss]==INF and gr[cur.ff-1][cur.ss]!='T' and gr[cur.ff-1][cur.ss]!='H' and ((vi[cur.ff][cur.ss]-wt)/s+wt)<=(hpath[cur.ff-1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1)){
				// cout << (hpath[cur.ff-1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to U: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
				vi[cur.ff-1][cur.ss]=vi[cur.ff][cur.ss]+1;
				q.push({cur.ff-1, cur.ss});
			}
			if (cur.ss>0 and vi[cur.ff][cur.ss-1]==INF and gr[cur.ff][cur.ss-1]!='T' and gr[cur.ff][cur.ss-1]!='H' and ((vi[cur.ff][cur.ss]-wt)/s+wt)<=(hpath[cur.ff][cur.ss-1]-(vi[cur.ff][cur.ss]-wt)/s-1)){
				// cout << (hpath[cur.ff][cur.ss-1]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to L: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
				vi[cur.ff][cur.ss-1]=vi[cur.ff][cur.ss]+1;
				q.push({cur.ff, cur.ss-1});
			}
			if (cur.ff+1<n and vi[cur.ff+1][cur.ss]==INF and gr[cur.ff+1][cur.ss]!='T' and gr[cur.ff+1][cur.ss]!='H' and ((vi[cur.ff][cur.ss]-wt)/s+wt)<=(hpath[cur.ff+1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1)){
				// cout << (hpath[cur.ff+1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to D: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
				vi[cur.ff+1][cur.ss]=vi[cur.ff][cur.ss]+1;
				q.push({cur.ff+1, cur.ss});
			}
			if (cur.ss+1<n and vi[cur.ff][cur.ss+1]==INF and gr[cur.ff][cur.ss+1]!='T' and gr[cur.ff][cur.ss+1]!='H' and ((vi[cur.ff][cur.ss]-wt)/s+wt)<=(hpath[cur.ff][cur.ss+1]-(vi[cur.ff][cur.ss]-wt)/s-1)){
				// cout << (hpath[cur.ff][cur.ss+1]-(vi[cur.ff][cur.ss]+1-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to R: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
				vi[cur.ff][cur.ss+1]=vi[cur.ff][cur.ss]+1;
				q.push({cur.ff, cur.ss+1});
			}
		}
		if (vi[hcor.ff][hcor.ss]<INF) return 1;
		else return 0;
	};
	for (auto hive:hvs){
		bfs1(hive);
	}
	ll l=0, r=n*n+1;
	while (l+1<r){
		ll mid = (l+r)/2;
		if (bfs2(mid)) {
			l=mid;
			// cout << mid << " with T" << ln;
		}
		else {
			r=mid;
			// cout << mid << " with F" << ln;
		}
	}
	if (bfs2(l))cout << l << ln;
	else cout << -1 << ln;
}

int main() {
	ll t=1; 
	// cin >> t;
	while (t--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Correct 0 ms 300 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Incorrect 1 ms 304 KB Output isn't correct
5 Correct 0 ms 212 KB Output is correct
6 Incorrect 1 ms 212 KB Output isn't correct
7 Execution timed out 1077 ms 6660 KB Time limit exceeded
8 Correct 1 ms 212 KB Output is correct
9 Incorrect 1 ms 212 KB Output isn't correct
10 Incorrect 0 ms 212 KB Output isn't correct
11 Incorrect 0 ms 212 KB Output isn't correct
12 Incorrect 1 ms 212 KB Output isn't correct
13 Correct 6 ms 332 KB Output is correct
14 Correct 6 ms 336 KB Output is correct
15 Incorrect 0 ms 212 KB Output isn't correct
16 Correct 1 ms 212 KB Output is correct
17 Incorrect 1 ms 212 KB Output isn't correct
18 Correct 1 ms 212 KB Output is correct
19 Incorrect 1 ms 212 KB Output isn't correct
20 Correct 1 ms 296 KB Output is correct
21 Incorrect 1 ms 212 KB Output isn't correct
22 Correct 1 ms 212 KB Output is correct
23 Incorrect 1 ms 212 KB Output isn't correct
24 Correct 1 ms 212 KB Output is correct
25 Incorrect 1 ms 212 KB Output isn't correct
26 Correct 1 ms 340 KB Output is correct
27 Incorrect 1 ms 340 KB Output isn't correct
28 Correct 1 ms 340 KB Output is correct
29 Incorrect 1 ms 340 KB Output isn't correct
30 Correct 1 ms 340 KB Output is correct
31 Incorrect 1 ms 340 KB Output isn't correct
32 Correct 1 ms 340 KB Output is correct
33 Incorrect 7 ms 1532 KB Output isn't correct
34 Correct 9 ms 1548 KB Output is correct
35 Incorrect 21 ms 1492 KB Output isn't correct
36 Incorrect 10 ms 1848 KB Output isn't correct
37 Correct 12 ms 1876 KB Output is correct
38 Incorrect 21 ms 1920 KB Output isn't correct
39 Incorrect 17 ms 2316 KB Output isn't correct
40 Correct 15 ms 2332 KB Output is correct
41 Incorrect 27 ms 2260 KB Output isn't correct
42 Incorrect 17 ms 2868 KB Output isn't correct
43 Correct 19 ms 2784 KB Output is correct
44 Incorrect 36 ms 2724 KB Output isn't correct
45 Incorrect 18 ms 3348 KB Output isn't correct
46 Correct 22 ms 3348 KB Output is correct
47 Incorrect 42 ms 3320 KB Output isn't correct
48 Incorrect 23 ms 3872 KB Output isn't correct
49 Correct 26 ms 3920 KB Output is correct
50 Incorrect 52 ms 3884 KB Output isn't correct
51 Incorrect 26 ms 4500 KB Output isn't correct
52 Correct 37 ms 4576 KB Output is correct
53 Incorrect 58 ms 4488 KB Output isn't correct
54 Incorrect 29 ms 5184 KB Output isn't correct
55 Correct 35 ms 5208 KB Output is correct
56 Incorrect 71 ms 5172 KB Output isn't correct
57 Incorrect 35 ms 5932 KB Output isn't correct
58 Correct 41 ms 5924 KB Output is correct
59 Incorrect 102 ms 5852 KB Output isn't correct
60 Incorrect 39 ms 6684 KB Output isn't correct
61 Correct 48 ms 6680 KB Output is correct
62 Incorrect 92 ms 6652 KB Output isn't correct
63 Execution timed out 1091 ms 6656 KB Time limit exceeded
64 Execution timed out 1082 ms 6732 KB Time limit exceeded
65 Execution timed out 1077 ms 6740 KB Time limit exceeded
66 Execution timed out 1074 ms 6660 KB Time limit exceeded
67 Execution timed out 1079 ms 6696 KB Time limit exceeded
68 Execution timed out 1078 ms 6660 KB Time limit exceeded
69 Execution timed out 1085 ms 6656 KB Time limit exceeded
70 Execution timed out 1074 ms 6780 KB Time limit exceeded
71 Execution timed out 1078 ms 6680 KB Time limit exceeded
72 Execution timed out 1083 ms 6660 KB Time limit exceeded
73 Execution timed out 1078 ms 6736 KB Time limit exceeded
74 Execution timed out 1070 ms 6764 KB Time limit exceeded
75 Execution timed out 1077 ms 6768 KB Time limit exceeded
76 Execution timed out 1084 ms 6724 KB Time limit exceeded
77 Execution timed out 1070 ms 6704 KB Time limit exceeded
78 Execution timed out 1038 ms 6776 KB Time limit exceeded
79 Execution timed out 1071 ms 6836 KB Time limit exceeded
80 Execution timed out 1085 ms 6828 KB Time limit exceeded
81 Execution timed out 1062 ms 6720 KB Time limit exceeded
82 Execution timed out 1082 ms 6684 KB Time limit exceeded
83 Execution timed out 1084 ms 6708 KB Time limit exceeded
84 Execution timed out 1083 ms 6824 KB Time limit exceeded
85 Execution timed out 1081 ms 6676 KB Time limit exceeded
86 Execution timed out 1077 ms 6704 KB Time limit exceeded
87 Execution timed out 1080 ms 6672 KB Time limit exceeded
88 Execution timed out 1072 ms 6612 KB Time limit exceeded
89 Execution timed out 1071 ms 6704 KB Time limit exceeded
90 Execution timed out 1078 ms 6692 KB Time limit exceeded
91 Execution timed out 1075 ms 6716 KB Time limit exceeded
92 Execution timed out 1072 ms 6588 KB Time limit exceeded