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
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using D = double;
#define f1 first
#define f2 second
#define randint(a, b) uniform_int_distribution<int>{a, b}(gen)
#ifdef LOC
void OUT() {cout << '\n';}
template<class H, class ... T> void OUT(H h, T ... t)
{
	cout << h << ' ';
	OUT(t...);
}
#define P(...) cout << "[" << #__VA_ARGS__ << "] ", OUT(__VA_ARGS__)
#else
#define P(...)
#define OUT(...)
#endif
//mt19937 gen;

int n, m, k;
LL numUp = 0;
LL numDown = 0;
char xd[2010][2010];
int dist[2010][2010];
int main(int, char ** /*args*/)
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m >> k;
	for(int i = 1; i <= n; ++i)
	{
		cin >> ((xd[i]) + 1);
	}
	queue<pair<int, int>> qu;
	dist[1][1] = 1;
	qu.push({1, 1});
	while(!qu.empty())
	{
		auto t = qu.front();
		qu.pop();
		if(t.f1 == n && t.f2 == m) 
		{
			numUp = n + m - 2;
			numDown = ((dist[t.f1][t.f2] - 1) - numUp) / 2;
			numUp += numDown;
			P(dist[t.f1][t.f2], numUp, numDown);
			P(t.f1, t.f2, n, m);
			break;
		}
		using P = pair<int, int>;
		for(auto [dx, dy] : {P{1, 0}, P{-1, 0}, P{0, 1}, P{0, -1}})
		{
			if(dist[t.f1 + dx][t.f2 + dy] == 0 && xd[t.f1 + dx][t.f2 + dy] == '.')
			{
				dist[t.f1 + dx][t.f2 + dy] = dist[t.f1][t.f2] + 1;
				qu.push({t.f1 + dx, t.f2 + dy});
			}
		}
	}
	pair<LL, int> ans = {1e18, 0};
	while(k--)
	{
		LL a, b;
		cin >> a >> b;
		LL val = a * numUp + b * numDown;
		P(numUp, numDown, a, b, val);
		if(ans.f1 == val) ++ans.f2;
		if(ans.f1 > val) ans = {val, 1};
	}
	cout << ans.f1 << ' ' << ans.f2 << '\n';
	return 0;
}