#include <cstdio> #include <cstdint> #include <vector> #include <queue> using namespace std; const int INF = 100000000; typedef struct { int best; bool safe; } field_t; int main () { int n, m, k; scanf("%d %d %d\n", &n, &m, &k); field_t f; f.best = INF; f.safe = true; vector<vector<field_t> > mountain; mountain.reserve(n + 4); for (int i = 0; i < n; ++i) { vector<field_t> row; mountain.push_back(row); mountain[i].reserve(m + 4); for (int j = 0; j < m; ++j) { char c; scanf("%c", &c); f.safe = (c == '.'); mountain[i].push_back(f); } scanf("\n"); } queue<pair<int, int> > q; q.push(make_pair(0, 0)); mountain[0][0].best = 0; while (!q.empty()) { pair<int, int> coords = q.front(); q.pop(); pair<int, int> neighs[] = { make_pair(coords.first - 1, coords.second), make_pair(coords.first + 1, coords.second), make_pair(coords.first, coords.second + 1), make_pair(coords.first, coords.second - 1), }; for (int i = 0; i < 4; ++i) { if ( (neighs[i].first < 0) || (neighs[i].first >= n) || (neighs[i].second < 0) || (neighs[i].second >= m) || (!mountain[neighs[i].first][neighs[i].second].safe) || (mountain[neighs[i].first][neighs[i].second].best < INF) ) { continue; } mountain[neighs[i].first][neighs[i].second].best = mountain[coords.first][coords.second].best + 1; q.push(neighs[i]); } } int len = mountain[n - 1][m - 1].best; int down = (len - m - n + 2) / 2; int up = m + n + down - 2; int64_t best = -1; int count = 0; for (int i = 0; i < k; ++i) { int64_t a, b; scanf("%lld %lld", &a, &b); int64_t t = a * up + b * down; if ((best == -1) || (t < best)) { best = t; count = 1; } else if (t == best) { ++count; } } printf("%lld %d\n", 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 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 | #include <cstdio> #include <cstdint> #include <vector> #include <queue> using namespace std; const int INF = 100000000; typedef struct { int best; bool safe; } field_t; int main () { int n, m, k; scanf("%d %d %d\n", &n, &m, &k); field_t f; f.best = INF; f.safe = true; vector<vector<field_t> > mountain; mountain.reserve(n + 4); for (int i = 0; i < n; ++i) { vector<field_t> row; mountain.push_back(row); mountain[i].reserve(m + 4); for (int j = 0; j < m; ++j) { char c; scanf("%c", &c); f.safe = (c == '.'); mountain[i].push_back(f); } scanf("\n"); } queue<pair<int, int> > q; q.push(make_pair(0, 0)); mountain[0][0].best = 0; while (!q.empty()) { pair<int, int> coords = q.front(); q.pop(); pair<int, int> neighs[] = { make_pair(coords.first - 1, coords.second), make_pair(coords.first + 1, coords.second), make_pair(coords.first, coords.second + 1), make_pair(coords.first, coords.second - 1), }; for (int i = 0; i < 4; ++i) { if ( (neighs[i].first < 0) || (neighs[i].first >= n) || (neighs[i].second < 0) || (neighs[i].second >= m) || (!mountain[neighs[i].first][neighs[i].second].safe) || (mountain[neighs[i].first][neighs[i].second].best < INF) ) { continue; } mountain[neighs[i].first][neighs[i].second].best = mountain[coords.first][coords.second].best + 1; q.push(neighs[i]); } } int len = mountain[n - 1][m - 1].best; int down = (len - m - n + 2) / 2; int up = m + n + down - 2; int64_t best = -1; int count = 0; for (int i = 0; i < k; ++i) { int64_t a, b; scanf("%lld %lld", &a, &b); int64_t t = a * up + b * down; if ((best == -1) || (t < best)) { best = t; count = 1; } else if (t == best) { ++count; } } printf("%lld %d\n", best, count); return 0; } |