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
#include<bits/stdc++.h>
using namespace std;
#define magiczne ios_base::sync_with_stdio(0);
#define linijki cin.tie(0);
#define f first
#define s second
typedef pair <long long, long long> PII;
PII elo;
long long n, m, k, a, b, best = 1e18, result, dist[2007][2007], high, low;
char temp;
queue <PII> Q;
int main()
{
    magiczne linijki
    cin >> n >> m >> k;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> temp;
            if (temp == 'X') dist[i][j] = -1;
            else dist[i][j] = INT_MAX;
        }
    dist[0][0] = 0;
    Q.push(make_pair(0, 0));
    while (!Q.empty())
    {
        elo = Q.front();
        Q.pop();
        if (elo.f && dist[elo.f - 1][elo.s] == INT_MAX)
        {
            dist[elo.f - 1][elo.s] = dist[elo.f][elo.s] + 1;
            Q.push(make_pair(elo.f - 1, elo.s));
        }
        if (elo.s && dist[elo.f][elo.s - 1] == INT_MAX)
        {
            dist[elo.f][elo.s - 1] = dist[elo.f][elo.s] + 1;
            Q.push(make_pair(elo.f, elo.s - 1));
        }
        if (elo.f < n - 1 && dist[elo.f + 1][elo.s] == INT_MAX)
        {
            dist[elo.f + 1][elo.s] = dist[elo.f][elo.s] + 1;
            Q.push(make_pair(elo.f + 1, elo.s));
        }
        if (elo.s < m - 1 && dist[elo.f][elo.s + 1] == INT_MAX)
        {
            dist[elo.f][elo.s + 1] = dist[elo.f][elo.s] + 1;
            Q.push(make_pair(elo.f, elo.s + 1));
        }
        if (dist[n - 1][m - 1] != INT_MAX) break;
    }
    high = m + n - 2;
    low = (dist[n - 1][m - 1] - high) / 2;
    high += low;
    while (k--)
    {
        cin >> a >> b;
        a *= high, b *= low;
        if (a + b < best)
        {
            best = a + b;
            result = 0;
        }
        if (a + b == best) result++;
    }
    cout << best << ' ' << result;
    return 0;
}