#include <bits/stdc++.h>
using namespace std;
const int SIZE=2300;
const int vx[]={0, 1, 0, -1};
const int vy[]={1, 0, -1, 0};
void imax(int &a, int b){
a=max(a, b);
}
void imin(int &a, int b){
a=min(a, b);
}
void lmax(long long &a, long long b){
a=max(a, b);
}
void lmin(long long &a, long long b){
a=min(a, b);
}
/*
WARNING: I'm using strange bracket style!
*/
queue <pair <int, int> > Q;
map <long long, int> M;
int dist[SIZE][SIZE];
string s[SIZE];
int n, m, q;
pair <int, int> A(int x){
return {x/m, x%m};
}
int B(pair <int, int> x){
return x.first*m+x.second;
}
bool inside(int x, int y){
return x>=0 && y>=0 && x<n && y<m;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for (int i=0; i<n; i++)
cin>>s[i];
Q.push({0, 0});
while (!Q.empty())
{
pair <int, int> u=A(Q.front().first);
int d=Q.front().second;
Q.pop();
if (dist[u.first][u.second])
continue;
dist[u.first][u.second]=d;
for (int g=0; g<4; g++)
if (inside(u.first+vx[g], u.second+vy[g]) && dist[u.first+vx[g]][u.second+vy[g]]==0 && s[u.first+vx[g]][u.second+vy[g]]=='.')
Q.push({B({u.first+vx[g], u.second+vy[g]}), d+1});
}
long long Y=(dist[n-1][m-1]-(n+m-2))/2, X=dist[n-1][m-1]-Y;
while (q--)
cin>>n>>m, M[n*X+m*Y]++;
cout<<(*M.begin()).first<<" "<<(*M.begin()).second<<endl;
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 | #include <bits/stdc++.h> using namespace std; const int SIZE=2300; const int vx[]={0, 1, 0, -1}; const int vy[]={1, 0, -1, 0}; void imax(int &a, int b){ a=max(a, b); } void imin(int &a, int b){ a=min(a, b); } void lmax(long long &a, long long b){ a=max(a, b); } void lmin(long long &a, long long b){ a=min(a, b); } /* WARNING: I'm using strange bracket style! */ queue <pair <int, int> > Q; map <long long, int> M; int dist[SIZE][SIZE]; string s[SIZE]; int n, m, q; pair <int, int> A(int x){ return {x/m, x%m}; } int B(pair <int, int> x){ return x.first*m+x.second; } bool inside(int x, int y){ return x>=0 && y>=0 && x<n && y<m; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>q; for (int i=0; i<n; i++) cin>>s[i]; Q.push({0, 0}); while (!Q.empty()) { pair <int, int> u=A(Q.front().first); int d=Q.front().second; Q.pop(); if (dist[u.first][u.second]) continue; dist[u.first][u.second]=d; for (int g=0; g<4; g++) if (inside(u.first+vx[g], u.second+vy[g]) && dist[u.first+vx[g]][u.second+vy[g]]==0 && s[u.first+vx[g]][u.second+vy[g]]=='.') Q.push({B({u.first+vx[g], u.second+vy[g]}), d+1}); } long long Y=(dist[n-1][m-1]-(n+m-2))/2, X=dist[n-1][m-1]-Y; while (q--) cin>>n>>m, M[n*X+m*Y]++; cout<<(*M.begin()).first<<" "<<(*M.begin()).second<<endl; return 0; } |
English