#include <bits/stdc++.h> using namespace std; typedef long long ll; #ifdef LOCAL #define debug(...) __VA_ARGS__ #else #define debug(...) {} #endif const int INF = 2137213722; bool vis[2001][2001]; ll dist[2001][2001]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); debug(freopen("wyc1.in","r",stdin);); int i; ll n,m,k; cin>>n>>m>>k; for (i = 0; i < n ; i++) for (int j = 0; j < m ; j++) dist[i][j] = INF; for (i = 0; i < n ; i++){ string s; cin>>s; for (int j = 0; j < m ; j++) if (s[j] == 'X') vis[i][j] = 1; } deque<pair<int,int> > Q; dist[0][0] = 0; Q.push_back({0,0}); while (Q.size()){ int a = Q.front().first; int b = Q.front().second; //cout<<a<<" "<<b<<" "<<dist[a][b]<<"\n"; vis[a][b] = 1; Q.pop_front(); if (a && !vis[a-1][b] && dist[a-1][b] > dist[a][b]+1){ dist[a-1][b] = dist[a][b]+1; Q.push_back({a-1,b}); } if (b && !vis[a][b-1] && dist[a][b-1] > dist[a][b]+1){ dist[a][b-1] = dist[a][b]+1; Q.push_back({a,b-1}); } if (a < n-1 && !vis[a+1][b] && dist[a+1][b] > dist[a][b]){ vis[a+1][b] = 1; dist[a+1][b] = dist[a][b]; Q.push_front({a+1,b}); } if (b < m-1 && !vis[a][b+1] && dist[a][b+1] > dist[a][b]){ dist[a][b+1] = dist[a][b]; Q.push_front({a,b+1}); } } /*/for (i = 0; i < n; i++){ for (int j = 0; j < m ; j++) cout<<dist[i][j]<<" "; cout<<"\n"; }/*/ ll wy = 213721372137213722; int count = 0; while (k--){ ll a,b; cin>>a>>b; ll akt = a*(n+m-2)+(a+b)*dist[n-1][m-1]; //cout<<akt<<"\n"; if (akt < wy){ wy = akt; count = 1; }else if (akt == wy) count++; } cout<<wy<<" "<<count<<"\n"; 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #ifdef LOCAL #define debug(...) __VA_ARGS__ #else #define debug(...) {} #endif const int INF = 2137213722; bool vis[2001][2001]; ll dist[2001][2001]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); debug(freopen("wyc1.in","r",stdin);); int i; ll n,m,k; cin>>n>>m>>k; for (i = 0; i < n ; i++) for (int j = 0; j < m ; j++) dist[i][j] = INF; for (i = 0; i < n ; i++){ string s; cin>>s; for (int j = 0; j < m ; j++) if (s[j] == 'X') vis[i][j] = 1; } deque<pair<int,int> > Q; dist[0][0] = 0; Q.push_back({0,0}); while (Q.size()){ int a = Q.front().first; int b = Q.front().second; //cout<<a<<" "<<b<<" "<<dist[a][b]<<"\n"; vis[a][b] = 1; Q.pop_front(); if (a && !vis[a-1][b] && dist[a-1][b] > dist[a][b]+1){ dist[a-1][b] = dist[a][b]+1; Q.push_back({a-1,b}); } if (b && !vis[a][b-1] && dist[a][b-1] > dist[a][b]+1){ dist[a][b-1] = dist[a][b]+1; Q.push_back({a,b-1}); } if (a < n-1 && !vis[a+1][b] && dist[a+1][b] > dist[a][b]){ vis[a+1][b] = 1; dist[a+1][b] = dist[a][b]; Q.push_front({a+1,b}); } if (b < m-1 && !vis[a][b+1] && dist[a][b+1] > dist[a][b]){ dist[a][b+1] = dist[a][b]; Q.push_front({a,b+1}); } } /*/for (i = 0; i < n; i++){ for (int j = 0; j < m ; j++) cout<<dist[i][j]<<" "; cout<<"\n"; }/*/ ll wy = 213721372137213722; int count = 0; while (k--){ ll a,b; cin>>a>>b; ll akt = a*(n+m-2)+(a+b)*dist[n-1][m-1]; //cout<<akt<<"\n"; if (akt < wy){ wy = akt; count = 1; }else if (akt == wy) count++; } cout<<wy<<" "<<count<<"\n"; return 0; } |