#include <bits/stdc++.h>
using namespace std;
string s;
long long t[3000][3000];
long long tt[3000][3000];
bool odw[3000][3000];
queue <pair<int,int> > Q;
map <long long, int> M;
void bfs(int n,int m)
{
pair <int,int> x;
odw[0][0]=1;
Q.push(make_pair(0,0));
while(!Q.empty())
{
///cout<<x.first<<' '<<x.second<<'\n';
x=Q.front();
Q.pop();
if(x.first!=0)
{
if(!odw[x.first-1][x.second])
{
Q.push(make_pair(x.first-1,x.second));
odw[x.first-1][x.second]=1;
tt[x.first-1][x.second]=tt[x.first][x.second]+1;
t[x.first-1][x.second]=t[x.first][x.second];
}
}
if(x.first!=n-1)
{
///cout<<x.first+1<<' '<<x.second<<' '<<odw[x.first+1][x.second]<<"\n\n\n";
if(!odw[x.first+1][x.second])
{
Q.push(make_pair(x.first+1,x.second));
odw[x.first+1][x.second]=1;
t[x.first+1][x.second]=t[x.first][x.second]+1;
tt[x.first+1][x.second]=tt[x.first][x.second];
}
}
if(x.second!=0)
{
if(!odw[x.first][x.second-1])
{
Q.push(make_pair(x.first,x.second-1));
odw[x.first][x.second-1]=1;
tt[x.first][x.second-1]=tt[x.first][x.second]+1;
t[x.first][x.second-1]=t[x.first][x.second];
}
}
if(x.second!=m-1)
{
if(!odw[x.first][x.second+1])
{
Q.push(make_pair(x.first,x.second+1));
odw[x.first][x.second+1]=1;
t[x.first][x.second+1]=t[x.first][x.second]+1;
tt[x.first][x.second+1]=tt[x.first][x.second];
}
}
}
}
int main()
{
///ios_base::sync_with_stdio(0);
///cin.tie(0);
long long a,b;
int n,m,k;
cin>>n>>m>>k;
for(int i=0;i<n;++i)
{
cin>>s;
for(int j=0;j<m;++j)
{
if(s[j]=='X')
{
odw[i][j]=1;
}
}
}
/*for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
cout<<odw[i][j];
}
cout<<'\n';
}*/
bfs(n,m);
/*for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
cout<<t[i][j]<<' '<<tt[i][j]<<" || ";
}
cout<<'\n';
}*/
for(int i=0;i<k;++i)
{
cin>>a>>b;
M[a*t[n-1][m-1]+b*tt[n-1][m-1]]++;
}
auto it=M.begin();
cout<<it->first<<' '<<it->second;
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 104 105 106 | #include <bits/stdc++.h> using namespace std; string s; long long t[3000][3000]; long long tt[3000][3000]; bool odw[3000][3000]; queue <pair<int,int> > Q; map <long long, int> M; void bfs(int n,int m) { pair <int,int> x; odw[0][0]=1; Q.push(make_pair(0,0)); while(!Q.empty()) { ///cout<<x.first<<' '<<x.second<<'\n'; x=Q.front(); Q.pop(); if(x.first!=0) { if(!odw[x.first-1][x.second]) { Q.push(make_pair(x.first-1,x.second)); odw[x.first-1][x.second]=1; tt[x.first-1][x.second]=tt[x.first][x.second]+1; t[x.first-1][x.second]=t[x.first][x.second]; } } if(x.first!=n-1) { ///cout<<x.first+1<<' '<<x.second<<' '<<odw[x.first+1][x.second]<<"\n\n\n"; if(!odw[x.first+1][x.second]) { Q.push(make_pair(x.first+1,x.second)); odw[x.first+1][x.second]=1; t[x.first+1][x.second]=t[x.first][x.second]+1; tt[x.first+1][x.second]=tt[x.first][x.second]; } } if(x.second!=0) { if(!odw[x.first][x.second-1]) { Q.push(make_pair(x.first,x.second-1)); odw[x.first][x.second-1]=1; tt[x.first][x.second-1]=tt[x.first][x.second]+1; t[x.first][x.second-1]=t[x.first][x.second]; } } if(x.second!=m-1) { if(!odw[x.first][x.second+1]) { Q.push(make_pair(x.first,x.second+1)); odw[x.first][x.second+1]=1; t[x.first][x.second+1]=t[x.first][x.second]+1; tt[x.first][x.second+1]=tt[x.first][x.second]; } } } } int main() { ///ios_base::sync_with_stdio(0); ///cin.tie(0); long long a,b; int n,m,k; cin>>n>>m>>k; for(int i=0;i<n;++i) { cin>>s; for(int j=0;j<m;++j) { if(s[j]=='X') { odw[i][j]=1; } } } /*for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { cout<<odw[i][j]; } cout<<'\n'; }*/ bfs(n,m); /*for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { cout<<t[i][j]<<' '<<tt[i][j]<<" || "; } cout<<'\n'; }*/ for(int i=0;i<k;++i) { cin>>a>>b; M[a*t[n-1][m-1]+b*tt[n-1][m-1]]++; } auto it=M.begin(); cout<<it->first<<' '<<it->second; return 0; } |
English