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
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,pa;
char c;
queue <pair<int,int> > Q;
pair<int,int> ob;
int Pole[2004][2004],P[2004][2004],L[2004][2004],G[2004][2004],D[2004][2004],odw[2004][2004];
main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> a >> b >> pa;
    for (int i = 1 ; i <= a ; i++)
    {
        for (int j = 1 ; j <= b ; j++)
        {
            cin >> c;
            if(c=='X')
                Pole[i][j] = 1;

        }
    }
    Q.push({1,1});
    odw[1][1] = 1;
    while(!Q.empty())
    {
        ob = Q.front();
        Q.pop();
        int x = ob.first;
        int y = ob.second;

        if(odw[x+1][y] == 0 && Pole[x+1][y] == 0 && x+1 <= a)
        {
            Q.push({x+1,y});
            odw[x+1][y] = 1;
            P[x+1][y] = P[x][y];
            L[x+1][y] = L[x][y];
            G[x+1][y] = G[x][y];
            D[x+1][y] = D[x][y] + 1;
        }

        //idziemy w gore
        if(odw[x-1][y] == 0 && Pole[x-1][y] == 0 && x-1 > 0)
        {
            Q.push({x-1,y});
            odw[x-1][y] = 1;
            P[x-1][y] = P[x][y];
            L[x-1][y] = L[x][y];
            G[x-1][y] = G[x][y] + 1;
            D[x-1][y] = D[x][y];
        }
        //idziemy w lewo
        if(odw[x][y-1] == 0 && Pole[x][y-1] == 0 && y-1 > 0)
        {
            Q.push({x,y-1});
            odw[x][y-1] = 1;
            P[x][y-1] = P[x][y];
            L[x][y-1] = L[x][y] + 1;
            G[x][y-1] = G[x][y];
            D[x][y-1] = D[x][y];
        }
        //idziemy w prawo
        if(odw[x][y+1] == 0 && Pole[x][y+1] == 0 && y+1 <= b)
        {
            Q.push({x,y+1});
            odw[x][y+1] = 1;
            P[x][y+1] = P[x][y] + 1;
            L[x][y+1] = L[x][y];
            G[x][y+1] = G[x][y];
            D[x][y+1] = D[x][y];
        }
    }
    int maxw = LONG_LONG_MAX;
    int liczba = 0;
    for (int i = 1 ; i <= pa ; i++)
    {
        int x;
        int y;
        cin >> x >> y;
        int temp = x*(P[a][b] + D[a][b]) + y*(L[a][b]+G[a][b]);
        if(temp < maxw)
        {
            maxw = temp;
            liczba = 1;
        }
        else if(temp == maxw)
            liczba++;

    }
    cout << maxw <<" "<<liczba;
    //cout << P[a][b] << " " << L[a][b] << " "<<G[a][b] << " " << D[a][b];
}