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
92
#include<bits/stdc++.h> 
using namespace std;

//g++ -O3 -static 4C/4C.cpp -std=c++11 -o 4C/4c
/*
5 7 1
......X
X.X..X.
..X.X.X
.X.X...
.....X.
2 1
*/

char area[2010][2010];
bool visited[2010][2010];
int dist[2010][2010];
const int max_times = 1e6 + 5;
long long a[max_times], b[max_times];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> area[i][j];
        }
    }
    for(int j = 0; j < k; j++){
        cin >> a[j] >> b[j];
    }

    queue<pair<int, int> > bfs;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    bfs.push({0, 0});
    visited[0][0] = true;
    int bestDistance = -1;

    while(bestDistance == -1){
        pair<int, int> loc = bfs.front();
        bfs.pop();
        int x = loc.first, y = loc.second;
        int d = dist[x][y];
        for(int i = 0; i < 4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx == n-1 && ny == m-1){
                bestDistance = d+1;
                break;
            }
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || area[nx][ny] == 'X' || visited[nx][ny]){
                continue;
            }
            bfs.push({nx, ny});
            dist[nx][ny] = d + 1;
            visited[nx][ny] = true;
        }
    }

    long long down = (bestDistance - n - m + 2) / 2;
    long long up = bestDistance - down;

    long long bestTime = LLONG_MAX;
    int bests = 0;

    for(int i = 0; i < k; i++){
        long long time = a[i] * up + b[i] * down;
        if(time < bestTime){
            bests = 1;
            bestTime = time;
        }else if (time == bestTime){
            bests ++;
        }
    }

    cout << bestTime << " " << bests;
    
    return 0;
}

/*
2 5 4
.....
...X.
2 1
2 2
1 7
2 1
*/