#include <bits/stdc++.h>
using namespace std;
long long n, m,k, inf=40000000000000006;
pair <long long,long long> tab [2007][2007];
long long wyn[1000006];
queue <pair<int, int> > q;
bool czy(int x,int y)
{
if(x>=n)
return 0;
if(y>=m)
return 0;
if(x<0)
return 0;
if(y<0)
return 0;
if(tab[x][y].first!=0||tab[x][y].second!=0)
{
return 0;
}
return 1;
}
void bfs()
{
q.push(make_pair(0,0));
tab[0][0].first=1;
tab[0][0].second=1;
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
if(czy(x+1,y)==1)
{
tab[x+1][y].first=tab[x][y].first+1;
tab[x+1][y].second=tab[x][y].second;
q.push(make_pair(x+1,y));
}
if(czy(x-1,y)==1)
{
tab[x-1][y].first=tab[x][y].first;
tab[x-1][y].second=tab[x][y].second+1;
q.push(make_pair(x-1,y));
}
if(czy(x,y+1)==1)
{
tab[x][y+1].first=tab[x][y].first+1;
tab[x][y+1].second=tab[x][y].second;
q.push(make_pair(x,y+1));
}
if(czy(x,y-1)==1)
{
tab[x][y-1].first=tab[x][y].first;
tab[x][y-1].second=tab[x][y].second+1;
q.push(make_pair(x,y-1));
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
for(int i=0; i<n; i++)
{
for(int j=0;j<m;j++)
{
char c;
cin>>c;
if(c=='X')
{
tab[i][j]=make_pair(inf, inf);
}
}
}
bfs();
pair<long long,long long> p=tab[n-1][m-1];
p.first--;
p.second--;
long long maks=inf;
for(int i=0;i<k;i++)
{
long long a,b;
cin>>a>>b;
wyn[i]=p.first*a+p.second*b;
maks=min(wyn[i],maks);
}
int ile=0;
for(int i=0;i<k;i++)
{
if(wyn[i]==maks)
{
ile++;
}
}
cout<<maks<<" "<<ile;
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 | #include <bits/stdc++.h> using namespace std; long long n, m,k, inf=40000000000000006; pair <long long,long long> tab [2007][2007]; long long wyn[1000006]; queue <pair<int, int> > q; bool czy(int x,int y) { if(x>=n) return 0; if(y>=m) return 0; if(x<0) return 0; if(y<0) return 0; if(tab[x][y].first!=0||tab[x][y].second!=0) { return 0; } return 1; } void bfs() { q.push(make_pair(0,0)); tab[0][0].first=1; tab[0][0].second=1; while(!q.empty()) { int x=q.front().first; int y=q.front().second; q.pop(); if(czy(x+1,y)==1) { tab[x+1][y].first=tab[x][y].first+1; tab[x+1][y].second=tab[x][y].second; q.push(make_pair(x+1,y)); } if(czy(x-1,y)==1) { tab[x-1][y].first=tab[x][y].first; tab[x-1][y].second=tab[x][y].second+1; q.push(make_pair(x-1,y)); } if(czy(x,y+1)==1) { tab[x][y+1].first=tab[x][y].first+1; tab[x][y+1].second=tab[x][y].second; q.push(make_pair(x,y+1)); } if(czy(x,y-1)==1) { tab[x][y-1].first=tab[x][y].first; tab[x][y-1].second=tab[x][y].second+1; q.push(make_pair(x,y-1)); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>k; for(int i=0; i<n; i++) { for(int j=0;j<m;j++) { char c; cin>>c; if(c=='X') { tab[i][j]=make_pair(inf, inf); } } } bfs(); pair<long long,long long> p=tab[n-1][m-1]; p.first--; p.second--; long long maks=inf; for(int i=0;i<k;i++) { long long a,b; cin>>a>>b; wyn[i]=p.first*a+p.second*b; maks=min(wyn[i],maks); } int ile=0; for(int i=0;i<k;i++) { if(wyn[i]==maks) { ile++; } } cout<<maks<<" "<<ile; return 0; } |
English