#include <iostream> #include <queue> using namespace std; char tab[5007][5007]; int dist[5007][5007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin>>n>>m>>k; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin>>tab[i][j]; dist[i][j] = -1; } } queue< pair<int, int> > q; q.push(make_pair(0, 0)); dist[0][0] = 0; while (!q.empty()) { pair<int, int> v = q.front(); q.pop(); if (v.first - 1 >= 0 && tab[v.first - 1][v.second] == '.' && dist[v.first - 1][v.second] == -1) { dist[v.first - 1][v.second] = dist[v.first][v.second] + 1; q.push(make_pair(v.first - 1, v.second)); } if (v.first + 1 < n && tab[v.first + 1][v.second] == '.' && dist[v.first + 1][v.second] == -1) { dist[v.first + 1][v.second] = dist[v.first][v.second] + 1; q.push(make_pair(v.first + 1, v.second)); } if (v.second - 1 >= 0 && tab[v.first][v.second - 1] == '.' && dist[v.first][v.second - 1] == -1) { dist[v.first][v.second - 1] = dist[v.first][v.second] + 1; q.push(make_pair(v.first, v.second - 1)); } if (v.second + 1 < m && tab[v.first][v.second + 1] == '.' && dist[v.first][v.second + 1] == -1) { dist[v.first][v.second + 1] = dist[v.first][v.second] + 1; q.push(make_pair(v.first, v.second + 1)); } } /* for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout<<dist[i][j]<<" "; } cout<<"\n"; }*/ long long minimalny = 1000000000000000000; long long ile = 0; long long konieczne = (long long)(n + m - 2); long long dodatkowe = (long long)(dist[n - 1][m - 1] - konieczne) / 2; for (int i = 0; i < k; i++) { long long a, b; cin>>a>>b; long long wyn = (konieczne + dodatkowe) * a + dodatkowe * b; //cout<<wyn<<endl; if (wyn == minimalny) { ile++; } if (wyn < minimalny) { minimalny = wyn; ile = 1; } } cout<<minimalny<<" "<<ile<<"\n"; }
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 80 81 82 83 | #include <iostream> #include <queue> using namespace std; char tab[5007][5007]; int dist[5007][5007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin>>n>>m>>k; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin>>tab[i][j]; dist[i][j] = -1; } } queue< pair<int, int> > q; q.push(make_pair(0, 0)); dist[0][0] = 0; while (!q.empty()) { pair<int, int> v = q.front(); q.pop(); if (v.first - 1 >= 0 && tab[v.first - 1][v.second] == '.' && dist[v.first - 1][v.second] == -1) { dist[v.first - 1][v.second] = dist[v.first][v.second] + 1; q.push(make_pair(v.first - 1, v.second)); } if (v.first + 1 < n && tab[v.first + 1][v.second] == '.' && dist[v.first + 1][v.second] == -1) { dist[v.first + 1][v.second] = dist[v.first][v.second] + 1; q.push(make_pair(v.first + 1, v.second)); } if (v.second - 1 >= 0 && tab[v.first][v.second - 1] == '.' && dist[v.first][v.second - 1] == -1) { dist[v.first][v.second - 1] = dist[v.first][v.second] + 1; q.push(make_pair(v.first, v.second - 1)); } if (v.second + 1 < m && tab[v.first][v.second + 1] == '.' && dist[v.first][v.second + 1] == -1) { dist[v.first][v.second + 1] = dist[v.first][v.second] + 1; q.push(make_pair(v.first, v.second + 1)); } } /* for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout<<dist[i][j]<<" "; } cout<<"\n"; }*/ long long minimalny = 1000000000000000000; long long ile = 0; long long konieczne = (long long)(n + m - 2); long long dodatkowe = (long long)(dist[n - 1][m - 1] - konieczne) / 2; for (int i = 0; i < k; i++) { long long a, b; cin>>a>>b; long long wyn = (konieczne + dodatkowe) * a + dodatkowe * b; //cout<<wyn<<endl; if (wyn == minimalny) { ile++; } if (wyn < minimalny) { minimalny = wyn; ile = 1; } } cout<<minimalny<<" "<<ile<<"\n"; } |