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
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <unordered_map>
#include <climits>
using namespace std;

void createShortesPath(const vector<vector<char>> &data, vector<vector<int>> &paths, int i, int j, int val)
{
    int n = data.size();
    int m = data[0].size();
    if (i < 0 or i >= n or j < 0 or j >= m)
        return;
    if (data[i][j] == 'X')
        return;
    if (paths[i][j] <= val)
        return;
    else
    {
        paths[i][j] = val;
        createShortesPath(data, paths, i - 1, j, val + 1);
        createShortesPath(data, paths, i + 1, j, val);
        createShortesPath(data, paths, i, j - 1, val + 1);
        createShortesPath(data, paths, i, j + 1, val);
    }
}

void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<char>> data(n, vector<char>(m));
    vector<pair<int, int>> query(k);
    vector<vector<int>> dist(n, vector<int>(m, INT_MAX));
    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        for (int j = 0; j < m; j++)
        {
            data[i][j] = s[j];
        }
    }
    for (int i = 0; i < k; i++)
    {
        cin >> query[i].first >> query[i].second;
    }

    createShortesPath(data, dist, 0, 0, 0);
    int movesBack = dist[n - 1][m - 1];
    int shortestDist = INT_MAX;
    int numOfFreinds = 0;
    for (int i = 0; i < k; i++)
    {
        int a = query[i].first;
        int b = query[i].second;
        int dist = a * (n + m - 2) + (a + b) * movesBack;
        if (dist < shortestDist)
        {
            shortestDist = dist;
            numOfFreinds = 1;
        }
        else if (dist == shortestDist)
        {
            numOfFreinds++;
        }
    }
    cout << shortestDist << ' ' << numOfFreinds << endl;
}

int main()
{
    solve();
    return 0;
}