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
#include <iostream>
#include <queue>
#include <climits>

using namespace std;

int x, y;

int ind(int xN, int yN)
{
    return (yN * x + xN);
}

int min(int a, int b)
{
    return (a > b ? a : b);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, i ,j;
    string buff;

    cin >> y;
    cin >> x;
    cin >> n;

    int* pola = new int[x * y]();

    for (i = 0; i < y; i++)
    {
        cin >> buff;

        for (j = 0; j < x; j++)
        {
            if (buff[j] == 'X')
                pola[ind(j, i)] = -2;
            else
                pola[ind(j, i)] = -1;
        }
    }

    pola[0] = 0;
    queue<int> toIndex;
    toIndex.push(0);
    int index;
    int maxA = (x * y - 2);
    int maxB = (x * y - x - 1);
    int wynik;

    while (true)
    {
        index = toIndex.front();
        toIndex.pop();
        //cout << index << endl;

        //cout << pola[index] << endl;
        if (index == maxA || index == maxB)
        {
            wynik = pola[index];
            break;
        }

        if ((index + 1) % x != 0 && pola[index + 1] == -1)//w prawo
        {
            pola[index + 1] = pola[index];
            toIndex.push(index + 1);
        }
        if ((index + 1) <= (x * (y - 1)) && pola[index + x] == -1) //w dół
        {
            pola[index + x] = pola[index];
            toIndex.push(index + x);
        }
        if ((index + 1) % x != 1 && pola[index - 1] == -1)//w lewo
        {
            pola[index - 1] = pola[index] + 1;
            toIndex.push(index - 1);
        }
        if ((index + 1) > (x) && pola[index - x] == -1)//w górę
        {
            pola[index - x] = pola[index] + 1;
            toIndex.push(index - x);
        }
    }

    int upwards = (x + y - 2) + wynik;
    int downwards = wynik;
    int a, b, score, times = 0;
    int best = INT_MAX;

    while (n--)
    {
        cin >> a;
        cin >> b;
        score = a * upwards + b * downwards;
        if (score < best)
        {
            best = score;
            times = 1;
        }
        else if (score == best)
        {
            times++;
        }
    }

    cout << best << " " << times << '\n';
}