#include <bits/stdc++.h> using namespace std; const int N = 2005; const int inf = 1e9 + 7; int n, m, k; char B[N][N]; int dp[N][N]; int pv[N][N]; int dx[4] = {-1, 0, 0, 1}; int dy[4] = { 0,-1, 1, 0}; bool inside(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < n; i++) { scanf("%s", B[i]); } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) dp[i][j] = inf; } vector<pair<int, int>> q = {{0, 0}}; dp[0][0] = 0; for(int i = 0; i < (int) q.size(); i++) { int x = q[i].first; int y = q[i].second; for(int d : {0, 1, 2, 3}) { int nx = x + dx[d]; int ny = y + dy[d]; if(inside(nx, ny) && B[nx][ny] == '.' && dp[nx][ny] > dp[x][y] + 1) { dp[nx][ny] = dp[x][y] + 1; pv[nx][ny] = d; q.push_back({nx, ny}); } } } assert(dp[n - 1][m - 1] != inf); int up = 0, dn = 0; int x = n - 1, y = m - 1; while(x != 0 || y != 0) { int d = pv[x][y]; x -= dx[d]; y -= dy[d]; if(dx[d] > 0) up++; if(dy[d] > 0) up++; if(dx[d] < 0) dn++; if(dy[d] < 0) dn++; } long long ans = inf * 1ll * inf; int cnt = 0; for(int i = 0; i < k; i++) { long long a, b; scanf("%lld%lld", &a, &b); if(up * a + dn * b < ans) { ans = up * a + dn * b; cnt = 1; } else if(up * a + dn * b == ans) { cnt++; } } printf("%lld %d\n", ans, cnt); }
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 | #include <bits/stdc++.h> using namespace std; const int N = 2005; const int inf = 1e9 + 7; int n, m, k; char B[N][N]; int dp[N][N]; int pv[N][N]; int dx[4] = {-1, 0, 0, 1}; int dy[4] = { 0,-1, 1, 0}; bool inside(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < n; i++) { scanf("%s", B[i]); } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) dp[i][j] = inf; } vector<pair<int, int>> q = {{0, 0}}; dp[0][0] = 0; for(int i = 0; i < (int) q.size(); i++) { int x = q[i].first; int y = q[i].second; for(int d : {0, 1, 2, 3}) { int nx = x + dx[d]; int ny = y + dy[d]; if(inside(nx, ny) && B[nx][ny] == '.' && dp[nx][ny] > dp[x][y] + 1) { dp[nx][ny] = dp[x][y] + 1; pv[nx][ny] = d; q.push_back({nx, ny}); } } } assert(dp[n - 1][m - 1] != inf); int up = 0, dn = 0; int x = n - 1, y = m - 1; while(x != 0 || y != 0) { int d = pv[x][y]; x -= dx[d]; y -= dy[d]; if(dx[d] > 0) up++; if(dy[d] > 0) up++; if(dx[d] < 0) dn++; if(dy[d] < 0) dn++; } long long ans = inf * 1ll * inf; int cnt = 0; for(int i = 0; i < k; i++) { long long a, b; scanf("%lld%lld", &a, &b); if(up * a + dn * b < ans) { ans = up * a + dn * b; cnt = 1; } else if(up * a + dn * b == ans) { cnt++; } } printf("%lld %d\n", ans, cnt); } |