#include<cstdio> #include<vector> #include<queue> #include<utility> typedef std::pair<int, int> pii; typedef std::pair<pii, int> pp; typedef std::queue<pp> qpp; typedef long long ll; int n, m, k; bool visited[2001][2001]; char t[2001][2001]; int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void input() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < n; i++) { scanf("%s", t[i]); } } pii next(pii & c, int i) { return std::make_pair(c.first + d[i][0], c.second + d[i][1]); } bool valid(pii & c) { return c.first >= 0 && c.first < n && c.second >= 0 && c.second < m && !visited[c.first][c.second] && t[c.first][c.second] == '.'; } int len() { qpp q; q.push(std::make_pair(std::make_pair(0, 0), 0)); visited[0][0] = true; while (true) { pp p = q.front(); q.pop(); if (p.first.first == n - 1 && p.first.second == m - 1) { return p.second; } pii c = p.first; int x = c.first; int y = c.second; int x1 = x - 1; int new_count = p.second + 1; if (x1 >= 0 && !visited[x1][y] && t[x1][y] == '.') { q.push(std::make_pair(std::make_pair(x1, y), new_count)); visited[x1][y] = true; } int x2 = x + 1; if (x2 < n && !visited[x2][y] && t[x2][y] == '.') { q.push(std::make_pair(std::make_pair(x2, y), new_count)); visited[x2][y] = true; } int y1 = y - 1; if (y1 >= 0 && !visited[x][y1] && t[x][y1] == '.') { q.push(std::make_pair(std::make_pair(x, y1), new_count)); visited[x][y1] = true; } int y2 = y + 1; if (y2 < m && !visited[x][y2] && t[x][y2] == '.') { q.push(std::make_pair(std::make_pair(x, y2), new_count)); visited[x][y2] = true; } } return 0; } ll best = 1e18; int count = 0; void solve(int p) { ll l = n + m - 2; ll lk = (p - l) / 2; ll cu = l + lk; ll cd = lk; int du, dd; // printf("path: %lld %lld\n", cu, cd); for (int i = 0; i < k; i++) { scanf("%d %d", &du, &dd); ll cost = cu * du + cd * dd; //printf("%lld\n", cost); if (cost < best) { best = cost; count = 1; } else if (cost == best) { count ++; } } } void print() { printf("%lld %d\n", best, count); } int main() { input(); int l = len(); solve(l); print(); 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 | #include<cstdio> #include<vector> #include<queue> #include<utility> typedef std::pair<int, int> pii; typedef std::pair<pii, int> pp; typedef std::queue<pp> qpp; typedef long long ll; int n, m, k; bool visited[2001][2001]; char t[2001][2001]; int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void input() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < n; i++) { scanf("%s", t[i]); } } pii next(pii & c, int i) { return std::make_pair(c.first + d[i][0], c.second + d[i][1]); } bool valid(pii & c) { return c.first >= 0 && c.first < n && c.second >= 0 && c.second < m && !visited[c.first][c.second] && t[c.first][c.second] == '.'; } int len() { qpp q; q.push(std::make_pair(std::make_pair(0, 0), 0)); visited[0][0] = true; while (true) { pp p = q.front(); q.pop(); if (p.first.first == n - 1 && p.first.second == m - 1) { return p.second; } pii c = p.first; int x = c.first; int y = c.second; int x1 = x - 1; int new_count = p.second + 1; if (x1 >= 0 && !visited[x1][y] && t[x1][y] == '.') { q.push(std::make_pair(std::make_pair(x1, y), new_count)); visited[x1][y] = true; } int x2 = x + 1; if (x2 < n && !visited[x2][y] && t[x2][y] == '.') { q.push(std::make_pair(std::make_pair(x2, y), new_count)); visited[x2][y] = true; } int y1 = y - 1; if (y1 >= 0 && !visited[x][y1] && t[x][y1] == '.') { q.push(std::make_pair(std::make_pair(x, y1), new_count)); visited[x][y1] = true; } int y2 = y + 1; if (y2 < m && !visited[x][y2] && t[x][y2] == '.') { q.push(std::make_pair(std::make_pair(x, y2), new_count)); visited[x][y2] = true; } } return 0; } ll best = 1e18; int count = 0; void solve(int p) { ll l = n + m - 2; ll lk = (p - l) / 2; ll cu = l + lk; ll cd = lk; int du, dd; // printf("path: %lld %lld\n", cu, cd); for (int i = 0; i < k; i++) { scanf("%d %d", &du, &dd); ll cost = cu * du + cd * dd; //printf("%lld\n", cost); if (cost < best) { best = cost; count = 1; } else if (cost == best) { count ++; } } } void print() { printf("%lld %d\n", best, count); } int main() { input(); int l = len(); solve(l); print(); return 0; } |