#include <algorithm> #include <cstdio> #include <vector> #include <set> #include <queue> using namespace std; long a[2004][2004]; struct Point { long x; long y; }; int main() { long n, m, k; char c; scanf("%ld%ld%ld", &n, &m, &k); for (long i = 1; i <= n; ++i) { a[i][0] = a[i][m + 1] = -1; for (long j = 1; j <= m; ++j) { scanf(" %c", &c); a[i][j] = (c == '.') ? 0 : -1; } } for (long j = 1; j <= m; ++j) { a[0][j] = a[n + 1][j] = -1; } queue<Point> q; Point p = {1, 1}; q.push(p); a[1][1] = -1; long len = 0; while(a[n][m] == 0) { len++; for (long i = q.size(); i > 0; i--) { p = q.front(); q.pop(); if (a[p.x][p.y + 1] == 0) { a[p.x][p.y + 1] = -1; q.push(Point{p.x, p.y + 1}); } if (a[p.x + 1][p.y] == 0) { a[p.x + 1][p.y] = -1; q.push(Point{p.x + 1, p.y}); } if (a[p.x][p.y - 1] == 0) { a[p.x][p.y - 1] = -1; q.push(Point{p.x, p.y - 1}); } if (a[p.x -1][p.y] == 0) { a[p.x - 1][p.y] = -1; q.push(Point{p.x - 1, p.y}); } } } long long f, b, res=__LONG_LONG_MAX__, r, resCount = 0, lenB = (len + 2 - m - n) / 2, lenF = len - lenB; for (long i = 0; i < k; i++) { scanf("%lld%lld", &f, &b); r = f*lenF + b*lenB; if (res > r) { res = r; resCount = 1; } else if (res == r) { resCount++; } } printf("%lld %lld", res, resCount); 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 | #include <algorithm> #include <cstdio> #include <vector> #include <set> #include <queue> using namespace std; long a[2004][2004]; struct Point { long x; long y; }; int main() { long n, m, k; char c; scanf("%ld%ld%ld", &n, &m, &k); for (long i = 1; i <= n; ++i) { a[i][0] = a[i][m + 1] = -1; for (long j = 1; j <= m; ++j) { scanf(" %c", &c); a[i][j] = (c == '.') ? 0 : -1; } } for (long j = 1; j <= m; ++j) { a[0][j] = a[n + 1][j] = -1; } queue<Point> q; Point p = {1, 1}; q.push(p); a[1][1] = -1; long len = 0; while(a[n][m] == 0) { len++; for (long i = q.size(); i > 0; i--) { p = q.front(); q.pop(); if (a[p.x][p.y + 1] == 0) { a[p.x][p.y + 1] = -1; q.push(Point{p.x, p.y + 1}); } if (a[p.x + 1][p.y] == 0) { a[p.x + 1][p.y] = -1; q.push(Point{p.x + 1, p.y}); } if (a[p.x][p.y - 1] == 0) { a[p.x][p.y - 1] = -1; q.push(Point{p.x, p.y - 1}); } if (a[p.x -1][p.y] == 0) { a[p.x - 1][p.y] = -1; q.push(Point{p.x - 1, p.y}); } } } long long f, b, res=__LONG_LONG_MAX__, r, resCount = 0, lenB = (len + 2 - m - n) / 2, lenF = len - lenB; for (long i = 0; i < k; i++) { scanf("%lld%lld", &f, &b); r = f*lenF + b*lenB; if (res > r) { res = r; resCount = 1; } else if (res == r) { resCount++; } } printf("%lld %lld", res, resCount); return 0; } |