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

int tab[2009][2009], col[4000009], wyn[4000009];
queue<int> kol;
vector<int> graf[4000009];
string s;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m,k,a,b;
    long long c,d,e = 1e16,f = 0;
    cin>>n>>m>>k;
    for(int i=1; i<=n; i++)
    {
        cin>>s;
        for(int j=0; j<s.size(); j++) if(s[j] == 'X') tab[i][j+1] = 1;
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(tab[i][j] == 1) continue;
            if(i > 1 && tab[i-1][j] == 0) graf[(i-1) * m + j].push_back((i-2) * m + j);
            if(i < n && tab[i+1][j] == 0) graf[(i-1) * m + j].push_back(i * m + j);
            if(j > 1 && tab[i][j-1] == 0) graf[(i-1) * m + j].push_back((i-1) * m + j - 1);
            if(j < m && tab[i][j+1] == 0) graf[(i-1) * m + j].push_back((i-1) * m + j + 1);
        }
    }
    kol.push(1);
    while(!kol.empty())
    {
        a = kol.front();
        kol.pop();
        if(col[a] != 0) continue;
        col[a] = 1;
        for(int i=0; i<graf[a].size(); i++)
        {
            b = graf[a][i];
            if((wyn[b] == 0 && b != 1) || wyn[b] > wyn[a] + 1)
            {
                wyn[b] = wyn[a]+1;
                kol.push(b);
            }
        }
    }
    a = (wyn[n * m] - n - m + 2) / 2;
    for(int i=1; i<=k; i++)
    {
        cin>>c>>d;
        c = c * (n + m - 2 + a) + d * a;
        if(c < e)
        {
            e = c;
            f = 1;
        }
        else if(c == e) f++;
    }
    cout<<e<<" "<<f;
    return 0;
}