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
83
84
85
86
87
88
89
90
91
92
#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define INF 99999999999999999

using namespace std;
typedef pair<long long, long long> pp;
typedef pair<long long, pp> ppp;

long long n, m, k, d, e, l;
long long a, b, w=INF;
char c;
deque<ppp> pq;
ppp temp;
pp p;
long long c1, c2;
long long tab[2003][2003][2];


void wyc()
{
    while(!pq.empty())
    {
        temp=pq.front();
        c1=temp.ff;
        p=temp.ss;
        pq.pop_front();
        if(c1<tab[p.ff][p.ss][1])
        {
            tab[p.ff][p.ss][1]=c1;
            if(tab[p.ff][p.ss+1][0]==1)
            {
                pq.push_back(mp((c1+1), mp(p.ff, p.ss+1)));
            }
            if(tab[p.ff+1][p.ss][0]==1)
            {
                pq.push_back(mp((c1+1), mp(p.ff+1, p.ss)));
            }
            if(tab[p.ff][p.ss-1][0]==1)
            {
                pq.push_front(mp(c1, mp(p.ff, p.ss-1)));
            }
            if(tab[p.ff-1][p.ss][0]==1)
            {
                pq.push_front(mp(c1, mp(p.ff-1, p.ss)));
            }
        }
    }
}
int main()
{
    scanf("%lld%lld%lld", &n, &m, &k);
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            scanf("%c", &c);
            while(c!='.' && c!='X')
            {
                scanf("%c", &c);
            }
            if(c=='.')
            {
                tab[i][j][0]=1;
            }
            tab[i][j][1]=INF;
        }
    }

    pq.push_front(mp(0, mp(1, 1)));
    wyc();

    d=tab[n][m][1];
    e=d-n-m+2;
    for(int i=0; i<k; ++i)
    {
        scanf("%lld%lld", &a, &b);
        if(w>(a*d)+(b*e))
        {
            w=(a*d)+(b*e);
            l=1;
        }
        else if(w==(a*d)+(b*e))
        {
            l++;
        }
    }
    printf("%lld %lld", w, l);

}