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
93
94
95
96
97
98
//runda 3C
#include <iostream>
#include <queue>
#include <limits>
using namespace std;

struct Punkt {
  int x, y;
  Punkt() {x = 0; y = 0;}
  Punkt(int _x, int _y):x(_x), y(_y) {}
};

char gory[2020][2020];

int main() {
  ios_base::sync_with_stdio(0);
  int n, m, k;
  queue<Punkt> Q;
  Punkt pa;
  long long a, b, mna = 0, mnb = 0, wynikakt, wynik = numeric_limits<long long>::max(), ile;

  cin >> n >> m >> k;
  for(int iN = 1; iN <= n; ++iN) {
    cin >> &gory[iN][1];
    gory[iN][0] = 'X';
    gory[iN][m + 1] = 'X';
  }
  for(int iM = 0; iM <= m + 1; ++iM) {
    gory[0][iM] = 'X';
    gory[n + 1][iM] = 'X';
  }

  gory[1][1] = 'X';
  Q.push(Punkt(1, 1));
  while(!Q.empty()) {
    pa = Q.front();
    Q.pop();
    if(pa.x == n && pa.y == m)
      break;
    if(gory[pa.x - 1][pa.y] == '.') {
      gory[pa.x - 1][pa.y] = 'd';
      Q.push(Punkt(pa.x - 1, pa.y));
    }
    if(gory[pa.x + 1][pa.y] == '.') {
      gory[pa.x + 1][pa.y] = 'g';
      Q.push(Punkt(pa.x + 1, pa.y));
    }
    if(gory[pa.x][pa.y - 1] == '.') {
      gory[pa.x][pa.y - 1] = 'p';
      Q.push(Punkt(pa.x, pa.y - 1));
    }
    if(gory[pa.x][pa.y + 1] == '.') {
      gory[pa.x][pa.y + 1] = 'l';
      Q.push(Punkt(pa.x, pa.y + 1));
    }
  }
/*
  for(int iN = 0; iN <= n + 1; ++iN)
    cout << gory[iN] << endl;
*/
  pa.x = n;
  pa.y = m;
  while(pa.x != 1 || pa.y != 1) {
    switch(gory[pa.x][pa.y]) {
      case 'g':
        ++mna;
        --pa.x;
        break;
      case 'l':
        ++mna;
        --pa.y;
        break;
      case 'd':
        ++mnb;
        ++pa.x;
        break;
      case 'p':
        ++mnb;
        ++pa.y;
        break;
    }
  }

  for(int iK = 0; iK < k; ++iK) {
    cin >> a >> b;
    wynikakt = mna * a + mnb * b;
    if(wynikakt == wynik)
      ++ile;
    else if(wynikakt < wynik) {
      wynik = wynikakt;
      ile = 1;
    }
  }

  cout << wynik << " " << ile;

  return 0;
}