#include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MM=2013; bool M[MM][MM],vis[MM][MM]; queue< pair<pii,int> > Q; int BFS(const int &n, const int &m) { Q.push({{1,1},0}); while(true) { int x = Q.front().fi.fi, y = Q.front().fi.sc, odl=Q.front().sc; Q.pop(); if(x==n&&y==m) return odl; if(vis[x][y]) continue; vis[x][y]=true; if(x!=1&&vis[x-1][y]==0) Q.push({{x-1,y},odl+1}); if(y!=1&&vis[x][y-1]==0) Q.push({{x,y-1},odl+1}); if(x!=n&&vis[x+1][y]==0) Q.push({{x+1,y},odl+1}); if(y!=m&&vis[x][y+1]==0) Q.push({{x,y+1},odl+1}); } } int main() { ios_base::sync_with_stdio(0); int n,m,k; char z; cin>>n>>m>>k; forn(i,1,n) forn(j,1,m) { cin>>z; if(z=='X') vis[i][j]=true; } int odl = BFS(n,m),cnt=0; ll mn=1000000000000000013,x,y,wyn; int nad = (odl - n - m + 2) / 2; forn(i,1,k) { cin>>x>>y; wyn = x*(n+m-2+nad) + y*nad; if(wyn==mn) cnt++; if(wyn<mn) mn=wyn,cnt=1; } return cout<<mn<<" "<<cnt<<"\n",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 | #include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MM=2013; bool M[MM][MM],vis[MM][MM]; queue< pair<pii,int> > Q; int BFS(const int &n, const int &m) { Q.push({{1,1},0}); while(true) { int x = Q.front().fi.fi, y = Q.front().fi.sc, odl=Q.front().sc; Q.pop(); if(x==n&&y==m) return odl; if(vis[x][y]) continue; vis[x][y]=true; if(x!=1&&vis[x-1][y]==0) Q.push({{x-1,y},odl+1}); if(y!=1&&vis[x][y-1]==0) Q.push({{x,y-1},odl+1}); if(x!=n&&vis[x+1][y]==0) Q.push({{x+1,y},odl+1}); if(y!=m&&vis[x][y+1]==0) Q.push({{x,y+1},odl+1}); } } int main() { ios_base::sync_with_stdio(0); int n,m,k; char z; cin>>n>>m>>k; forn(i,1,n) forn(j,1,m) { cin>>z; if(z=='X') vis[i][j]=true; } int odl = BFS(n,m),cnt=0; ll mn=1000000000000000013,x,y,wyn; int nad = (odl - n - m + 2) / 2; forn(i,1,k) { cin>>x>>y; wyn = x*(n+m-2+nad) + y*nad; if(wyn==mn) cnt++; if(wyn<mn) mn=wyn,cnt=1; } return cout<<mn<<" "<<cnt<<"\n",0; } |