#include <bits/stdc++.h> using namespace std; int pole[2001][2001]; int g[2001][2001]; int d[2001][2001]; bool p[2001][2001]; int x,y,k; long long marzec; long long minio=4000000000000000; deque <pair <int,int> >Q; string s; int main() { p[0][0]=true; cin>>x>>y>>k; for(int i=0; i<x; i++) { cin>>s; for(int j=0; j<y; j++) { if(s[j]=='X') pole[i][j]=1; } } Q.push_front(make_pair(0,0)); while(!Q.empty()) { int v=Q.front().first; int f=Q.front().second; Q.pop_front(); if(v+1<x) { if(!p[v+1][f]) { if(pole[v+1][f]==0) { Q.push_back(make_pair(v+1,f)); g[v+1][f]=g[v][f]+1; d[v+1][f]=d[v][f]; p[v+1][f]=true; } } } if(f+1<y) { if(!p[v][f+1]) { if(pole[v][f+1]==0) { Q.push_back(make_pair(v,f+1)); g[v][f+1]=g[v][f]+1; d[v][f+1]=d[v][f]; p[v][f+1]=true; } } } if(v-1>=0) { if(!p[v-1][f]) { if(pole[v-1][f]==0) { Q.push_front(make_pair(v-1,f)); d[v-1][f]=d[v][f]+1; g[v-1][f]=g[v][f]; p[v-1][f]=true; } } } if(f-1>=0) { if(!p[v][f-1]) { if(pole[v][f-1]==0) { Q.push_front(make_pair(v,f-1)); d[v][f-1]=g[v][f]+1; g[v][f-1]=g[v][f]; p[v][f-1]=true; } } } } long long dol=g[x-1][y-1]-(x-1)-(y-1); long long gora=g[x-1][y-1]; long long a,b; for(int i=0; i<k; i++) { cin>>a>>b; if(a*gora+b*dol<minio){ minio=a*gora+b*dol; marzec=1; } else if(a*gora+b*dol==minio)marzec++; } cout << minio << ' ' << marzec; 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | #include <bits/stdc++.h> using namespace std; int pole[2001][2001]; int g[2001][2001]; int d[2001][2001]; bool p[2001][2001]; int x,y,k; long long marzec; long long minio=4000000000000000; deque <pair <int,int> >Q; string s; int main() { p[0][0]=true; cin>>x>>y>>k; for(int i=0; i<x; i++) { cin>>s; for(int j=0; j<y; j++) { if(s[j]=='X') pole[i][j]=1; } } Q.push_front(make_pair(0,0)); while(!Q.empty()) { int v=Q.front().first; int f=Q.front().second; Q.pop_front(); if(v+1<x) { if(!p[v+1][f]) { if(pole[v+1][f]==0) { Q.push_back(make_pair(v+1,f)); g[v+1][f]=g[v][f]+1; d[v+1][f]=d[v][f]; p[v+1][f]=true; } } } if(f+1<y) { if(!p[v][f+1]) { if(pole[v][f+1]==0) { Q.push_back(make_pair(v,f+1)); g[v][f+1]=g[v][f]+1; d[v][f+1]=d[v][f]; p[v][f+1]=true; } } } if(v-1>=0) { if(!p[v-1][f]) { if(pole[v-1][f]==0) { Q.push_front(make_pair(v-1,f)); d[v-1][f]=d[v][f]+1; g[v-1][f]=g[v][f]; p[v-1][f]=true; } } } if(f-1>=0) { if(!p[v][f-1]) { if(pole[v][f-1]==0) { Q.push_front(make_pair(v,f-1)); d[v][f-1]=g[v][f]+1; g[v][f-1]=g[v][f]; p[v][f-1]=true; } } } } long long dol=g[x-1][y-1]-(x-1)-(y-1); long long gora=g[x-1][y-1]; long long a,b; for(int i=0; i<k; i++) { cin>>a>>b; if(a*gora+b*dol<minio){ minio=a*gora+b*dol; marzec=1; } else if(a*gora+b*dol==minio)marzec++; } cout << minio << ' ' << marzec; return 0; } |