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;
#define mk make_pair
#define fst first
#define sz(x) x.size()
#define snd second
#define pb push_back
#define VAR(v) #v << " = " << v << " "
#define debug if(0)
#define M_PI 3.14159265358979323846
typedef complex<long double> C;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ll> Matrix;
const int maxn = (int)2e3 + 9;
const int INF = (int)1e9 + 7;
int mod = (int)1e9 + 7;
int n, m, k, cnt = 0;
ll ans = (ll(INF)*(ll(INF)));
int dist[maxn][maxn];
string tab[maxn];

int BFS(){
    ii s = mk(0, 0);
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            dist[i][j] = -1;
    dist[s.fst][s.snd] = 0;
    queue<ii> Q; Q.push(s);
    debug cout << Q.size()<<endl;
    while(!Q.empty()){
        int i = Q.front().fst;
        int j = Q.front().snd;
        int d = dist[i][j]; Q.pop();
        debug cout << VAR(i) << VAR(j) << VAR(d) << VAR(tab[i][j]) << endl;
        if(i != 0 && dist[i-1][j] == -1 && tab[i-1][j] != 'X'){
            dist[i-1][j] = d + 1;
            Q.push(mk(i-1, j));
        }
        if(j != 0 && dist[i][j-1] == -1 && tab[i][j-1] != 'X'){
            dist[i][j-1] = d + 1;
            Q.push(mk(i, j-1));
        }
        if(i != n - 1 && dist[i+1][j] == -1 && tab[i+1][j] != 'X'){
            dist[i+1][j] = d + 1;
            Q.push(mk(i+1, j));
        }
        if(j != m - 1 && dist[i][j+1] == -1 && tab[i][j+1] != 'X'){
            dist[i][j+1] = d + 1;
            Q.push(mk(i, j+1));
        }
    }
    return dist[n-1][m-1];
}


int main(int argc, char* argv[])
{
    ios::sync_with_stdio(false);
    debug; else cin.tie(NULL), cout.tie(NULL);
    cin>>n>>m>>k;
    for(int i = 0; i < n; i++)
        cin>>tab[i];
    int tmp = BFS();
    ll A = n + m - 2 + (tmp - n - m + 2)/2;
    ll B = (tmp - n - m + 2) / 2;
    debug cout<<VAR(tmp) << VAR(A)<<VAR(B)<<endl;
    for(int i = 0; i < k; i++){
        ll a, b; cin>>a>>b;
        if(a * A + b * B ==  ans)
            cnt++;
        if(a * A + b * B < ans)
            ans = a * A + b * B, cnt = 1;
    }
    cout<<ans<<" "<<cnt<<"\n";
    return 0;
}