#include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<long long, long long> #define FOR(x, j) for(int x = 0; x < j; x++) #define ll long long const int INF = 1e17; const int maxN = 1e6+10; int A[2005][2005]; pii P[2005][2005]; bool visited[2005][2005]; int n, m, k; bool in_bounds(int i, int j){ if(i < 0 || i >= n) return false; if(j < 0 || j >= m) return false; if(visited[i][j]) return false; if(A[i][j] == 1) return false; return true; } int D_X[] = {1, -1, 0, 0}; int D_Y[] = {0, 0, 1, -1}; void solve(){ cin >> n >> m >> k; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){ char c; cin >> c; if(c == '.') A[i][j] = 0; else A[i][j] = 1; } P[0][0] = {-1, -1}; queue<pii> q; q.push({0, 0}); visited[0][0] = true; while(!q.empty()){ pii curr = q.front(); q.pop(); //cout << "CURRENT POSITION:" << curr.first << " " << curr.second << "\n"; for(int i = 0; i < 4; i++){ int new_x = curr.second + D_X[i]; int new_y = curr.first + D_Y[i]; //cout << "CHECK: " << new_y << " " << new_x << "\n"; if(in_bounds(new_y, new_x)){ P[new_y][new_x] = curr; visited[new_y][new_x] = true; q.push({new_y, new_x}); } } } pii curr = {n-1, m-1}; pii p = P[curr.first][curr.second]; int count_h = 0, count_l = 0; while(p.first != -1){ if(p.second < curr.second) count_h++; else if(p.second > curr.second) count_l++; else if(p.first < curr.first) count_h++; else count_l++; curr = p; p = P[curr.first][curr.second]; } pii K[(int)(1e6+5)]; int best_min = INF; for(int i = 0; i < k; i++){ int a, b; cin >> a >> b; best_min = min(best_min, count_h*a + count_l*b); K[i] = {a, b}; } int am = 0; for(int i = 0; i < k; i++){ int curr = count_h*K[i].first + count_l*K[i].second; if(best_min == curr) am++; } cout << best_min << " " << am << "\n"; } #undef int int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); cout.tie(0); bool multitest = false; //multitest = true; if (multitest) { int t; cin >> t; while (t--) solve(); } else solve(); }
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 | #include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<long long, long long> #define FOR(x, j) for(int x = 0; x < j; x++) #define ll long long const int INF = 1e17; const int maxN = 1e6+10; int A[2005][2005]; pii P[2005][2005]; bool visited[2005][2005]; int n, m, k; bool in_bounds(int i, int j){ if(i < 0 || i >= n) return false; if(j < 0 || j >= m) return false; if(visited[i][j]) return false; if(A[i][j] == 1) return false; return true; } int D_X[] = {1, -1, 0, 0}; int D_Y[] = {0, 0, 1, -1}; void solve(){ cin >> n >> m >> k; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){ char c; cin >> c; if(c == '.') A[i][j] = 0; else A[i][j] = 1; } P[0][0] = {-1, -1}; queue<pii> q; q.push({0, 0}); visited[0][0] = true; while(!q.empty()){ pii curr = q.front(); q.pop(); //cout << "CURRENT POSITION:" << curr.first << " " << curr.second << "\n"; for(int i = 0; i < 4; i++){ int new_x = curr.second + D_X[i]; int new_y = curr.first + D_Y[i]; //cout << "CHECK: " << new_y << " " << new_x << "\n"; if(in_bounds(new_y, new_x)){ P[new_y][new_x] = curr; visited[new_y][new_x] = true; q.push({new_y, new_x}); } } } pii curr = {n-1, m-1}; pii p = P[curr.first][curr.second]; int count_h = 0, count_l = 0; while(p.first != -1){ if(p.second < curr.second) count_h++; else if(p.second > curr.second) count_l++; else if(p.first < curr.first) count_h++; else count_l++; curr = p; p = P[curr.first][curr.second]; } pii K[(int)(1e6+5)]; int best_min = INF; for(int i = 0; i < k; i++){ int a, b; cin >> a >> b; best_min = min(best_min, count_h*a + count_l*b); K[i] = {a, b}; } int am = 0; for(int i = 0; i < k; i++){ int curr = count_h*K[i].first + count_l*K[i].second; if(best_min == curr) am++; } cout << best_min << " " << am << "\n"; } #undef int int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); cout.tie(0); bool multitest = false; //multitest = true; if (multitest) { int t; cin >> t; while (t--) solve(); } else solve(); } |