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
#include <cstdio>
#include <cstdlib>

#include <deque>

bool visited[2000][2000] = { 0 };
char tab[20001][2000];

struct position {
    int x, y;
    int dist;
};

int main() {
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int c;
            do {
                c = getchar();
            } while (c != '.' && c != 'X');
            tab[i][j] = c;
        }
    }

    std::deque<position> q;
    q.push_back(position { 0, 0, 0 });

    int the_dist = 0;

    while (true) {
        position p = q.front();
        q.pop_front();

        if (p.x < 0 || p.y < 0 || p.x >= n || p.y >= m || tab[p.x][p.y] == 'X' || visited[p.x][p.y]) {
            continue;
        }

        if (p.x == n - 1 && p.y == m - 1) {
            the_dist = p.dist;
            break;
        }

        visited[p.x][p.y] = true;

        q.push_back(position { p.x + 1, p.y, p.dist + 1 });
        q.push_back(position { p.x, p.y + 1, p.dist + 1 });
        q.push_back(position { p.x - 1, p.y, p.dist + 1 });
        q.push_back(position { p.x, p.y - 1, p.dist + 1 });
    }

    const long long int normal_steps = n + m - 2;
    const long long int additional_steps = the_dist - normal_steps;

    long long int best_time = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
    int best_count = 0;

    for (int i = 0; i < k; i++) {
        long long int a, b;
        scanf("%lld %lld", &a, &b);

        long long int my_time = a * normal_steps + (a + b) * (additional_steps / 2);
        if (best_time > my_time) {
            best_time = my_time;
            best_count = 1;
        } else if (best_time == my_time) {
            best_count++;
        }
    }

    printf("%lld %d\n", best_time, best_count);

    return 0;
}