답안 #1016373

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016373 2024-07-08T02:21:34 Z nrg_studio Mecho (IOI09_mecho) C++17
100 / 100
161 ms 5660 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define f first
#define s second
#define FOR(i, a, b, s) for (int i = (a); i < (b); i += s)
#define F0R(i, a) for (int i = 0; i < (a); i++)

vector<vector<int>> g;
pii mecho, home;
int n, s;
pii d[4] = {{0,1},{0,-1},{1,0},{-1,0}};

bool check(int t) {
	vector<vector<int>> dist(n+2,vector<int>(n+2,INT_MAX));
	dist[mecho.f][mecho.s] = t*s;
	queue<pii> q; q.push(mecho);
	
	while (q.size()) {
		pii cur = q.front(); q.pop();
		if (dist[cur.f][cur.s]>=g[cur.f][cur.s]) {continue;}
		for (pii y : d) {
			if (dist[cur.f+y.f][cur.s+y.s]==INT_MAX) {
				dist[cur.f+y.f][cur.s+y.s] = dist[cur.f][cur.s]+1;
				q.push({cur.f+y.f,cur.s+y.s});
			}
		}
	} return (dist[home.f][home.s]!=INT_MAX);
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> n >> s;
	g = vector<vector<int>>(n+2,vector<int>(n+2,INT_MAX));
	char c; queue<pii> q;
	F0R(i,n) {
		F0R(j,n) {
			cin >> c;
			if (c=='M') {mecho = {i+1,j+1}; g[i+1][j+1]=-1;}
			else if (c=='D') {home = {i+1,j+1};}
			else if (c=='H') {g[i+1][j+1]=0; q.push({i+1,j+1});}
			else if (c=='G') {g[i+1][j+1]=-1;}
			else if (c=='T') {g[i+1][j+1]=-2;}
		}
	}
	F0R(i,n+1) {g[0][i] = g[i][0] = g[n+1][i] = g[i][n+1] = -2;}
	while (q.size()) {
		pii x = q.front(); q.pop();
		for (pii y : d) {
			if (g[x.f+y.f][x.s+y.s]==-1) {
				g[x.f+y.f][x.s+y.s]=g[x.f][x.s]+s;
				q.push({x.f+y.f,x.s+y.s});
			}
		}
	}
	int l = 0, h = n*n, m = (l+h)/2, ans = -1;
	while (l <= h) {
		if (check(m)) {ans = m; l = m+1;}
		else {h = m-1;}
		m = (l+h)/2;
	} cout << ans << '\n';
} 	
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 53 ms 5624 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 416 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 5 ms 1444 KB Output is correct
34 Correct 6 ms 1456 KB Output is correct
35 Correct 15 ms 1456 KB Output is correct
36 Correct 6 ms 1748 KB Output is correct
37 Correct 8 ms 1628 KB Output is correct
38 Correct 19 ms 1756 KB Output is correct
39 Correct 11 ms 2080 KB Output is correct
40 Correct 9 ms 2096 KB Output is correct
41 Correct 26 ms 2132 KB Output is correct
42 Correct 11 ms 2484 KB Output is correct
43 Correct 12 ms 2456 KB Output is correct
44 Correct 33 ms 2440 KB Output is correct
45 Correct 14 ms 2912 KB Output is correct
46 Correct 13 ms 2880 KB Output is correct
47 Correct 43 ms 2880 KB Output is correct
48 Correct 16 ms 3364 KB Output is correct
49 Correct 16 ms 3368 KB Output is correct
50 Correct 58 ms 3368 KB Output is correct
51 Correct 25 ms 3876 KB Output is correct
52 Correct 20 ms 3888 KB Output is correct
53 Correct 56 ms 3876 KB Output is correct
54 Correct 20 ms 4408 KB Output is correct
55 Correct 22 ms 4412 KB Output is correct
56 Correct 65 ms 4384 KB Output is correct
57 Correct 23 ms 5056 KB Output is correct
58 Correct 26 ms 4968 KB Output is correct
59 Correct 72 ms 4952 KB Output is correct
60 Correct 31 ms 5572 KB Output is correct
61 Correct 29 ms 5632 KB Output is correct
62 Correct 82 ms 5560 KB Output is correct
63 Correct 88 ms 5520 KB Output is correct
64 Correct 161 ms 5540 KB Output is correct
65 Correct 141 ms 5536 KB Output is correct
66 Correct 102 ms 5596 KB Output is correct
67 Correct 96 ms 5592 KB Output is correct
68 Correct 47 ms 5652 KB Output is correct
69 Correct 48 ms 5648 KB Output is correct
70 Correct 38 ms 5640 KB Output is correct
71 Correct 42 ms 5648 KB Output is correct
72 Correct 33 ms 5632 KB Output is correct
73 Correct 32 ms 5612 KB Output is correct
74 Correct 41 ms 5604 KB Output is correct
75 Correct 50 ms 5600 KB Output is correct
76 Correct 45 ms 5612 KB Output is correct
77 Correct 48 ms 5616 KB Output is correct
78 Correct 57 ms 5604 KB Output is correct
79 Correct 38 ms 5600 KB Output is correct
80 Correct 42 ms 5588 KB Output is correct
81 Correct 47 ms 5588 KB Output is correct
82 Correct 45 ms 5608 KB Output is correct
83 Correct 51 ms 5616 KB Output is correct
84 Correct 47 ms 5592 KB Output is correct
85 Correct 52 ms 5584 KB Output is correct
86 Correct 50 ms 5572 KB Output is correct
87 Correct 50 ms 5564 KB Output is correct
88 Correct 54 ms 5660 KB Output is correct
89 Correct 67 ms 5592 KB Output is correct
90 Correct 54 ms 5572 KB Output is correct
91 Correct 52 ms 5604 KB Output is correct
92 Correct 55 ms 5576 KB Output is correct