#include <bits/stdc++.h> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } char t[2048][2048]; int dist[2048][2048]; int h, w, k; int getDist() { h = nextInt(); w = nextInt(); k = nextInt(); for (int r=0; r<h; ++r) scanf("%s", t[r]); for (int r=0; r<h; ++r) for (int c=0; c<w; ++c) dist[r][c] = -1; dist[0][0] = 0; queue<pair<int, int> > q; q.push(make_pair(0, 0)); int dr[] = { -1, 0, 0, 1 }; int dc[] = { 0, -1, 1, 0 }; while (!q.empty()) { int r = q.front().first; int c = q.front().second; q.pop(); for (int i=0; i<4; ++i) { int rr = r + dr[i]; if (rr < 0 || rr >= h) continue; int cc = c + dc[i]; if (cc < 0 || cc >= w) continue; if (t[rr][cc] != '.') continue; if (dist[rr][cc] >= 0) continue; dist[rr][cc] = 1 + dist[r][c]; if (rr == h-1 && cc == w-1) return dist[rr][cc]; q.push(make_pair(rr, cc)); } } return -1; } int main() { int dd = getDist(); int steps = h + w - 2; long long cntUp = (dd + steps) / 2; long long cntDown = (dd - steps) / 2; long long bestTime = 999999999999999999LL; int bestCount = 0; for (int i=0; i<k; ++i) { long long userTime = cntUp * nextInt(); userTime += cntDown * nextInt(); if (userTime < bestTime) { bestTime = userTime; bestCount = 0; } if (userTime == bestTime) ++bestCount; } printf("%lld %d\n", bestTime, bestCount); 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 | #include <bits/stdc++.h> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } char t[2048][2048]; int dist[2048][2048]; int h, w, k; int getDist() { h = nextInt(); w = nextInt(); k = nextInt(); for (int r=0; r<h; ++r) scanf("%s", t[r]); for (int r=0; r<h; ++r) for (int c=0; c<w; ++c) dist[r][c] = -1; dist[0][0] = 0; queue<pair<int, int> > q; q.push(make_pair(0, 0)); int dr[] = { -1, 0, 0, 1 }; int dc[] = { 0, -1, 1, 0 }; while (!q.empty()) { int r = q.front().first; int c = q.front().second; q.pop(); for (int i=0; i<4; ++i) { int rr = r + dr[i]; if (rr < 0 || rr >= h) continue; int cc = c + dc[i]; if (cc < 0 || cc >= w) continue; if (t[rr][cc] != '.') continue; if (dist[rr][cc] >= 0) continue; dist[rr][cc] = 1 + dist[r][c]; if (rr == h-1 && cc == w-1) return dist[rr][cc]; q.push(make_pair(rr, cc)); } } return -1; } int main() { int dd = getDist(); int steps = h + w - 2; long long cntUp = (dd + steps) / 2; long long cntDown = (dd - steps) / 2; long long bestTime = 999999999999999999LL; int bestCount = 0; for (int i=0; i<k; ++i) { long long userTime = cntUp * nextInt(); userTime += cntDown * nextInt(); if (userTime < bestTime) { bestTime = userTime; bestCount = 0; } if (userTime == bestTime) ++bestCount; } printf("%lld %d\n", bestTime, bestCount); return 0; } |