#include <iostream> #include <stdio.h> #include <queue> #define INF 1000000000000000000 #define ff first #define ss second using namespace std; int r[2002][2002]; int n,m,x,y,k; long long h,res1=INF,res2; queue <pair <int,int> > q; int main() { scanf("%d%d%d",&n,&m,&k); getchar(); for(int i=0;i<=max(n,m)+1;i++){ r[i][0]=-1; r[0][i]=-1; r[i][m+1]=-1; r[n+1][i]=-1; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(getchar()=='X') r[i][j]=-1; } getchar(); } r[1][1]=1; q.push(make_pair(1,1)); while(!q.empty()){ x=q.front().ff; y=q.front().ss; q.pop(); if(!r[x+1][y]){ r[x+1][y]=r[x][y]+1; q.push(make_pair(x+1,y)); } if(!r[x-1][y]){ r[x-1][y]=r[x][y]+1; q.push(make_pair(x-1,y)); } if(!r[x][y+1]){ r[x][y+1]=r[x][y]+1; q.push(make_pair(x,y+1)); } if(!r[x][y-1]){ r[x][y-1]=r[x][y]+1; q.push(make_pair(x,y-1)); } } y=(r[n][m]-n-m+1)>>1; x=y+n+m-2; for(int i=0;i<k;i++){ scanf("%d%d",&n,&m); h=(long long)n*(long long)x+(long long)m*(long long)y; if(h<res1){ res1=h; res2=1; }else if(h==res1) res2++; } printf("%lld %lld",res1,res2); 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 | #include <iostream> #include <stdio.h> #include <queue> #define INF 1000000000000000000 #define ff first #define ss second using namespace std; int r[2002][2002]; int n,m,x,y,k; long long h,res1=INF,res2; queue <pair <int,int> > q; int main() { scanf("%d%d%d",&n,&m,&k); getchar(); for(int i=0;i<=max(n,m)+1;i++){ r[i][0]=-1; r[0][i]=-1; r[i][m+1]=-1; r[n+1][i]=-1; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(getchar()=='X') r[i][j]=-1; } getchar(); } r[1][1]=1; q.push(make_pair(1,1)); while(!q.empty()){ x=q.front().ff; y=q.front().ss; q.pop(); if(!r[x+1][y]){ r[x+1][y]=r[x][y]+1; q.push(make_pair(x+1,y)); } if(!r[x-1][y]){ r[x-1][y]=r[x][y]+1; q.push(make_pair(x-1,y)); } if(!r[x][y+1]){ r[x][y+1]=r[x][y]+1; q.push(make_pair(x,y+1)); } if(!r[x][y-1]){ r[x][y-1]=r[x][y]+1; q.push(make_pair(x,y-1)); } } y=(r[n][m]-n-m+1)>>1; x=y+n+m-2; for(int i=0;i<k;i++){ scanf("%d%d",&n,&m); h=(long long)n*(long long)x+(long long)m*(long long)y; if(h<res1){ res1=h; res2=1; }else if(h==res1) res2++; } printf("%lld %lld",res1,res2); return 0; } |