#include<bits/stdc++.h>
using namespace std;
constexpr int N=2e3+3;
struct xyz{
int x, y, z;
};
char tab[N][N];
bitset<N>bt[N];
int bfs(int &n, int &m)
{
queue<xyz>q;
q.push({1,1,0});
while(!q.empty())
{
bt[q.front().x][q.front().y]=1;
if(q.front().x==n && q.front().y==m)
return q.front().z;
if(tab[q.front().x+1][q.front().y]=='.' && !bt[q.front().x+1][q.front().y])
{
bt[q.front().x+1][q.front().y]=1;
q.push({q.front().x+1, q.front().y, q.front().z});
}
if(tab[q.front().x][q.front().y+1]=='.' && !bt[q.front().x][q.front().y+1])
{
bt[q.front().x][q.front().y+1]=1;
q.push({q.front().x, q.front().y+1, q.front().z});
}
if(tab[q.front().x-1][q.front().y]=='.' && !bt[q.front().x-1][q.front().y])
{
q.push({q.front().x-1, q.front().y, q.front().z+1});
}
if(tab[q.front().x][q.front().y-1]=='.' && !bt[q.front().x][q.front().y-1])
{
q.push({q.front().x, q.front().y-1, q.front().z+1});
}
q.pop();
}
}
int main()
{
int n,m,k,a,b,ile;
long long mn=2e18;
scanf("%d%d%d", &n, &m, &k);
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
scanf(" %c", &tab[i][j]);
}
}
long long cofnij=bfs(n,m);
while(k--)
{
scanf("%d%d", &a, &b);
//cout << cofnij << " " << cofnij+n-1+m-1 << endl;
long long tmp=cofnij*(long long)b+(long long)a*(long long)(cofnij+n-1+m-1);
if(mn>tmp)
{
mn=tmp;
ile=1;
}
else if(mn==tmp)
{
++ile;
}
}
printf("%lld %d", mn, 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 | #include<bits/stdc++.h> using namespace std; constexpr int N=2e3+3; struct xyz{ int x, y, z; }; char tab[N][N]; bitset<N>bt[N]; int bfs(int &n, int &m) { queue<xyz>q; q.push({1,1,0}); while(!q.empty()) { bt[q.front().x][q.front().y]=1; if(q.front().x==n && q.front().y==m) return q.front().z; if(tab[q.front().x+1][q.front().y]=='.' && !bt[q.front().x+1][q.front().y]) { bt[q.front().x+1][q.front().y]=1; q.push({q.front().x+1, q.front().y, q.front().z}); } if(tab[q.front().x][q.front().y+1]=='.' && !bt[q.front().x][q.front().y+1]) { bt[q.front().x][q.front().y+1]=1; q.push({q.front().x, q.front().y+1, q.front().z}); } if(tab[q.front().x-1][q.front().y]=='.' && !bt[q.front().x-1][q.front().y]) { q.push({q.front().x-1, q.front().y, q.front().z+1}); } if(tab[q.front().x][q.front().y-1]=='.' && !bt[q.front().x][q.front().y-1]) { q.push({q.front().x, q.front().y-1, q.front().z+1}); } q.pop(); } } int main() { int n,m,k,a,b,ile; long long mn=2e18; scanf("%d%d%d", &n, &m, &k); for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { scanf(" %c", &tab[i][j]); } } long long cofnij=bfs(n,m); while(k--) { scanf("%d%d", &a, &b); //cout << cofnij << " " << cofnij+n-1+m-1 << endl; long long tmp=cofnij*(long long)b+(long long)a*(long long)(cofnij+n-1+m-1); if(mn>tmp) { mn=tmp; ile=1; } else if(mn==tmp) { ++ile; } } printf("%lld %d", mn, ile); return 0; } |
English