#include <iostream> #include <queue> const int MAXN = 2020; char map[MAXN][MAXN]; int D[MAXN][MAXN]; std::pair<int,int> DV[4] = { {1,0}, {-1,0}, {0,1}, {0,-1}}; int main() { std::ios_base::sync_with_stdio(0); int n,m,k; std::cin >> n >> m >> k; for (int i=1;i<=n;++i) std::cin >> (map[i]+1); std::queue<std::pair<int,int>> q; q.push({1,1}); map[1][1] = 'D'; while (!q.empty()) { auto [y, x] = q.front(); q.pop(); for (auto [dy, dx] : DV) { if (map[y+dy][x+dx] != '.') continue; map[y+dy][x+dx] = 'D'; D[y+dy][x+dx] = D[y][x]+1; q.push({y+dy, x+dx}); } } int64_t dist = D[n][m]; int64_t base = n+m-2; int64_t extra = (dist-base)/2; int64_t best = 1000000000000000000ll; int count = 0; for (int i=0;i<k;++i) { int64_t a,b; std::cin >> a >> b; int64_t d = base * a + extra * (a+b); if (d < best) { best = d; count = 1; } else if (d == best) ++count; } std::cout << best << " " << count << std::endl; 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 | #include <iostream> #include <queue> const int MAXN = 2020; char map[MAXN][MAXN]; int D[MAXN][MAXN]; std::pair<int,int> DV[4] = { {1,0}, {-1,0}, {0,1}, {0,-1}}; int main() { std::ios_base::sync_with_stdio(0); int n,m,k; std::cin >> n >> m >> k; for (int i=1;i<=n;++i) std::cin >> (map[i]+1); std::queue<std::pair<int,int>> q; q.push({1,1}); map[1][1] = 'D'; while (!q.empty()) { auto [y, x] = q.front(); q.pop(); for (auto [dy, dx] : DV) { if (map[y+dy][x+dx] != '.') continue; map[y+dy][x+dx] = 'D'; D[y+dy][x+dx] = D[y][x]+1; q.push({y+dy, x+dx}); } } int64_t dist = D[n][m]; int64_t base = n+m-2; int64_t extra = (dist-base)/2; int64_t best = 1000000000000000000ll; int count = 0; for (int i=0;i<k;++i) { int64_t a,b; std::cin >> a >> b; int64_t d = base * a + extra * (a+b); if (d < best) { best = d; count = 1; } else if (d == best) ++count; } std::cout << best << " " << count << std::endl; return 0; } |