#include <iostream> #include <cstdio> using namespace std; int n, m, k, b, e, x, y, ile; int BFSx[4000005]; int BFSy[4000005]; int odl[2005][2005]; bool czy[2005][2005]; char tab[2005][2005]; long long A, B, X, Y, MIN; long long res[1000005]; int main () { scanf("%d%d%d\n", &n, &m, &k); for (int i = 0; i < n; ++i) scanf("%s", tab[i]); for (int i = n; i > 0; --i) for (int j = m; j > 0; --j) tab[i][j] = tab[i - 1][j - 1]; for (int j = 0; j <= m; ++j) tab[0][j] = tab[n + 1][j] = 'X'; for (int i = 0; i <= n; ++i) tab[i][0] = tab[i][m + 1] = 'X'; czy[1][1] = 1; BFSx[0] = BFSy[0] = 1; while (b <= e) { x = BFSx[b]; y = BFSy[b]; if (czy[x - 1][y] == 0 && tab[x - 1][y] == '.') { e++; BFSx[e] = x - 1; BFSy[e] = y; czy[x - 1][y] = 1; odl[x - 1][y] = odl[x][y] + 1; } if (czy[x + 1][y] == 0 && tab[x + 1][y] == '.') { e++; BFSx[e] = x + 1; BFSy[e] = y; czy[x + 1][y] = 1; odl[x + 1][y] = odl[x][y] + 1; } if (czy[x][y - 1] == 0 && tab[x][y - 1] == '.') { e++; BFSx[e] = x; BFSy[e] = y - 1; czy[x][y - 1] = 1; odl[x][y - 1] = odl[x][y] + 1; } if (czy[x][y + 1] == 0 && tab[x][y + 1] == '.') { e++; BFSx[e] = x; BFSy[e] = y + 1; czy[x][y + 1] = 1; odl[x][y + 1] = odl[x][y] + 1; } b++; } A = (long long)((n + m - 2 + odl[n][m]) / 2); B = (long long)((odl[n][m] - n - m + 2) / 2); MIN = 1000000000000000000LL; for (int i = 0; i < k; ++i) { scanf("%lld%lld", &X, &Y); res[i] = A * X + B * Y; if (res[i] < MIN) MIN = res[i]; } for (int i = 0; i < k; ++i) if (res[i] == MIN) ile++; printf("%lld %d\n", MIN, ile); 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 | #include <iostream> #include <cstdio> using namespace std; int n, m, k, b, e, x, y, ile; int BFSx[4000005]; int BFSy[4000005]; int odl[2005][2005]; bool czy[2005][2005]; char tab[2005][2005]; long long A, B, X, Y, MIN; long long res[1000005]; int main () { scanf("%d%d%d\n", &n, &m, &k); for (int i = 0; i < n; ++i) scanf("%s", tab[i]); for (int i = n; i > 0; --i) for (int j = m; j > 0; --j) tab[i][j] = tab[i - 1][j - 1]; for (int j = 0; j <= m; ++j) tab[0][j] = tab[n + 1][j] = 'X'; for (int i = 0; i <= n; ++i) tab[i][0] = tab[i][m + 1] = 'X'; czy[1][1] = 1; BFSx[0] = BFSy[0] = 1; while (b <= e) { x = BFSx[b]; y = BFSy[b]; if (czy[x - 1][y] == 0 && tab[x - 1][y] == '.') { e++; BFSx[e] = x - 1; BFSy[e] = y; czy[x - 1][y] = 1; odl[x - 1][y] = odl[x][y] + 1; } if (czy[x + 1][y] == 0 && tab[x + 1][y] == '.') { e++; BFSx[e] = x + 1; BFSy[e] = y; czy[x + 1][y] = 1; odl[x + 1][y] = odl[x][y] + 1; } if (czy[x][y - 1] == 0 && tab[x][y - 1] == '.') { e++; BFSx[e] = x; BFSy[e] = y - 1; czy[x][y - 1] = 1; odl[x][y - 1] = odl[x][y] + 1; } if (czy[x][y + 1] == 0 && tab[x][y + 1] == '.') { e++; BFSx[e] = x; BFSy[e] = y + 1; czy[x][y + 1] = 1; odl[x][y + 1] = odl[x][y] + 1; } b++; } A = (long long)((n + m - 2 + odl[n][m]) / 2); B = (long long)((odl[n][m] - n - m + 2) / 2); MIN = 1000000000000000000LL; for (int i = 0; i < k; ++i) { scanf("%lld%lld", &X, &Y); res[i] = A * X + B * Y; if (res[i] < MIN) MIN = res[i]; } for (int i = 0; i < k; ++i) if (res[i] == MIN) ile++; printf("%lld %d\n", MIN, ile); return 0; } |