#include <deque> #include <iostream> #include <limits> #include <unordered_map> #include <vector> using i64 = int64_t; template<typename T> struct Array2D { Array2D(int size_x, int size_y, T init = {}) : data(size_x * size_y, init) , height(size_x) , width(size_y) {} T &at(int x, int y) { return data[x * width + y]; } T &at(std::pair<int, int> const &xy) { return at(xy.first, xy.second); } std::vector<T> data; int height; int width; }; std::pair<int, int> bfs(Array2D<char> &map) { constexpr int R = 1; constexpr int D = 2; constexpr int L = -1; constexpr int U = -2; Array2D<std::pair<int, int>> info{ map.height, map.width, { 0, 0 } }; Array2D<char> visited{ map.height, map.width, false }; visited.at(0, 0) = true; std::deque<std::pair<std::pair<int, int>, int>> todo; todo.push_back({ { 0, 1 }, R }); todo.push_back({ { 1, 0 }, D }); while (!todo.empty()) { auto [yx, d] = todo.front(); auto [y, x] = yx; todo.pop_front(); if (visited.at(y, x) || map.at(y, x) == 'X') continue; visited.at(y, x) = true; if (d == R) { auto &nfo = info.at(y, x); nfo = info.at(y, x - 1); ++nfo.first; } else if (d == D) { auto &nfo = info.at(y, x); nfo = info.at(y - 1, x); ++nfo.first; } else if (d == L) { auto &nfo = info.at(y, x); nfo = info.at(y, x + 1); ++nfo.second; } else { auto &nfo = info.at(y, x); nfo = info.at(y + 1, x); ++nfo.second; } if (x + 1 < map.width) todo.push_back({ { y, x + 1 }, R }); if (x > 0) todo.push_back({ { y, x - 1 }, L }); if (y + 1 < map.height) todo.push_back({ { y + 1, x }, D }); if (y > 0) todo.push_back({ { y - 1, x }, U }); } return info.at(map.height - 1, map.width - 1); } int main() { std::ios::sync_with_stdio(false); int n, m, k; std::cin >> n >> m >> k; Array2D<char> map{ n, m, 0 }; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { std::cin >> map.at(i, j); } } std::vector<std::pair<i64, i64>> people(k); for (int i = 0; i < k; ++i) { std::cin >> people[i].first >> people[i].second; } auto [min_a, min_b] = bfs(map); i64 best_time = std::numeric_limits<i64>::max(); std::unordered_map<i64, i64> best_count; for (int i = 0; i < k; ++i) { const i64 time = min_a * people[i].first + min_b * people[i].second; ++best_count[time]; if (time < best_time) best_time = time; } std::cout << best_time << " " << best_count[best_time] << std::endl; 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #include <deque> #include <iostream> #include <limits> #include <unordered_map> #include <vector> using i64 = int64_t; template<typename T> struct Array2D { Array2D(int size_x, int size_y, T init = {}) : data(size_x * size_y, init) , height(size_x) , width(size_y) {} T &at(int x, int y) { return data[x * width + y]; } T &at(std::pair<int, int> const &xy) { return at(xy.first, xy.second); } std::vector<T> data; int height; int width; }; std::pair<int, int> bfs(Array2D<char> &map) { constexpr int R = 1; constexpr int D = 2; constexpr int L = -1; constexpr int U = -2; Array2D<std::pair<int, int>> info{ map.height, map.width, { 0, 0 } }; Array2D<char> visited{ map.height, map.width, false }; visited.at(0, 0) = true; std::deque<std::pair<std::pair<int, int>, int>> todo; todo.push_back({ { 0, 1 }, R }); todo.push_back({ { 1, 0 }, D }); while (!todo.empty()) { auto [yx, d] = todo.front(); auto [y, x] = yx; todo.pop_front(); if (visited.at(y, x) || map.at(y, x) == 'X') continue; visited.at(y, x) = true; if (d == R) { auto &nfo = info.at(y, x); nfo = info.at(y, x - 1); ++nfo.first; } else if (d == D) { auto &nfo = info.at(y, x); nfo = info.at(y - 1, x); ++nfo.first; } else if (d == L) { auto &nfo = info.at(y, x); nfo = info.at(y, x + 1); ++nfo.second; } else { auto &nfo = info.at(y, x); nfo = info.at(y + 1, x); ++nfo.second; } if (x + 1 < map.width) todo.push_back({ { y, x + 1 }, R }); if (x > 0) todo.push_back({ { y, x - 1 }, L }); if (y + 1 < map.height) todo.push_back({ { y + 1, x }, D }); if (y > 0) todo.push_back({ { y - 1, x }, U }); } return info.at(map.height - 1, map.width - 1); } int main() { std::ios::sync_with_stdio(false); int n, m, k; std::cin >> n >> m >> k; Array2D<char> map{ n, m, 0 }; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { std::cin >> map.at(i, j); } } std::vector<std::pair<i64, i64>> people(k); for (int i = 0; i < k; ++i) { std::cin >> people[i].first >> people[i].second; } auto [min_a, min_b] = bfs(map); i64 best_time = std::numeric_limits<i64>::max(); std::unordered_map<i64, i64> best_count; for (int i = 0; i < k; ++i) { const i64 time = min_a * people[i].first + min_b * people[i].second; ++best_count[time]; if (time < best_time) best_time = time; } std::cout << best_time << " " << best_count[best_time] << std::endl; return 0; } |