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
#include <bits/stdc++.h>
typedef long long ll;
#define MAXN 2007
using namespace std;
char tab[MAXN][MAXN];
bool vis[MAXN][MAXN][2];
int n, m, k;
ll res, a, b, best, my_time;
deque<pair<pair<int, int>, int>> dist;
pair<pair<int, int>, int> current;
pair<int, int> nnext;
int up[][2] = {{0, 1}, {1, 0}};
int down[][2] = {{-1, 0}, {0, -1}};
inline bool inside(const pair<int, int> &x, const int &move){
  return x.first >= 0 && x.second >= 0 && x.first < n && x.second < m && !vis[x.first][x.second][move];
}
int main(){
  scanf("%d%d%d", &n, &m, &k);

  for(int i = 0; i < n; i++){
    scanf("%s", tab[i]);
    for(int j = 0; j <m ;j ++){
      if(tab[i][j] == 'X'){
       vis[i][j][0] = true;
       vis[i][j][1] = true;
      }
    }
  }
  dist.push_back(make_pair(make_pair(0, 0), 0));
  vis[0][0][0] = true;
  vis[0][0][1] = true;
  while(!dist.empty()){
    current = dist.front();
    if (current.first == make_pair(n-1, m-1)){
      break;
    }
    dist.pop_front();
    for (int i = 0; i < 2; i++){
      nnext = current.first;
      nnext.first += up[i][0];
      nnext.second += up[i][1];
      if (inside(nnext, 0)){
        dist.push_front(make_pair(nnext, current.second));
        vis[nnext.first][nnext.second][0] = true;
      }
    }

    for (int i = 0; i < 2; i++){
      nnext = current.first;
      nnext.first += down[i][0];
      nnext.second += down[i][1];
      if (inside(nnext, 1)){
        dist.push_back(make_pair(nnext, current.second + 1));
        vis[nnext.first][nnext.second][1] = true;
      }
    }

  }

  best = 100000000LL * 1000000000LL;
  while(k--){
    scanf("%lld%lld", &a, &b);
    my_time = a * (ll)(current.second + n + m - 2) + b * current.second;
    if (my_time < best) {
      best = my_time;
      res = 0ll;
    }
    if (my_time == best){
      res++;
    }
  }
  printf("%lld %lld\n", best, res);
}