#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
int n, m, k;
vector<string> mapa;
struct Wsp
{
int nn,mm;
Wsp(int nn, int mm) : nn(nn), mm(mm)
{
mapa[nn][mm]='X';
}
};
vector<Wsp> nast;
void idz(int wn, int wm)
{
if (wn<0 || n<=wn || wm<0 || m<=wm || mapa[wn][wm]=='X')
return;
nast.push_back(Wsp(wn, wm));
}
int dlDrogi()
{
vector<Wsp> ter;
ter.push_back(Wsp(0, 0));
for (int dl=0; ; ++dl)
{
nast.clear();
for (int i=0; i<ter.size(); ++i)
{
Wsp &w=ter[i];
if (w.nn==n-1 && w.mm==m-1)
return dl;
idz(w.nn-1, w.mm);
idz(w.nn+1, w.mm);
idz(w.nn, w.mm-1);
idz(w.nn, w.mm+1);
}
nast.swap(ter);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin>>n>>m>>k;
for (int i=0; i<n; ++i)
{
string s;
cin>>s;
mapa.push_back(s);
}
int dl=dlDrogi();
int podst=(n-1)+(m-1);
int dod=(dl-podst)/2;
LL g=podst+dod;
LL d=dod;
LL naj=1000000000000000000LL;
int lNaj=0;
for (int i=0; i<k; ++i)
{
int a, b;
cin>>a>>b;
LL czas=a*g+b*d;
if (czas<naj)
{
naj=czas;
lNaj=1;
}
else if (czas==naj)
++lNaj;
}
cout<<naj<<' '<<lNaj<<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 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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; typedef long long LL; int n, m, k; vector<string> mapa; struct Wsp { int nn,mm; Wsp(int nn, int mm) : nn(nn), mm(mm) { mapa[nn][mm]='X'; } }; vector<Wsp> nast; void idz(int wn, int wm) { if (wn<0 || n<=wn || wm<0 || m<=wm || mapa[wn][wm]=='X') return; nast.push_back(Wsp(wn, wm)); } int dlDrogi() { vector<Wsp> ter; ter.push_back(Wsp(0, 0)); for (int dl=0; ; ++dl) { nast.clear(); for (int i=0; i<ter.size(); ++i) { Wsp &w=ter[i]; if (w.nn==n-1 && w.mm==m-1) return dl; idz(w.nn-1, w.mm); idz(w.nn+1, w.mm); idz(w.nn, w.mm-1); idz(w.nn, w.mm+1); } nast.swap(ter); } } int main() { ios_base::sync_with_stdio(false); cin>>n>>m>>k; for (int i=0; i<n; ++i) { string s; cin>>s; mapa.push_back(s); } int dl=dlDrogi(); int podst=(n-1)+(m-1); int dod=(dl-podst)/2; LL g=podst+dod; LL d=dod; LL naj=1000000000000000000LL; int lNaj=0; for (int i=0; i<k; ++i) { int a, b; cin>>a>>b; LL czas=a*g+b*d; if (czas<naj) { naj=czas; lNaj=1; } else if (czas==naj) ++lNaj; } cout<<naj<<' '<<lNaj<<endl; return 0; } |
English