Submission #944985

# Submission time Handle Problem Language Result Execution time Memory
944985 2024-03-13T09:31:46 Z sano Mecho (IOI09_mecho) C++14
73 / 100
175 ms 6696 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') 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)) 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()) {
      |                                                    ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Correct 0 ms 344 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Correct 1 ms 360 KB Output is correct
6 Incorrect 1 ms 348 KB Output isn't correct
7 Incorrect 78 ms 6300 KB Output isn't correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Incorrect 0 ms 452 KB Output isn't correct
13 Incorrect 1 ms 600 KB Output isn't correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 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 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 356 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 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 464 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 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 5 ms 1648 KB Output is correct
34 Correct 5 ms 1668 KB Output is correct
35 Correct 21 ms 1692 KB Output is correct
36 Correct 6 ms 1884 KB Output is correct
37 Correct 6 ms 1884 KB Output is correct
38 Correct 27 ms 1880 KB Output is correct
39 Correct 9 ms 2416 KB Output is correct
40 Correct 10 ms 2424 KB Output is correct
41 Correct 65 ms 2428 KB Output is correct
42 Correct 10 ms 2892 KB Output is correct
43 Correct 10 ms 2940 KB Output is correct
44 Correct 49 ms 2956 KB Output is correct
45 Correct 13 ms 3356 KB Output is correct
46 Correct 12 ms 3420 KB Output is correct
47 Correct 53 ms 3404 KB Output is correct
48 Correct 14 ms 3928 KB Output is correct
49 Correct 15 ms 3988 KB Output is correct
50 Correct 62 ms 3924 KB Output is correct
51 Correct 18 ms 4580 KB Output is correct
52 Correct 18 ms 4548 KB Output is correct
53 Correct 69 ms 4580 KB Output is correct
54 Correct 22 ms 5148 KB Output is correct
55 Correct 20 ms 5224 KB Output is correct
56 Correct 121 ms 5160 KB Output is correct
57 Correct 29 ms 5812 KB Output is correct
58 Correct 22 ms 5872 KB Output is correct
59 Correct 92 ms 5868 KB Output is correct
60 Correct 27 ms 6664 KB Output is correct
61 Correct 31 ms 6628 KB Output is correct
62 Correct 98 ms 6676 KB Output is correct
63 Correct 108 ms 6628 KB Output is correct
64 Correct 175 ms 6696 KB Output is correct
65 Correct 162 ms 6624 KB Output is correct
66 Correct 124 ms 6592 KB Output is correct
67 Correct 120 ms 6600 KB Output is correct
68 Correct 83 ms 6676 KB Output is correct
69 Correct 61 ms 6616 KB Output is correct
70 Correct 48 ms 6672 KB Output is correct
71 Correct 50 ms 6696 KB Output is correct
72 Incorrect 40 ms 6644 KB Output isn't correct
73 Incorrect 42 ms 6652 KB Output isn't correct
74 Correct 63 ms 6652 KB Output is correct
75 Correct 126 ms 6640 KB Output is correct
76 Correct 74 ms 6592 KB Output is correct
77 Correct 117 ms 6640 KB Output is correct
78 Correct 80 ms 6660 KB Output is correct
79 Correct 85 ms 6648 KB Output is correct
80 Correct 64 ms 6612 KB Output is correct
81 Correct 69 ms 6604 KB Output is correct
82 Correct 59 ms 6640 KB Output is correct
83 Correct 98 ms 6644 KB Output is correct
84 Correct 89 ms 6468 KB Output is correct
85 Correct 76 ms 6664 KB Output is correct
86 Correct 76 ms 6668 KB Output is correct
87 Correct 80 ms 6676 KB Output is correct
88 Correct 79 ms 6676 KB Output is correct
89 Correct 116 ms 6636 KB Output is correct
90 Correct 76 ms 6688 KB Output is correct
91 Correct 137 ms 6604 KB Output is correct
92 Correct 80 ms 6684 KB Output is correct