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
#include<bits/stdc++.h>

using namespace std;

int n, m ,k;

#define correctCoord(x, y) x >= 0 && x < m && y >= 0 && y < n

struct Node{
    int x, y;
    bool visited, type;
    int depth[2];
    Node(int x, int y, bool type):visited(0),depth{0}{
        this->x = x;
        this->y = y;
        this->type = type;
    }
};

inline void addNodeToQue(queue<Node*> & que, Node* toAdd, Node* current){
    que.push(toAdd);
    int depthToAdd = 0;
    if(toAdd->x > current->x || toAdd->y > current->y) depthToAdd = 1;
    current->depth[depthToAdd] += 1;
    toAdd->depth[0] = current->depth[0];
    toAdd->depth[1] = current->depth[1];
    current->depth[depthToAdd] -= 1;

    toAdd->visited = 1;
}

Node* calcWay(vector <vector<Node*> > & maps){
    queue<Node*> toVisit;
    toVisit.push(maps[0][0]);
    maps[0][0]->visited=1;
    while(!toVisit.empty()){
        Node * current = toVisit.front();
        toVisit.pop();

        int x = current->x, y = current->y;

        if(x == m-1 && y == n-1) return current;

        if(correctCoord(x, y+1) && !maps[y+1][x]->visited && maps[y+1][x]->type) addNodeToQue(toVisit, maps[y+1][x], current);
        if(correctCoord(x+1, y) && !maps[y][x+1]->visited && maps[y][x+1]->type) addNodeToQue(toVisit, maps[y][x+1], current);
        if(correctCoord(x-1, y) && !maps[y][x-1]->visited && maps[y][x-1]->type) addNodeToQue(toVisit, maps[y][x-1], current);
        if(correctCoord(x, y-1) && !maps[y-1][x]->visited && maps[y-1][x]->type) addNodeToQue(toVisit, maps[y-1][x], current);
    }
}

int main(){

    ios_base::sync_with_stdio(0);
    cin >> n >> m >> k;
    char z; bool type;
    vector< vector<Node*> > maps;
    for(int y=0; y<n; y++){
        vector<Node*> row;
        maps.push_back(row);
        for(int x=0; x<m; x++){
            cin >> z;
            type = 0;
            if(z == '.'){
                type = 1;
            }
            maps[y].push_back(new Node(x, y, type));
        }
    }

    Node * final = calcWay(maps);
    unsigned long long best, result;
    int count = 1;
    int a, b;
    cin >> a >> b;
    best = a*final->depth[1]+b*final->depth[0];
    for(int i=1; i<k; i++){
        cin >> a >> b;
        result = a*final->depth[1]+b*final->depth[0];
        if(result < best){
            best = result;
            count = 0;
        }
        if(result == best) ++count;
    }
    cout << best << " " << count << "\n";
}