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
#include <iostream>
#include <queue>
#include <limits>

using Map = char **;
using DistanceMap = long long **;

struct Point {
    int x, y;
};


auto top(Point point) {
    return Point{point.x - 1, point.y};
}
auto bottom(Point point) {
    return Point{point.x + 1, point.y};
}
auto right(Point point) {
    return Point{point.x, point.y + 1};
}
auto left(Point point) {
    return Point{point.x, point.y - 1};
}

bool onBoard(int height, int width, Point point) {
    return point.x >=0 and point.x < height and point.y >= 0 and point.y < width;
}

bool notCovered(DistanceMap distances, Point point) {
    return distances[point.x][point.y] == -1;
}

bool notBlocked(Map map, Point point) {
    return map[point.x][point.y] == '.';
}


long long shortestPath(DistanceMap distances, Map map, int height, int width) {
    std::queue<Point> bfsQueue;
    Point current{0, 0};
    distances[current.x][current.y] = 0;
    while (true) {
        for (auto next: {top(current), bottom(current), left(current), right(current)}) {
            if (onBoard(height, width, next) and notCovered(distances, next) and notBlocked(map, next)) {
                bfsQueue.push(next);
                distances[next.x][next.y] = distances[current.x][current.y] + 1;
            }
        }
        current = bfsQueue.front();
        bfsQueue.pop();
        if (current.x == height - 1 and current.y == width - 1) break;
    }
    return distances[height - 1][width - 1];
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, m, k;
    std::cin >> n >> m >> k;
    Map map = new char *[n];
    DistanceMap distances = new long long *[n];
    for (int i = 0; i < n; ++i) {
        map[i] = new char[m];
        distances[i] = new long long[m];
        for (int j = 0; j < m; ++j) {
            std::cin >> map[i][j];
            distances[i][j] = -1;
        }
    }
    long long pathLen = shortestPath(distances, map, n, m);
    long long minLen = m + n - 2;
    long long upSteps = (pathLen + minLen) / 2;
    long long downSteps = (pathLen - minLen) / 2;
    long long fastestTime = std::numeric_limits<long long>::max(), fastestCount = 0;
    for (int i = 0; i < k; ++i) {
        long long a, b;
        std::cin >> a >> b;
        long long currentTime = upSteps * a + downSteps * b;
        if (fastestTime == currentTime) {
            ++fastestCount;
        } else if (fastestTime > currentTime) {
            fastestTime = currentTime;
            fastestCount = 1;
        }
    }
    std::cout << fastestTime << ' ' << fastestCount << std::endl;
    return 0;
}