#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <climits> using namespace std; void input(long long &n, long long &m, long long &k, vector<string> &arr, vector<pair<long long, long long>> &q) { string x; long long a, b; cin >> n >> m >> k; for (long long i = 0; i < n; i++) { cin >> x; arr.push_back(x); } for (long long i = 0; i < k; i++) { cin >> a >> b; q.push_back({a, b}); } } void solve (long long &n, long long &m, long long &k, vector<string> &arr, vector<pair<long long, long long>> &q) { vector<vector<long long>> dist(n, vector<long long>(m, INT_MAX)); pair<long long, long long> result = {INT_MAX, INT_MAX}; vector<tuple<int,int,int,int>> qe = {{0,0,0,0}}; for (int i = 0; i < qe.size(); i++) { auto [a, b, u, d] = qe[i]; if (a < 0 || b < 0 || a >= arr.size() || b >= arr[a].size() || arr[a][b] == 'X') continue; if (u + d >= dist[a][b]) continue; dist[a][b] = u + d; if (a == arr.size() - 1 && b == arr[a].size() - 1) { result = {u, d}; continue; } qe.push_back({a + 1, b, u + 1, d}); qe.push_back({a, b + 1, u + 1, d}); qe.push_back({a - 1, b, u, d + 1}); qe.push_back({a, b - 1, u, d + 1}); } unordered_map<long long, long long> res; long long best_res = -1; for (auto [u, d] : q) { long long ovr = (result.first * u) + (result.second * d); if (best_res == -1 || ovr < best_res) best_res = ovr; res[ovr]++; } cout << best_res << " " << res[best_res] << "\n"; } int main() { ios_base::sync_with_stdio(0); long long n, m, k; vector<string> arr; vector<pair<long long, long long>> q; input(n, m, k, arr, q); solve(n, m, k, arr, q); 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 | #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <climits> using namespace std; void input(long long &n, long long &m, long long &k, vector<string> &arr, vector<pair<long long, long long>> &q) { string x; long long a, b; cin >> n >> m >> k; for (long long i = 0; i < n; i++) { cin >> x; arr.push_back(x); } for (long long i = 0; i < k; i++) { cin >> a >> b; q.push_back({a, b}); } } void solve (long long &n, long long &m, long long &k, vector<string> &arr, vector<pair<long long, long long>> &q) { vector<vector<long long>> dist(n, vector<long long>(m, INT_MAX)); pair<long long, long long> result = {INT_MAX, INT_MAX}; vector<tuple<int,int,int,int>> qe = {{0,0,0,0}}; for (int i = 0; i < qe.size(); i++) { auto [a, b, u, d] = qe[i]; if (a < 0 || b < 0 || a >= arr.size() || b >= arr[a].size() || arr[a][b] == 'X') continue; if (u + d >= dist[a][b]) continue; dist[a][b] = u + d; if (a == arr.size() - 1 && b == arr[a].size() - 1) { result = {u, d}; continue; } qe.push_back({a + 1, b, u + 1, d}); qe.push_back({a, b + 1, u + 1, d}); qe.push_back({a - 1, b, u, d + 1}); qe.push_back({a, b - 1, u, d + 1}); } unordered_map<long long, long long> res; long long best_res = -1; for (auto [u, d] : q) { long long ovr = (result.first * u) + (result.second * d); if (best_res == -1 || ovr < best_res) best_res = ovr; res[ovr]++; } cout << best_res << " " << res[best_res] << "\n"; } int main() { ios_base::sync_with_stdio(0); long long n, m, k; vector<string> arr; vector<pair<long long, long long>> q; input(n, m, k, arr, q); solve(n, m, k, arr, q); return 0; } |