#include <bits/stdc++.h>
using namespace std;
int n, m, q, a, b, c;
char s[2010][2010];
bitset <2010> odw[2010];
queue <pair <int, pair <int, int> > > que;
int main()
{
scanf("%d%d%d", &n, &m, &q);
for(int i=1; i<=n; ++i)
{
scanf("%s", s[i]+1);
}
que.push({0, {1, 1}});
while(!que.empty())
{
c=que.front().first;
a=que.front().second.first;
b=que.front().second.second;
que.pop();
if(odw[a][b] || s[a][b]=='X') continue;
odw[a][b]=1;
if(a==n && b==m) break;
if(a>1) que.push({c+1, {a-1, b}});
if(a<n) que.push({c+1, {a+1, b}});
if(b>1) que.push({c+1, {a, b-1}});
if(b<m) que.push({c+1, {a, b+1}});
}
long long x=n-1+m-1+(c-n-m+2)/2;
long long y=(c-n-m+2)/2;
long long odp=1e18, p=0;
while(q--)
{
long long w1, w2;
scanf("%lld%lld", &w1, &w2);
if(odp>w1*x+w2*y)
{
odp=w1*x+w2*y;
p=0;
}
p+=(odp==w1*x+w2*y);
}
printf("%lld %lld\n", odp, p);
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 | #include <bits/stdc++.h> using namespace std; int n, m, q, a, b, c; char s[2010][2010]; bitset <2010> odw[2010]; queue <pair <int, pair <int, int> > > que; int main() { scanf("%d%d%d", &n, &m, &q); for(int i=1; i<=n; ++i) { scanf("%s", s[i]+1); } que.push({0, {1, 1}}); while(!que.empty()) { c=que.front().first; a=que.front().second.first; b=que.front().second.second; que.pop(); if(odw[a][b] || s[a][b]=='X') continue; odw[a][b]=1; if(a==n && b==m) break; if(a>1) que.push({c+1, {a-1, b}}); if(a<n) que.push({c+1, {a+1, b}}); if(b>1) que.push({c+1, {a, b-1}}); if(b<m) que.push({c+1, {a, b+1}}); } long long x=n-1+m-1+(c-n-m+2)/2; long long y=(c-n-m+2)/2; long long odp=1e18, p=0; while(q--) { long long w1, w2; scanf("%lld%lld", &w1, &w2); if(odp>w1*x+w2*y) { odp=w1*x+w2*y; p=0; } p+=(odp==w1*x+w2*y); } printf("%lld %lld\n", odp, p); return 0; } |
English