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
81
82
#include<bits/stdc++.h>
using namespace std;

int n, m;
char G[2002][2002];
int k, a, b;
int D[2002][2002], M[2002][2002];

void bfs()
{
    queue< pair<int, pair<int, int> > >Q;
    Q.push( make_pair(0, make_pair(1, 1)));
    int r,p,k;
    while(!Q.empty())
    {
        k=Q.front().first;
        p=Q.front().second.first;
        r=Q.front().second.second;
        Q.pop();

        if(G[p+1][r]=='.'&&D[p+1][r]==0&&M[p+1][r]==0)
        {
            D[p+1][r]=D[p][r]+1;
            M[p+1][r]=M[p][r];
            Q.push(make_pair(D[p+1][r]+M[p+1][r], make_pair(p+1, r)));
        }
        if(G[p-1][r]=='.'&&D[p-1][r]==0&&M[p-1][r]==0)
        {
            D[p-1][r]=D[p][r];
            M[p-1][r]=M[p][r]+1;
            Q.push(make_pair(D[p-1][r]+M[p-1][r], make_pair(p-1, r)));
        }
        if(G[p][r+1]=='.'&&D[p][r+1]==0&&M[p][r+1]==0)
        {
            D[p][r+1]=D[p][r]+1;
            M[p][r+1]=M[p][r];
            Q.push(make_pair(D[p][r+1]+M[p][r+1], make_pair(p, r+1)));
        }
        if(G[p][r-1]=='.'&&D[p][r-1]==0&&M[p][r-1]==0)
        {
            D[p][r-1]=D[p][r];
            M[p][r-1]=M[p][r]+1;
            Q.push(make_pair(D[p][r-1]+M[p][r-1], make_pair(p, r-1)));
        }
    }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m >> k;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin >> G[i][j];
        }
    }
    bfs();

    long long val=1000000000000000009, ilosc=1;
    for(int i=0;i<k;i++)
    {
        cin >> a >> b;

        if((long long)a*D[n][m]+(long long)b*M[n][m]<val)
        {
            val=(long long)a*D[n][m]+(long long)b*M[n][m];
            ilosc=1;
        }
        else if((long long)a*D[n][m]+(long long)b*M[n][m]==val)
        {
            ilosc++;
        }
    }

    cout << val << " " << ilosc;
}