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
111
112
113
114
115
#include <iostream>
#include <vector>

using namespace std;

struct para
{
    int x;
    int y;
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, k, i, j, minimum, il_minimum, a, b, pom, pom2, uj = 0;
    char c;
    bool unfound = true;
    long long droga = -1, wlkv;
    para wstaw;

    cin >> n >> m >> k;
    vector<para> OPEN (1);
    OPEN[0].x=1;
    OPEN[0].y=1;
    vector<vector<bool>> TAB (n+2,vector<bool>(m+2));

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            cin>>c;
            if (c == '.')
                TAB[i][j]=true;
            else
                TAB[i][j]=false;
                uj++;
        }
    }

    if (uj < n && uj < m)
    {
        droga = n+m-2;
    }
    else
    {
        while (unfound)
        {
            wlkv = OPEN.size();
            for (i = 0; i < wlkv; i++)
            {
                if (OPEN[i].x == n && OPEN[i].y == m)
                {
                    unfound = false;
                    break;
                }

                if ( TAB[OPEN[i].x][OPEN[i].y+1] )
                {
                    wstaw.x = OPEN[i].x;
                    wstaw.y = OPEN[i].y+1;
                    OPEN.push_back(wstaw);
                }
                if ( TAB[OPEN[i].x][OPEN[i].y-1] )
                {
                    wstaw.x = OPEN[i].x;
                    wstaw.y = OPEN[i].y-1;
                    OPEN.push_back(wstaw);
                }
                if ( TAB[OPEN[i].x+1][OPEN[i].y] )
                {
                    wstaw.x = OPEN[i].x+1;
                    wstaw.y = OPEN[i].y;
                    OPEN.push_back(wstaw);
                }
                if ( TAB[OPEN[i].x-1][OPEN[i].y] )
                {
                    wstaw.x = OPEN[i].x-1;
                    wstaw.y = OPEN[i].y;
                    OPEN.push_back(wstaw);
                }
                TAB[OPEN[i].x][OPEN[i].y] = false;
            }
            droga++;
            OPEN.erase (OPEN.begin(), OPEN.begin() + wlkv);
        }
    }





    pom2 = n+m-2;
    cin >> a >> b;
    minimum = (pom2)*a + (a+b)*(droga-(pom2))/2;
    il_minimum = 1;
    for (i = 1; i < k; i++)
    {
        cin >> a >> b;
        pom = (pom2)*a + (a+b)*(droga-(pom2))/2;
        if (pom < minimum)
        {
            minimum = pom;
            il_minimum = 1;
        }
        else if (pom == minimum)
        {
            il_minimum ++;
        }
    }

    cout << minimum << " " << il_minimum;

    return 0;
}