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
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
const int maxi = 2005;
const int MAXI = 1000000005;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

long long mini = 1e18 + 1;
int ile = 1000005;
pair<int, int> res;
int n, m, k;
string lab[maxi];
pair<int, int> start;
pair<int, int> meta;
deque<pair<int, int>> q;
pair<int, int> dist[maxi][maxi];
bool w(pair<int, int> p){
    return (p.first >= 0) && (p.second >= 0) && (p.first < n) && (p.second < m);
}
pair<int, int> bfs(){
    q.clear();
    q.push_back(start);
    dist[start.first][start.second] = make_pair(0, 0);
    while (!q.empty()){
        pair<int, int> x = q.front();
        q.pop_front();
        for (int i = 0; i < 2; i++){
            pair<int, int> nx = make_pair(x.first + dx[i], x.second + dy[i]);
            if (w(nx) && lab[nx.first][nx.second] != 'X' && dist[nx.first][nx.second] == make_pair(MAXI, MAXI)){
                dist[nx.first][nx.second] = make_pair(dist[x.first][x.second].first + 1, dist[x.first][x.second].second);
                q.push_back(nx);
            }
        }
        for (int i = 2; i < 4; i++){
            pair<int, int> nx = make_pair(x.first + dx[i], x.second + dy[i]);
            if (w(nx) && lab[nx.first][nx.second] != 'X' && dist[nx.first][nx.second] == make_pair(MAXI, MAXI)){
                dist[nx.first][nx.second] = make_pair(dist[x.first][x.second].first, dist[x.first][x.second].second + 1);
                q.push_back(nx);
            }
        }
    }
    return dist[meta.first][meta.second];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++){
        cin >> lab[i];
    }
    for (int i = 0; i < maxi; i++){
        for (int j = 0; j < maxi; j++){
            dist[i][j] = make_pair(MAXI, MAXI);
        }
    }
    start = make_pair(0, 0);
    meta = make_pair(n - 1, m - 1);
    res = bfs();
    for (int i = 0; i < k; i++){
        long long a, b;
        cin >> a >> b;
        long long t = a*res.second + b*res.first;
        if (t < mini){
            mini = t;
            ile = 1;
        }
        else if (t == mini)ile++;
    }
    cout << mini << " " << ile << endl;
    return 0;
}