#include <bits/stdc++.h>
using namespace std;
long long n,m,k,r,a,b,mw=1e18,lw,w,lx,ly,ld;
bool o[2001][2001];
struct point{
int x;
int y;
int d;
};
queue<point> kol;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
cin>>n>>m>>k;
char z;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>z;
if(z=='X'){
o[j+1][i+1]=1;
}
}
}
kol.push({1,1,0});
o[1][1]=1;
while(true){
//cout<<kol.front().x<<" "<<kol.front().y<<" "<<kol.front().d<<endl;
lx=kol.front().x;
ly=kol.front().y;
ld=kol.front().d;
if(lx==m&&ly==n){
r=ld;
break;
}
if(lx>1){
if(!o[lx-1][ly]){
kol.push({lx-1,ly,ld+1});
o[lx-1][ly]=1;
}
}
if(lx<m){
if(!o[lx+1][ly]){
kol.push({lx+1,ly,ld+1});
o[lx+1][ly]=1;
}
}
if(ly>1){
if(!o[lx][ly-1]){
kol.push({lx,ly-1,ld+1});
o[lx][ly-1]=1;
}
}
if(ly<n){
if(!o[lx][ly+1]){
kol.push({lx,ly+1,ld+1});
o[lx][ly+1]=1;
}
}
kol.pop();
}
//cout<<r<<endl;
for(int i=0; i<k; i++){
cin>>a>>b;
w=a*(n+m-2)+(a+b)*(r+2-n-m)/2;
if(w<mw){
mw=w;
lw=1;
}else if(w==mw){
lw++;
}
}
cout<<mw<<" "<<lw;
}
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 | #include <bits/stdc++.h> using namespace std; long long n,m,k,r,a,b,mw=1e18,lw,w,lx,ly,ld; bool o[2001][2001]; struct point{ int x; int y; int d; }; queue<point> kol; int main() { ios_base::sync_with_stdio(false); cin.tie(); cin>>n>>m>>k; char z; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ cin>>z; if(z=='X'){ o[j+1][i+1]=1; } } } kol.push({1,1,0}); o[1][1]=1; while(true){ //cout<<kol.front().x<<" "<<kol.front().y<<" "<<kol.front().d<<endl; lx=kol.front().x; ly=kol.front().y; ld=kol.front().d; if(lx==m&&ly==n){ r=ld; break; } if(lx>1){ if(!o[lx-1][ly]){ kol.push({lx-1,ly,ld+1}); o[lx-1][ly]=1; } } if(lx<m){ if(!o[lx+1][ly]){ kol.push({lx+1,ly,ld+1}); o[lx+1][ly]=1; } } if(ly>1){ if(!o[lx][ly-1]){ kol.push({lx,ly-1,ld+1}); o[lx][ly-1]=1; } } if(ly<n){ if(!o[lx][ly+1]){ kol.push({lx,ly+1,ld+1}); o[lx][ly+1]=1; } } kol.pop(); } //cout<<r<<endl; for(int i=0; i<k; i++){ cin>>a>>b; w=a*(n+m-2)+(a+b)*(r+2-n-m)/2; if(w<mw){ mw=w; lw=1; }else if(w==mw){ lw++; } } cout<<mw<<" "<<lw; } |
English