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
#include <iostream>
#include <list>
#include <climits>


int findPath(char **map, int n, int m) {
    int block = (int) 'X';
    int steps = 0;
    int s = 0;
    int SIZE = m * n;
    int *visited = new int[SIZE]{0};
    int *distance = new int[SIZE]{0};
    int *queue = new int[SIZE]{0};


    visited[s] = true;
    int queueFront = 0;
    int queueBack = 1;
    int rLimit = m - 1;
    while (!visited[SIZE - 1] && queueFront < queueBack) {
        s = queue[queueFront++];
        int i = s / m;
        int j = s % m;

        if (j < rLimit && !visited[s + 1] && (int) map[i][j + 1] != block) {
            visited[s + 1] = 1;
            distance[s + 1] = distance[s] + 1;
            queue[queueBack++] = s + 1;
        }
        if (i < n - 1 && !visited[s + m] && (int) map[i + 1][j] != block) {
            visited[s + m] = 1;
            distance[s + m] = distance[s] + 1;
            queue[queueBack++] = s + m;
        }

        if (j > 0 && !visited[s - 1] && (int) map[i][j - 1] != block) {
            visited[s - 1] = 1;
            distance[s - 1] = distance[s] + 1;
            queue[queueBack++] = s - 1;
        }

        if (i > 0 && !visited[s - m] && (int) map[i - 1][j] != block) {
            visited[s - m] = 1;
            distance[s - m] = distance[s] + 1;
            queue[queueBack++] = s - m;
        }
        steps++;
    }
    return distance[SIZE - 1];
}

int main() {
    std::ios_base::sync_with_stdio(0);
    int n, m, k;
    std::cin >> n >> m >> k;
    auto map = new char *[n];

    for (int i = 0; i < n; i++) {
        map[i] = new char[m];
        std::cin >> map[i];
    }

    long long distance = findPath(map, n, m);
    long long minPath = n + m - 2;
    long long overhead = (distance - minPath) / 2;
    long long costUp;
    long long costDown;
    long long min = LLONG_MAX;
    int minCount = 0;
    for (int i = 0; i < k; i++) {
        std::cin >> costUp >> costDown;
        long long cost = minPath * costUp;
        cost += overhead * (costUp + costDown);
        if (cost < min) {
            min = cost;
            minCount = 1;
        } else if (cost == min) {
            minCount += 1;
        }
    }
    std::cout << min << " " << minCount << std::endl;

    return 0;
}