#include <bits/stdc++.h> using namespace std; char T[2005][2005]; pair<int,int> Wchodz[2005][2005]; queue<pair<int,int> > K; priority_queue<long long> S; int Odw[2005][2005]; int Odl[2005][2005]; pair<int,int> wierz; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int i,j; int n,m,k; cin>>n>>m>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>T[i][j]; } } for(int i=0;i<=n;i++){ T[i][0]=1; } for(int j=0;j<=m;j++){ T[0][j]=1; } K.push({1,1}); while(K.size()>0){ wierz = K.front(); i = wierz.first; j = wierz.second; K.pop(); if(Odw[i][j]==1){ continue; } //cout<<i<<" "<<j<<" "<<Odl[i][j]<<"\n"; Odw[i][j]=1; if(Odw[i+1][j]==0 and T[i+1][j]=='.'){ K.push({i+1,j}); Odl[i+1][j]= Odl[i][j]; } if(Odw[i][j+1]==0 and T[i][j+1]=='.'){ K.push({i,j+1}); Odl[i][j+1]=Odl[i][j]; } if(Odw[i-1][j]==0 and T[i-1][j]=='.'){ K.push({i-1,j}); Odl[i-1][j]=Odl[i][j]+1; } if(Odw[i][j-1]==0 and T[i][j-1]=='.'){ K.push({i,j-1}); Odl[i][j-1]=Odl[i][j]+1; } } long long a,b; long long dol, gor; dol = Odl[n][m]; gor = n+m-2+Odl[n][m]; //cout<<dol; int il =0; long long wyn; long long mi=3000000000000000000; for(int i=0;i<k;i++){ cin>>a>>b; wyn=(a*gor+b*dol); if(wyn<mi){ mi = wyn; il = 1; } else if(wyn==mi){ il++; } } cout<<mi<<" "<<il<<"\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 72 73 74 75 76 77 78 79 80 | #include <bits/stdc++.h> using namespace std; char T[2005][2005]; pair<int,int> Wchodz[2005][2005]; queue<pair<int,int> > K; priority_queue<long long> S; int Odw[2005][2005]; int Odl[2005][2005]; pair<int,int> wierz; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int i,j; int n,m,k; cin>>n>>m>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>T[i][j]; } } for(int i=0;i<=n;i++){ T[i][0]=1; } for(int j=0;j<=m;j++){ T[0][j]=1; } K.push({1,1}); while(K.size()>0){ wierz = K.front(); i = wierz.first; j = wierz.second; K.pop(); if(Odw[i][j]==1){ continue; } //cout<<i<<" "<<j<<" "<<Odl[i][j]<<"\n"; Odw[i][j]=1; if(Odw[i+1][j]==0 and T[i+1][j]=='.'){ K.push({i+1,j}); Odl[i+1][j]= Odl[i][j]; } if(Odw[i][j+1]==0 and T[i][j+1]=='.'){ K.push({i,j+1}); Odl[i][j+1]=Odl[i][j]; } if(Odw[i-1][j]==0 and T[i-1][j]=='.'){ K.push({i-1,j}); Odl[i-1][j]=Odl[i][j]+1; } if(Odw[i][j-1]==0 and T[i][j-1]=='.'){ K.push({i,j-1}); Odl[i][j-1]=Odl[i][j]+1; } } long long a,b; long long dol, gor; dol = Odl[n][m]; gor = n+m-2+Odl[n][m]; //cout<<dol; int il =0; long long wyn; long long mi=3000000000000000000; for(int i=0;i<k;i++){ cin>>a>>b; wyn=(a*gor+b*dol); if(wyn<mi){ mi = wyn; il = 1; } else if(wyn==mi){ il++; } } cout<<mi<<" "<<il<<"\n"; return 0; } |