#include <cstdio> const int inf = 1000000000; const int max_n = 2100; char T[max_n][max_n]; int dist[max_n][max_n]; int n, m, k; int start, stop; int queue[max_n * max_n][2]; int main() { scanf ("%d %d %d", &n, &m, &k); for (int i = 0; i < n; ++i) scanf("%s", T[i]); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { dist[i][j] = inf; } } start = 0; stop = 1; queue[0][0] = 0; queue[0][1] = 0; dist[0][0] = 0; while (start < stop) { int x = queue[start][0]; int y = queue[start][1]; ++start; if (x > 0 && T[x - 1][y] == '.' && dist[x - 1][y] == inf) { queue[stop][0] = x - 1; queue[stop][1] = y; dist[x - 1][y] = 1 + dist[x][y]; ++stop; } if (x < n - 1 && T[x + 1][y] == '.' && dist[x + 1][y] == inf) { queue[stop][0] = x + 1; queue[stop][1] = y; dist[x + 1][y] = 1 + dist[x][y]; ++stop; } if (y > 0 && T[x][y - 1] == '.' && dist[x][y - 1] == inf) { queue[stop][0] = x; queue[stop][1] = y - 1; dist[x][y - 1] = 1 + dist[x][y]; ++stop; } if (y < m - 1 && T[x][y + 1] == '.' && dist[x][y + 1] == inf) { queue[stop][0] = x; queue[stop][1] = y + 1; dist[x][y + 1] = 1 + dist[x][y]; ++stop; } } long long down = (dist[n - 1][m - 1] - (n - 1) - (m - 1)) / 2; long long up = n - 1 + m - 1 + down; long long cost = 1000000000000000000LL; int cnt = 0; for (int i = 0; i < k; ++i) { int a, b; scanf ("%d %d", &a, &b); if (a * up + b * down < cost) { cost = a * up + b * down; cnt = 1; } else if (a * up + b * down == cost) { ++cnt; } } printf ("%lld %d\n", cost, cnt); 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 | #include <cstdio> const int inf = 1000000000; const int max_n = 2100; char T[max_n][max_n]; int dist[max_n][max_n]; int n, m, k; int start, stop; int queue[max_n * max_n][2]; int main() { scanf ("%d %d %d", &n, &m, &k); for (int i = 0; i < n; ++i) scanf("%s", T[i]); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { dist[i][j] = inf; } } start = 0; stop = 1; queue[0][0] = 0; queue[0][1] = 0; dist[0][0] = 0; while (start < stop) { int x = queue[start][0]; int y = queue[start][1]; ++start; if (x > 0 && T[x - 1][y] == '.' && dist[x - 1][y] == inf) { queue[stop][0] = x - 1; queue[stop][1] = y; dist[x - 1][y] = 1 + dist[x][y]; ++stop; } if (x < n - 1 && T[x + 1][y] == '.' && dist[x + 1][y] == inf) { queue[stop][0] = x + 1; queue[stop][1] = y; dist[x + 1][y] = 1 + dist[x][y]; ++stop; } if (y > 0 && T[x][y - 1] == '.' && dist[x][y - 1] == inf) { queue[stop][0] = x; queue[stop][1] = y - 1; dist[x][y - 1] = 1 + dist[x][y]; ++stop; } if (y < m - 1 && T[x][y + 1] == '.' && dist[x][y + 1] == inf) { queue[stop][0] = x; queue[stop][1] = y + 1; dist[x][y + 1] = 1 + dist[x][y]; ++stop; } } long long down = (dist[n - 1][m - 1] - (n - 1) - (m - 1)) / 2; long long up = n - 1 + m - 1 + down; long long cost = 1000000000000000000LL; int cnt = 0; for (int i = 0; i < k; ++i) { int a, b; scanf ("%d %d", &a, &b); if (a * up + b * down < cost) { cost = a * up + b * down; cnt = 1; } else if (a * up + b * down == cost) { ++cnt; } } printf ("%lld %d\n", cost, cnt); return 0; } |