#include<bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.ST << ", " << p.ND << ")"; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto &e : x) out << e << (&e == &*--x.end() ? "" : ", "); return out << '}'; } template<class... Args> void dump(Args&&... args) { ((cerr << args << "; "), ...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << "\n" #else # define debug(...) false #endif template<class T> int size(T && a) { return (int) a.size(); } using LL = long long; using PII = pair<int, int>; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<string> plane(n); REP(i, n) cin >> plane[i]; vector dst(n, vector<int>(m, -1)); array dx = {1, -1, 0, 0}; array dy = {0, 0, 1, -1}; dst[0][0] = 0; deque<PII> Q = {{0, 0}}; while(!Q.empty()) { auto [x, y] = Q.front(); Q.pop_front(); REP(d, 4) { int a = x + dx[d], b = y + dy[d]; if(0 <= a && a < n && 0 <= b && b < m && dst[a][b] == -1) { if(plane[a][b] == '.') { dst[a][b] = dst[x][y] + 1; Q.emplace_back(a, b); } } } } int down = (n - 1) + (m - 1); int bonus = dst[n - 1][m - 1] - down; int up = bonus / 2; down += bonus / 2; debug(up, down); LL ans = 1e18, cnt = 0; REP(i, k) { LL a, b; cin >> a >> b; LL score = a * down + b * up; if(score < ans) ans = score, cnt = 0; if(score == ans) cnt++; } cout << ans << " " << cnt << "\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 | #include<bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.ST << ", " << p.ND << ")"; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto &e : x) out << e << (&e == &*--x.end() ? "" : ", "); return out << '}'; } template<class... Args> void dump(Args&&... args) { ((cerr << args << "; "), ...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << "\n" #else # define debug(...) false #endif template<class T> int size(T && a) { return (int) a.size(); } using LL = long long; using PII = pair<int, int>; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<string> plane(n); REP(i, n) cin >> plane[i]; vector dst(n, vector<int>(m, -1)); array dx = {1, -1, 0, 0}; array dy = {0, 0, 1, -1}; dst[0][0] = 0; deque<PII> Q = {{0, 0}}; while(!Q.empty()) { auto [x, y] = Q.front(); Q.pop_front(); REP(d, 4) { int a = x + dx[d], b = y + dy[d]; if(0 <= a && a < n && 0 <= b && b < m && dst[a][b] == -1) { if(plane[a][b] == '.') { dst[a][b] = dst[x][y] + 1; Q.emplace_back(a, b); } } } } int down = (n - 1) + (m - 1); int bonus = dst[n - 1][m - 1] - down; int up = bonus / 2; down += bonus / 2; debug(up, down); LL ans = 1e18, cnt = 0; REP(i, k) { LL a, b; cin >> a >> b; LL score = a * down + b * up; if(score < ans) ans = score, cnt = 0; if(score == ans) cnt++; } cout << ans << " " << cnt << "\n"; } |