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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

#define f first
#define s second

const int MAXN = 2e3 + 13;
const int INF = 1e9 + 3;
const long long INF2 = 1e18 + 3;

pair<int, int> tab[4] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int dp[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, k;
    int x, y;
    pair<int, int> p;
    char ch;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch;
            if (ch == '.')
                dp[i][j] = INF;
        }
    }
    dp[1][1] = 0;
    queue<pair<int, int> > q;
    q.push({1, 1});
    while (!q.empty()) {
        p = q.front();
        q.pop();
        for (auto &it:tab) {
            x = p.f + it.f;
            y = p.s + it.s;
            if (dp[x][y] > dp[p.f][p.s] + 1) {
                dp[x][y] = dp[p.f][p.s] + 1;
                q.push({x, y});
            }
        }
    }
    long long a, b, ans = INF2;
    long long c = n + m - 2;
    long long d = (dp[n][m] - c) / 2;
    long long sum;
    int cnt = 0;
    while (k--) {
        cin >> a >> b;
        sum = a * c + (a + b) * d;
        if (sum < ans) {
            ans = sum;
            cnt = 1;
        }
        else if (sum == ans)
            cnt++;
    }
    cout << ans << ' ' << cnt;
    return 0;
}