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
// Karol Kosinski 2020
#include <cstdio>
#include <utility>
#include <queue>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
using namespace std;

typedef long long LL;
const int NX = 2000;
const int FREE = -1;
const int DANGER = -2;
const int dx[] = { +1,  0, -1,  0 };
const int dy[] = {  0, +1,  0, -1 };

int T[NX][NX];
char S[NX];

int bfs(int n, int m)
{
    queue<pair<int, int>> Q;
    T[0][0] = 0;
    Q.emplace(0, 0);
    while (not Q.empty())
    {
        auto& p = Q.front();
        FOR(i,0,4)
        {
            int x = p.first + dx[i];
            int y = p.second + dy[i];
            if (0 <= x and x < n and 0 <= y and y < m and T[x][y] == FREE)
            {
                T[x][y] = T[p.first][p.second] + 1;
                Q.emplace(x, y);
            }
        }
        Q.pop();
    }
    return T[ n - 1 ][ m - 1 ];
}

int main()
{
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    FOR(i,0,n)
    {
        scanf("%s", S);
        FOR(j,0,m)
        {
            T[i][j] = (S[j] == 'X') ? DANGER : FREE;
        }
    }
    int res = bfs(n, m);
    LL down = (res - n - m + 2) / 2;
    LL up = res - down;
    LL best = 1'000'000'000'000'000'003LL;
    int counter = 0;
    FOR(i,0,k)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        LL v = a * up + b * down;
        if (v == best)
        {
            ++ counter;
        }
        else if (v < best)
        {
            best = v;
            counter = 1;
        }
    }
    printf("%lld %d\n", best, counter);
    return 0;
}