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

using namespace std;

const int N = 2e3+1;
bool tab[N][N];

int n, m, result, k, cnt;
long long wyn = 1e18, temp;
char c;
queue<pair<pair<int,int>, int> > q;
pair<pair<int,int>, int> item;

int f, s, val;

int times_up() {
    return (result-n-m)/2 + n + m - 1;
}

void decompose() {
    f = item.first.first;
    s = item.first.second;
    val = item.second;
    // cout << "f: " << f << endl << "s: " << s << endl << "val: " << val << endl << endl;
}

vector<int> times;

int main() {
    cin >> n >> m >> k;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> c;
            tab[i][j] = c == '.';
        }
    }

    q.push({{0,0}, 0});
    while (!q.empty()) {
        item = q.front();
        q.pop();
        decompose();

        if (f == n-1 && s == m-1) {
            result = val;
            break;
        }
        if (!tab[f][s])
            continue;
        tab[f][s] = false;

        if (f != 0) {
            if (tab[f-1][s])
            q.push({{f-1,s}, val+1});
        }

        if (f != n-1) {
            if (tab[f+1][s])
            q.push({{f+1,s}, val+1});
        }

        if (s != 0) {
            if (tab[f][s-1])
            q.push({{f,s-1}, val+1});
        }

        if (s != m-1) {
            if (tab[f][s+1])
            q.push({{f,s+1}, val+1});
        }
    }

    long long up = times_up();
    long long down = result - up;
    long long speed_up, speed_down;

    for (int i = 0; i < k; i++) {
        cin >> speed_up >> speed_down;
        temp = speed_up * up + speed_down * down;
        if (temp == wyn) {
            cnt++;
        } else
        if (temp < wyn) {
            cnt = 1;
            wyn = temp;
        }
    }

    cout << wyn << " " << cnt;
}