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
 99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <queue>

using namespace std;

int main()
{
    long long x, y, znaj, x1, x2, fron, dro, debug = 0;
    cin >> x >> y >> znaj;
    string a;
    char ac;
    int stok [x][y];
    for (int q = 0; q < x; q++){
        cin >> a;
        for (int w = 0; w < y; w++){
            ac = a[w];
            if (ac == '.'){
                stok[q][w] = 0;
            }
            else stok[q][w] = -1;
        }
    }
    /*for (int q = 0; q < x; q++){
        for (int w = 0; w < y; w++){
            cout << stok[q][w] << " ";
        }
        cout << '\n';
    }*/
    ///BFSSSSSSSSSSSSSSSSSSSSSSSS
    queue <int> kolej;
    kolej.push(0);
    stok [0][0] = 1;
    while (kolej.size() > 0){
        debug++;
        //if (debug == 10) return 0;
        fron = kolej.front();
        kolej.pop();
        x1 = fron/y;
        x2 = fron%y;
        dro = stok[x1][x2];
        //cout << x1 << " " << x2 << '\n';
        //cout << "------>" << x1*y+x2 << '\n';
        if (x1 < x && x2+1 < y){
            //cout << "można poziom przód\n";
            if (stok[x1][x2+1] == 0){
                //cout << "serio można\n";
                //cout << (x1)*y+(x2+1) << '\n';
                kolej.push((x1)*y+(x2+1));
                stok[x1][x2+1] = dro+1;
            }
        }
        if (x1+1 < x && x2 < y){
            //cout << "można pion przód\n";
            if (stok[x1+1][x2] == 0){
                //cout << "serio można\n";
                //cout << (x1+1)*y+(x2) << '\n';
                kolej.push((x1+1)*y+(x2));
                stok[x1+1][x2] = dro+1;
            }
        }
        if (x1 >= 0 && x2-1 >= 0){
            //cout << "można poziom tył\n";
            if (stok[x1][x2-1] == 0){
                //cout << "serio można\n";
                //cout << (x1)*y+(x2-1) << '\n';
                kolej.push((x1)*y+(x2-1));
                stok[x1][x2-1] = dro+1;
            }
        }
        if (x1-1 >= 0 && x2 >= 0){
            //cout << "można pion tył\n";
            if (stok[x1-1][x2] == 0){
                //cout << "serio można\n";
                //cout << (x1-1)*y+(x2) << '\n';
                kolej.push((x1-1)*y+(x2));
                stok[x1-1][x2] = dro+1;
            }
        }
    }
    for (int q = 0; q < x; q++){
        for (int w = 0; w < y; w++){
            //cout << stok[q][w] << " ";
        }
        //cout << '\n';
    }
    long long dol, gora, maks = 1000000000000000000, ile;
    gora = (stok[x-1][y-1]-x-y+1)/2;
    dol = stok[x-1][y-1]-gora;
    dol--;
    //cout << gora << " " << dol << '\n';

    for (int q = 0; q < znaj; q++){
        cin >> x >> y;
        if (gora*y+dol*x < maks){
            maks = gora*y+dol*x;
            ile = 1;
        }
        else if (gora*y+dol*x == maks){
            ile++;
        }
        //cout << gora*y+dol*x << '\n';
    }
    cout << maks << " " << ile;





}