#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ff first
#define ss second
using ll = long long;
using pii = pair<int, int>;
template<class T> int size(T && a) {
return (int)(a.size());
}
ostream& operator << (ostream &os, string str) {
for(char c : str) os << c;
return os;
}
template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) {
return os << '(' << p.ff << "," << p.ss << ')';
}
template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) {
os << '{';
for(auto it = x.begin(); it != x.end(); ++it)
os << *it << (it == prev(x.end()) ? "" : " ");
return os << '}';
}
template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) {
for(auto x : vec)
os << "\n " << x;
return os;
}
void dump() {}
template <class T, class... Args> void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
#ifdef DEBUG
# define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n'
#else
# define debug(...) 0
#endif
//--------------------------------------------------
const int INF = int(1e9);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<string> board(n);
REP(i, n) cin >> board[i];
vector<vector<int>> graph(n*m);
REP(i, n) REP(j, m) {
if(board[i][j] == 'X')
continue;
int a = m*i+j;
if(i > 0 and board[i-1][j] != 'X') {
int b = m*(i-1)+j;
graph[a].emplace_back(b);
graph[b].emplace_back(a);
}
if(j > 0 and board[i][j-1] != 'X') {
int b = m*i+j-1;
graph[a].emplace_back(b);
graph[b].emplace_back(a);
}
}
vector<int> dist(n*m, INF);
queue<int> Q;
Q.push(0);
dist[0] = 0;
while(!Q.empty()) {
int v = Q.front();
Q.pop();
for(int u : graph[v]) {
if(dist[u] == INF) {
dist[u] = dist[v] + 1;
Q.push(u);
}
}
}
debug(dist);
ll c1 = n+m-2, c2 = (dist[n*m-1]-n-m+2)/2;
pair<ll, int> ans = pair(ll(INF) * INF, 0);
REP(i, k) {
ll a, b;
cin >> a >> b;
ll temp = a*c1 + (a+b)*c2;
if(temp < ans.ff)
ans = pair(temp, 1);
else if(temp == ans.ff)
++ans.ss;
}
cout << ans.ff << ' ' << ans.ss << '\n';
}
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ff first #define ss second using ll = long long; using pii = pair<int, int>; template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.ff << "," << p.ss << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- const int INF = int(1e9); int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<string> board(n); REP(i, n) cin >> board[i]; vector<vector<int>> graph(n*m); REP(i, n) REP(j, m) { if(board[i][j] == 'X') continue; int a = m*i+j; if(i > 0 and board[i-1][j] != 'X') { int b = m*(i-1)+j; graph[a].emplace_back(b); graph[b].emplace_back(a); } if(j > 0 and board[i][j-1] != 'X') { int b = m*i+j-1; graph[a].emplace_back(b); graph[b].emplace_back(a); } } vector<int> dist(n*m, INF); queue<int> Q; Q.push(0); dist[0] = 0; while(!Q.empty()) { int v = Q.front(); Q.pop(); for(int u : graph[v]) { if(dist[u] == INF) { dist[u] = dist[v] + 1; Q.push(u); } } } debug(dist); ll c1 = n+m-2, c2 = (dist[n*m-1]-n-m+2)/2; pair<ll, int> ans = pair(ll(INF) * INF, 0); REP(i, k) { ll a, b; cin >> a >> b; ll temp = a*c1 + (a+b)*c2; if(temp < ans.ff) ans = pair(temp, 1); else if(temp == ans.ff) ++ans.ss; } cout << ans.ff << ' ' << ans.ss << '\n'; } |
English