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
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000000000007
using ll = long long;

int n, m, k, ile_a, ile_b, tab[2007][2007], odw[2007][2007];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
string s;
ll a, b, mini, wynik[1000007], suma;
queue <pair <int, int> > q;

void bfs (pair <int, int> p)
{
    odw[p.first][p.second] = 1;
    q.push(p);
    while (!q.empty())
    {
        p = q.front();
        q.pop();
        for (int i=0; i<4; i++)
        {
            if (odw[p.first+dx[i]][p.second+dy[i]] == 0 && tab[p.first+dx[i]][p.second+dy[i]] == 0)
            {
                odw[p.first+dx[i]][p.second+dy[i]] = odw[p.first][p.second] + 1;
                q.push({p.first+dx[i], p.second+dy[i]});
            }
        }
    }
}

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

    cin >> n >> m >> k;
    for (int y=1; y<=n; y++)
    {
        cin >> s;
        for (int x=1; x<=m; x++)
            if (s[x-1] == 'X')
                tab[x][y] = 1;
    }
    for (int i=0; i<=n+1; i++)
    {
        tab[0][i] = 1;
        tab[m+1][i] = 1;
    }
    for (int i=0; i<=m+1; i++)
    {
        tab[i][0] = 1;
        tab[i][n+1] = 1;
    }
    bfs({1, 1});
    ile_a = n+m-2;
    int pom = odw[m][n] - n - m + 1;
    pom /= 2;
    ile_a += pom;
    ile_b += pom;
    mini = inf;

    for (int i=1; i<=k; i++)
    {
        cin >> a >> b;
        wynik[i] = a*ile_a + b*ile_b;
        mini = min(mini, wynik[i]);
    }
    for (int i=1; i<=k; i++)
        if (wynik[i] == mini)
            suma++;
    cout << mini << " " << suma << "\n";

    return 0;
}