#include<bits/stdc++.h> using namespace std; int n,m,k; bool mapa[2005][2005]; int odl[2005][2005]; pair<int, int> v[1000005]; long long czas[1000005]; void wczytaj() { cin >> n >> m >> k; for(int i = 1; i <= n; i++) { string s; cin >> s; for(int j = 0; j < (int)s.size(); j++) { if(s[j] == '.') mapa[i][j+1] = 1; } } for(int i = 1; i <= k; i++) { cin >> v[i].first >> v[i].second; } } void bfs() { queue<pair<int, int> > kolej; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) odl[i][j] = 1000000009; odl[1][1] = 0; kolej.push(make_pair(1,1)); while(!kolej.empty()) { auto w = kolej.front(); kolej.pop(); if(w.first > 1 && odl[w.first-1][w.second] > odl[w.first][w.second]+1 && mapa[w.first-1][w.second] == 1) { odl[w.first-1][w.second] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first-1,w.second)); } if(w.second < m && odl[w.first][w.second+1] > odl[w.first][w.second]+1 && mapa[w.first][w.second+1] == 1) { odl[w.first][w.second+1] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first,w.second+1)); } if(w.second > 1 && odl[w.first][w.second-1] > odl[w.first][w.second]+1 && mapa[w.first][w.second-1] == 1) { odl[w.first][w.second-1] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first,w.second-1)); } if(w.first < n && odl[w.first+1][w.second] > odl[w.first][w.second]+1 && mapa[w.first+1][w.second] == 1) { odl[w.first+1][w.second] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first+1,w.second)); } } } void licz() { int opt = 0; int wynik = 0; czas[0] = 1000000000000000005; int roznica = odl[n][m] - n - m + 2; int ruchyg = n+m-2 + (roznica/2); int ruchyd = (roznica/2); for(int i = 1; i <= k; i++) { czas[i] = (long long)v[i].first*ruchyg + (long long)v[i].second*ruchyd; if(czas[i] < czas[opt]) opt = i; } for(int i = 1; i <= k; i++) if(czas[opt] == czas[i]) wynik++; cout << czas[opt] << " " << wynik; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); wczytaj(); bfs(); licz(); }
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 84 85 86 87 88 | #include<bits/stdc++.h> using namespace std; int n,m,k; bool mapa[2005][2005]; int odl[2005][2005]; pair<int, int> v[1000005]; long long czas[1000005]; void wczytaj() { cin >> n >> m >> k; for(int i = 1; i <= n; i++) { string s; cin >> s; for(int j = 0; j < (int)s.size(); j++) { if(s[j] == '.') mapa[i][j+1] = 1; } } for(int i = 1; i <= k; i++) { cin >> v[i].first >> v[i].second; } } void bfs() { queue<pair<int, int> > kolej; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) odl[i][j] = 1000000009; odl[1][1] = 0; kolej.push(make_pair(1,1)); while(!kolej.empty()) { auto w = kolej.front(); kolej.pop(); if(w.first > 1 && odl[w.first-1][w.second] > odl[w.first][w.second]+1 && mapa[w.first-1][w.second] == 1) { odl[w.first-1][w.second] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first-1,w.second)); } if(w.second < m && odl[w.first][w.second+1] > odl[w.first][w.second]+1 && mapa[w.first][w.second+1] == 1) { odl[w.first][w.second+1] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first,w.second+1)); } if(w.second > 1 && odl[w.first][w.second-1] > odl[w.first][w.second]+1 && mapa[w.first][w.second-1] == 1) { odl[w.first][w.second-1] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first,w.second-1)); } if(w.first < n && odl[w.first+1][w.second] > odl[w.first][w.second]+1 && mapa[w.first+1][w.second] == 1) { odl[w.first+1][w.second] = odl[w.first][w.second]+1; kolej.push(make_pair(w.first+1,w.second)); } } } void licz() { int opt = 0; int wynik = 0; czas[0] = 1000000000000000005; int roznica = odl[n][m] - n - m + 2; int ruchyg = n+m-2 + (roznica/2); int ruchyd = (roznica/2); for(int i = 1; i <= k; i++) { czas[i] = (long long)v[i].first*ruchyg + (long long)v[i].second*ruchyd; if(czas[i] < czas[opt]) opt = i; } for(int i = 1; i <= k; i++) if(czas[opt] == czas[i]) wynik++; cout << czas[opt] << " " << wynik; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); wczytaj(); bfs(); licz(); } |