답안 #944993

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944993 2024-03-13T09:40:51 Z sano Mecho (IOI09_mecho) C++14
84 / 100
179 ms 6528 KB
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<fstream>
#include<algorithm>
#define For(i, n) for(int i = 0; i < n; i++)
#define ffor(i, a, n) for(int i = a; i < n; i++)
#define vec vector
#define ff first
#define ss second
#define pb push_back
#define pairs pair<int, int>
#define NEK 1000000000

using namespace std;

vec<pairs> r = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

void BFS(queue<pairs>&q, vec<vec<char>>&p, vec<vec<int>>&d) {
	while (!q.empty()) {
		pairs x = q.front(); q.pop();
		For(i, 4) {
			pairs t = { x.ff + r[i].ff, x.ss + r[i].ss };
			if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) continue;
			if (p[t.ff][t.ss] == 'T' || p[t.ff][t.ss] == 'D') continue;
			if (d[t.ff][t.ss] != -1) continue;
			d[t.ff][t.ss] = d[x.ff][x.ss] + 1;
			q.push(t);
		}
	}
}

bool BFS2(queue<pairs> q, vec<vec<char>>& p, vec<vec<int>>& d, int&s, pairs&e, int&c) {
	vec<vec<int>> d2(p.size(), vec<int>(p.size(), -1));
	d2[q.front().ff][q.front().ss] = 0;
	while (!q.empty()) {
		pairs x = q.front(); q.pop();
		if (x.ff == e.ff && x.ss == e.ss) return true;
		For(i, 4) {
			pairs t = { x.ff + r[i].ff, x.ss + r[i].ss };
			if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) {
				continue;
			}
			if (p[t.ff][t.ss] == 'T' || d2[t.ff][t.ss] != -1) continue;
			d2[t.ff][t.ss] = d2[x.ff][x.ss] + 1;
			if ((d2[t.ff][t.ss] / s) >= (d[t.ff][t.ss] - c) && d[t.ff][t.ss] != -1) continue;
			q.push(t);
		}
	}
	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//ifstream cin("atlarge.in");
	//ofstream cout("atlarge.out");
	int n, v;
	cin >> n >> v;
	vec<vec<char>> p(n, vec<char>(n));
	pairs s, e;
	queue<pairs> q;
	vec<vec<int>> d1(n, vec<int>(n, -1));
	For(i, n) {
		For(j, n) {
			cin >> p[i][j];
			if (p[i][j] == 'M') {
				s = { i, j };
			}
			if (p[i][j] == 'D') {
				e = { i, j };
			}
			if (p[i][j] == 'H') {
				q.push({ i, j });
				d1[i][j] = 0;
			}
		}
	}
	BFS(q, p, d1);
	int l = 0, r = n*n;
	q.push(s);
	while (l < r) {
		int c = (l + r) / 2 + 1;
		if (BFS2(q, p, d1, v, e, c)) l = c;
		else r = c-1;
	}
	if (BFS2(q, p, d1, v, e, l)) cout << l << '\n';
	else cout << -1 << '\n';
	return 0;
}

Compilation message

mecho.cpp: In function 'void BFS(std::queue<std::pair<int, int> >&, std::vector<std::vector<char> >&, std::vector<std::vector<int> >&)':
mecho.cpp:25:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |    if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) continue;
      |                                ~~~~~^~~~~~~~~~~
mecho.cpp:25:57: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |    if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) continue;
      |                                                    ~~~~~^~~~~~~~~~~
mecho.cpp: In function 'bool BFS2(std::queue<std::pair<int, int> >, std::vector<std::vector<char> >&, std::vector<std::vector<int> >&, int&, std::pair<int, int>&, int&)':
mecho.cpp:42:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |    if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) {
      |                                ~~~~~^~~~~~~~~~~
mecho.cpp:42:57: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |    if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) {
      |                                                    ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 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 344 KB Output is correct
7 Correct 91 ms 6528 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Incorrect 1 ms 600 KB Output isn't correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 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 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 5 ms 1572 KB Output is correct
34 Correct 9 ms 1612 KB Output is correct
35 Correct 19 ms 1568 KB Output is correct
36 Correct 6 ms 1884 KB Output is correct
37 Correct 6 ms 1884 KB Output is correct
38 Correct 28 ms 1884 KB Output is correct
39 Correct 8 ms 2272 KB Output is correct
40 Correct 9 ms 2332 KB Output is correct
41 Correct 33 ms 2296 KB Output is correct
42 Correct 16 ms 2736 KB Output is correct
43 Correct 10 ms 2780 KB Output is correct
44 Correct 40 ms 2768 KB Output is correct
45 Correct 13 ms 3164 KB Output is correct
46 Correct 12 ms 3212 KB Output is correct
47 Correct 53 ms 3220 KB Output is correct
48 Correct 14 ms 3688 KB Output is correct
49 Correct 14 ms 3756 KB Output is correct
50 Correct 73 ms 3676 KB Output is correct
51 Correct 19 ms 4308 KB Output is correct
52 Correct 18 ms 4308 KB Output is correct
53 Correct 117 ms 4524 KB Output is correct
54 Correct 21 ms 4900 KB Output is correct
55 Correct 20 ms 4900 KB Output is correct
56 Correct 78 ms 4880 KB Output is correct
57 Correct 23 ms 5516 KB Output is correct
58 Correct 23 ms 5516 KB Output is correct
59 Correct 105 ms 5500 KB Output is correct
60 Correct 26 ms 6200 KB Output is correct
61 Correct 30 ms 6296 KB Output is correct
62 Correct 106 ms 6204 KB Output is correct
63 Correct 116 ms 6168 KB Output is correct
64 Correct 160 ms 6232 KB Output is correct
65 Correct 179 ms 6384 KB Output is correct
66 Correct 147 ms 6220 KB Output is correct
67 Correct 125 ms 6180 KB Output is correct
68 Correct 63 ms 6272 KB Output is correct
69 Correct 72 ms 6244 KB Output is correct
70 Correct 46 ms 6216 KB Output is correct
71 Correct 46 ms 6212 KB Output is correct
72 Incorrect 38 ms 6292 KB Output isn't correct
73 Incorrect 46 ms 6308 KB Output isn't correct
74 Correct 61 ms 6252 KB Output is correct
75 Correct 69 ms 6284 KB Output is correct
76 Correct 62 ms 6236 KB Output is correct
77 Correct 83 ms 6232 KB Output is correct
78 Correct 81 ms 6228 KB Output is correct
79 Correct 50 ms 6240 KB Output is correct
80 Correct 107 ms 6220 KB Output is correct
81 Correct 74 ms 6228 KB Output is correct
82 Correct 67 ms 6188 KB Output is correct
83 Correct 99 ms 6288 KB Output is correct
84 Correct 67 ms 6212 KB Output is correct
85 Correct 78 ms 6240 KB Output is correct
86 Correct 82 ms 6244 KB Output is correct
87 Correct 74 ms 6344 KB Output is correct
88 Correct 82 ms 6232 KB Output is correct
89 Correct 96 ms 6216 KB Output is correct
90 Correct 112 ms 6236 KB Output is correct
91 Correct 168 ms 6240 KB Output is correct
92 Correct 87 ms 6240 KB Output is correct