#include <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define pi pair<int, int> #define MAXN 2001 using namespace std; const ll INF = LLONG_MAX; struct sqr { ll up = INF, down = INF; bool acc = true; bool vis = false; }; bool operator<(sqr a, sqr b) { if (a.up == b.up) return a.down < b.down; else return a.up < b.up; } bool valid(int x, int y, int n, int m) { return x >= 0 && y >= 0 && x < n && y < m; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<vector<sqr>> D(n); string s; for (int i = 0; i < n; i++) { D[i].resize(m); cin >> s; for (int j = 0; j < m; j++) { if (s[j] != '.') D[i][j].acc = false; } } queue<pi> Q; Q.push(make_pair(0, 0)); D[0][0].up = D[0][0].down = 0; D[0][0].vis = true; while (!Q.empty()) { int x = Q.front().fi; int y = Q.front().se; Q.pop(); // cout<<x<<" "<<y<<endl; // cout<<" "<<D[x][y].up<<" "<<D[x][y].down<<endl; for (int ox = -1; ox <= 1; ox++) for (int oy = -1; oy <= 1; oy++) { if (ox * oy != 0 || ox + oy == 0) continue; if (!valid(x + ox, y + oy, n, m)) continue; if (!D[x + ox][y + oy].acc || D[x + ox][y + oy].vis) continue; Q.push(make_pair(x + ox, y + oy)); D[x + ox][y + oy].up = D[x][y].up; D[x + ox][y + oy].down = D[x][y].down; if (ox == 1 || oy == 1) D[x + ox][y + oy].up += 1; else D[x + ox][y + oy].down += 1; D[x + ox][y + oy].vis = true; } } // cout<<D[n-1][m-1].up<<" "<<D[n-1][m-1].down<<" "<<D[n-1][m-1].vis<<endl; ll a, b; ll time = INF; int winners = 0; for (int i = 0; i < k; i++) { cin >> a >> b; ll nt = (ll) a * (ll) D[n - 1][m - 1].up + (ll) b * (ll) D[n - 1][m - 1].down; // cout<<a<<" "<<b<<" "<<nt<<endl; if (nt < time) { time = nt; winners = 1; } else if (nt == time) { winners++; } } cout << time << " " << winners << endl; return 0; }
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 109 110 111 112 | #include <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define pi pair<int, int> #define MAXN 2001 using namespace std; const ll INF = LLONG_MAX; struct sqr { ll up = INF, down = INF; bool acc = true; bool vis = false; }; bool operator<(sqr a, sqr b) { if (a.up == b.up) return a.down < b.down; else return a.up < b.up; } bool valid(int x, int y, int n, int m) { return x >= 0 && y >= 0 && x < n && y < m; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<vector<sqr>> D(n); string s; for (int i = 0; i < n; i++) { D[i].resize(m); cin >> s; for (int j = 0; j < m; j++) { if (s[j] != '.') D[i][j].acc = false; } } queue<pi> Q; Q.push(make_pair(0, 0)); D[0][0].up = D[0][0].down = 0; D[0][0].vis = true; while (!Q.empty()) { int x = Q.front().fi; int y = Q.front().se; Q.pop(); // cout<<x<<" "<<y<<endl; // cout<<" "<<D[x][y].up<<" "<<D[x][y].down<<endl; for (int ox = -1; ox <= 1; ox++) for (int oy = -1; oy <= 1; oy++) { if (ox * oy != 0 || ox + oy == 0) continue; if (!valid(x + ox, y + oy, n, m)) continue; if (!D[x + ox][y + oy].acc || D[x + ox][y + oy].vis) continue; Q.push(make_pair(x + ox, y + oy)); D[x + ox][y + oy].up = D[x][y].up; D[x + ox][y + oy].down = D[x][y].down; if (ox == 1 || oy == 1) D[x + ox][y + oy].up += 1; else D[x + ox][y + oy].down += 1; D[x + ox][y + oy].vis = true; } } // cout<<D[n-1][m-1].up<<" "<<D[n-1][m-1].down<<" "<<D[n-1][m-1].vis<<endl; ll a, b; ll time = INF; int winners = 0; for (int i = 0; i < k; i++) { cin >> a >> b; ll nt = (ll) a * (ll) D[n - 1][m - 1].up + (ll) b * (ll) D[n - 1][m - 1].down; // cout<<a<<" "<<b<<" "<<nt<<endl; if (nt < time) { time = nt; winners = 1; } else if (nt == time) { winners++; } } cout << time << " " << winners << endl; return 0; } |