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
78
79
80
#include <iostream>
#include <queue>
using namespace std;

int n, m, k;
int mapka[4000005];
int up[4000005];
int down[4000005];
int d[4000005];
int vis[4000005];
const int inf = 1e9;
long long ans = 1e18;
int l;

void bfs()
{
    queue <int> w;
    w.push(1);
    while (!w.empty())
    {
        int u = w.front();
        w.pop();
        if (vis[u]) continue;
        vis[u] = true;
        if ((u > m) && (mapka[u - m]) && (!vis[u - m]) && (d[u] + 1 < d[u - m]))
        {
            d[u - m] = d[u] + 1;
            up[u - m] = up[u];
            down[u - m] = down[u] + 1;
            w.push(u - m);
        }
        if ((u % m) && (mapka[u + 1]) && (!vis[u + 1]) && (d[u] + 1 < d[u + 1]))
        {
            d[u + 1] = d[u] + 1;
            up[u + 1] = up[u] + 1;
            down[u + 1] = down[u];
            w.push(u + 1);
        }
        if ((u <= (n - 1) * m) && (mapka[u + m]) && (!vis[u + m]) && (d[u] + 1 < d[u + m]))
        {
            d[u + m] = d[u] + 1;
            up[u + m] = up[u] + 1;
            down[u + m] = down[u];
            w.push(u + m);
        }
        if ((u % m != 1) && (mapka[u - 1]) && (!vis[u - 1]) && (d[u] + 1 < d[u - 1]))
        {
            d[u - 1] = d[u] + 1;
            up[u - 1] = up[u];
            down[u - 1] = down[u] + 1;
            w.push(u - 1);
        }
    } 
}

int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= m * n; i++)
    {
        char c;
        cin >> c;
        if (c == '.') mapka[i] = 1;
    }
    for (int i = 2; i <= n * m; i++) d[i] = inf;
    bfs();
    while (k--)
    {
        long long a, b, t1 = up[n * m], t2 = down[n * m];
        cin >> a >> b;
        t1 *= a;
        t2 *= b;
        if (t1 + t2 == ans) l++;
        if (t1 + t2 < ans) {ans = t1 + t2; l = 1; }
    }
    cout << ans << " " << l;
}