#include <iostream> #include <string> #include <vector> #include <queue> #include <tuple> #include <map> int main() { std::ios::sync_with_stdio(false); int n, m, k; std::cin >> n >> m >> k; std::vector<std::string> map(n); for (int i = 0; i < n; ++i) std::cin >> map[i]; std::vector<std::vector<int>> dis(n, std::vector<int>(m, -1)); std::queue<std::tuple<int, int, int>> Q; Q.push({0, 0, 0}); while (dis[n - 1][m - 1] == -1) { auto [x, y, d] = Q.front(); Q.pop(); if (dis[x][y] != -1 || map[x][y] == 'X') continue; dis[x][y] = d; if (x > 0) Q.push({x - 1, y, d + 1}); if (x < n - 1) Q.push({x + 1, y, d + 1}); if (y > 0) Q.push({x, y - 1, d + 1}); if (y < m - 1) Q.push({x, y + 1, d + 1}); } int up = n + m - 2 + (dis[n - 1][m - 1] - (n + m - 2)) / 2; int down = (dis[n - 1][m - 1] - (n + m - 2)) / 2; long long best = 1e18; int count = 0; for (int i = 0; i < k; ++i) { int a, b; std::cin >> a >> b; long long ret = (long long)a * up + (long long)b * down; if (best > ret) { best = ret; count = 1; } else if (best == ret) { count++; } } std::cout << best << " " << count; 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 | #include <iostream> #include <string> #include <vector> #include <queue> #include <tuple> #include <map> int main() { std::ios::sync_with_stdio(false); int n, m, k; std::cin >> n >> m >> k; std::vector<std::string> map(n); for (int i = 0; i < n; ++i) std::cin >> map[i]; std::vector<std::vector<int>> dis(n, std::vector<int>(m, -1)); std::queue<std::tuple<int, int, int>> Q; Q.push({0, 0, 0}); while (dis[n - 1][m - 1] == -1) { auto [x, y, d] = Q.front(); Q.pop(); if (dis[x][y] != -1 || map[x][y] == 'X') continue; dis[x][y] = d; if (x > 0) Q.push({x - 1, y, d + 1}); if (x < n - 1) Q.push({x + 1, y, d + 1}); if (y > 0) Q.push({x, y - 1, d + 1}); if (y < m - 1) Q.push({x, y + 1, d + 1}); } int up = n + m - 2 + (dis[n - 1][m - 1] - (n + m - 2)) / 2; int down = (dis[n - 1][m - 1] - (n + m - 2)) / 2; long long best = 1e18; int count = 0; for (int i = 0; i < k; ++i) { int a, b; std::cin >> a >> b; long long ret = (long long)a * up + (long long)b * down; if (best > ret) { best = ret; count = 1; } else if (best == ret) { count++; } } std::cout << best << " " << count; return 0; } |