#include <cstdio>
#include <queue>
using namespace std;
queue<pair<int,pair<int,int>>> q;
long long a[1000005], b[1000005];
char t[2005][2005];
bool odw[2005][2005];
int dist[2005][2005];
int n, m, k;
int main(){
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
scanf(" %c", &t[i][j]);
}
t[i][0] = '#';
t[i][m + 1] = '#';
}
for (int j = 1; j <= m; j++){
t[0][j] = '#';
t[n + 1][j] = '#';
}
for (int i = 0; i < k; i++){
scanf("%lld%lld", &a[i], &b[i]);
}
q.push(make_pair(0, make_pair(1, 1)));
odw[1][1] = true;
while (!q.empty()){
auto f = q.front();
q.pop();
int x = f.second.first, y = f.second.second;
dist[x][y] = f.first;
if ((!odw[x - 1][y]) && (t[x - 1][y] != '#') && (t[x - 1][y] != 'X')){
q.push(make_pair(f.first + 1, make_pair(x - 1, y)));
odw[x - 1][y] = true;
}
if ((!odw[x + 1][y]) && (t[x + 1][y] != '#') && (t[x + 1][y] != 'X')){
q.push(make_pair(f.first + 1, make_pair(x + 1, y)));
odw[x + 1][y] = true;
}
if ((!odw[x][y - 1]) && (t[x][y - 1] != '#') && (t[x][y - 1] != 'X')){
q.push(make_pair(f.first + 1, make_pair(x, y - 1)));
odw[x][y - 1] = true;
}
if ((!odw[x][y + 1]) && (t[x][y + 1] != '#') && (t[x][y + 1] != 'X')){
q.push(make_pair(f.first + 1, make_pair(x, y + 1)));
odw[x][y + 1] = true;
}
//printf("pos %d %d, dist %d\n", x, y, dist[x][y]);
}
long long best = 1000000000000000000LL;
int best_c = 0;
int l = dist[n][m] - (m - 1) - (n - 1);
for (int i = 0; i < k; i++){
long long c = ((m - 1) + (n - 1)) * a[i] + l / 2 * (a[i] + b[i]);
if (c < best){
best = c;
best_c = 1;
}
else if (c == best){
best_c++;
}
}
printf("%lld %d\n", best, best_c);
}
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 | #include <cstdio> #include <queue> using namespace std; queue<pair<int,pair<int,int>>> q; long long a[1000005], b[1000005]; char t[2005][2005]; bool odw[2005][2005]; int dist[2005][2005]; int n, m, k; int main(){ scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ scanf(" %c", &t[i][j]); } t[i][0] = '#'; t[i][m + 1] = '#'; } for (int j = 1; j <= m; j++){ t[0][j] = '#'; t[n + 1][j] = '#'; } for (int i = 0; i < k; i++){ scanf("%lld%lld", &a[i], &b[i]); } q.push(make_pair(0, make_pair(1, 1))); odw[1][1] = true; while (!q.empty()){ auto f = q.front(); q.pop(); int x = f.second.first, y = f.second.second; dist[x][y] = f.first; if ((!odw[x - 1][y]) && (t[x - 1][y] != '#') && (t[x - 1][y] != 'X')){ q.push(make_pair(f.first + 1, make_pair(x - 1, y))); odw[x - 1][y] = true; } if ((!odw[x + 1][y]) && (t[x + 1][y] != '#') && (t[x + 1][y] != 'X')){ q.push(make_pair(f.first + 1, make_pair(x + 1, y))); odw[x + 1][y] = true; } if ((!odw[x][y - 1]) && (t[x][y - 1] != '#') && (t[x][y - 1] != 'X')){ q.push(make_pair(f.first + 1, make_pair(x, y - 1))); odw[x][y - 1] = true; } if ((!odw[x][y + 1]) && (t[x][y + 1] != '#') && (t[x][y + 1] != 'X')){ q.push(make_pair(f.first + 1, make_pair(x, y + 1))); odw[x][y + 1] = true; } //printf("pos %d %d, dist %d\n", x, y, dist[x][y]); } long long best = 1000000000000000000LL; int best_c = 0; int l = dist[n][m] - (m - 1) - (n - 1); for (int i = 0; i < k; i++){ long long c = ((m - 1) + (n - 1)) * a[i] + l / 2 * (a[i] + b[i]); if (c < best){ best = c; best_c = 1; } else if (c == best){ best_c++; } } printf("%lld %d\n", best, best_c); } |
English