#include <cassert>
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
struct P {
i16 i, j;
i32 g, d;
};
typedef std::queue<P> QP;
u8 mapa[2002][2002];
QP q;
void Sprawdz(i16 i, i16 j, i32 g, i32 d)
{
if (!mapa[i][j]) {
mapa[i][j] = 1;
P p = { i, j, g, d };
q.push(p);
}
}
int main()
{
i16 n, m;
i32 k;
std::cin >> n >> m >> k;
std::string buf;
buf.reserve(m);
memset(mapa[0], 1, m+2);
for (i32 i = 1; i <= n; i++) {
std::cin >> buf;
mapa[i][0] = 1;
for (i32 j = 1; j <= m; j++) {
mapa[i][j] = buf[j-1] == 'X' ? 1 : 0;
}
mapa[i][m+1] = 1;
}
memset(mapa[n+1], 1, m+2);
P p = { 1, 1, 0, 0 };
q.push(p);
while (!q.empty()) {
p = q.front();
q.pop();
if (p.i == n && p.j == m)
break;
Sprawdz(p.i + 1, p.j, p.g + 1, p.d); // S
Sprawdz(p.i, p.j + 1, p.g + 1, p.d); // E
Sprawdz(p.i - 1, p.j, p.g, p.d + 1); // N
Sprawdz(p.i, p.j - 1, p.g, p.d + 1); // W
}
assert(p.i == n);
assert(p.j == m);
i32 g = p.g;
i32 d = p.d;
i64 best = INT64_MAX;
i32 count = 0;
for (i32 ll = 0; ll < k; ll++) {
i64 tg, td;
std::cin >> tg >> td;
i64 t = tg * g + td * d;
if (t < best) {
best = t;
count = 1;
} else if (t == best) {
count++;
}
}
std::cout << best << ' ' << count << '\n';
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <cassert> #include <cstdint> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <queue> typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef uint8_t u8; struct P { i16 i, j; i32 g, d; }; typedef std::queue<P> QP; u8 mapa[2002][2002]; QP q; void Sprawdz(i16 i, i16 j, i32 g, i32 d) { if (!mapa[i][j]) { mapa[i][j] = 1; P p = { i, j, g, d }; q.push(p); } } int main() { i16 n, m; i32 k; std::cin >> n >> m >> k; std::string buf; buf.reserve(m); memset(mapa[0], 1, m+2); for (i32 i = 1; i <= n; i++) { std::cin >> buf; mapa[i][0] = 1; for (i32 j = 1; j <= m; j++) { mapa[i][j] = buf[j-1] == 'X' ? 1 : 0; } mapa[i][m+1] = 1; } memset(mapa[n+1], 1, m+2); P p = { 1, 1, 0, 0 }; q.push(p); while (!q.empty()) { p = q.front(); q.pop(); if (p.i == n && p.j == m) break; Sprawdz(p.i + 1, p.j, p.g + 1, p.d); // S Sprawdz(p.i, p.j + 1, p.g + 1, p.d); // E Sprawdz(p.i - 1, p.j, p.g, p.d + 1); // N Sprawdz(p.i, p.j - 1, p.g, p.d + 1); // W } assert(p.i == n); assert(p.j == m); i32 g = p.g; i32 d = p.d; i64 best = INT64_MAX; i32 count = 0; for (i32 ll = 0; ll < k; ll++) { i64 tg, td; std::cin >> tg >> td; i64 t = tg * g + td * d; if (t < best) { best = t; count = 1; } else if (t == best) { count++; } } std::cout << best << ' ' << count << '\n'; return 0; } |
English