#include <bits/stdc++.h> #define int long long using namespace std; const int N = 2009; char avail[N][N]; int dist[N][N]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; void bfs(int i, int j) { vector<pair<int, int>> q; q.push_back(make_pair(i, j)); for (int i = 0; i < q.size(); i++) { pair<int, int> v = q[i]; for (int j = 0; j < 4; j++) { int x1 = v.first + dx[j]; int y1 = v.second + dy[j]; if (avail[x1][y1] && dist[x1][y1] == -1) { dist[x1][y1] = dist[v.first][v.second] + 1; q.push_back(make_pair(x1, y1)); } } } } void solve() { int n, m, q; cin >> n >> m >> q; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char x; cin >> x; if (x == '.') avail[i][j] = 1, dist[i][j] = -1; } } dist[1][1] = 0; bfs(1, 1); int res = 0, min_time = 1e18; for (int i = 0; i < q; i++) { int a, b; cin >> b >> a; int time = (m + n - 2) * b + (a + b) * (dist[n][m] - n - m + 2) / 2; if (time < min_time) { min_time = time; res = 1; } else if (time == min_time) { res++; } } cout << min_time << " " << res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //cout << fixed << setprecision(12); int t = 1; //cin >> t; while(t--) solve(); 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 | #include <bits/stdc++.h> #define int long long using namespace std; const int N = 2009; char avail[N][N]; int dist[N][N]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; void bfs(int i, int j) { vector<pair<int, int>> q; q.push_back(make_pair(i, j)); for (int i = 0; i < q.size(); i++) { pair<int, int> v = q[i]; for (int j = 0; j < 4; j++) { int x1 = v.first + dx[j]; int y1 = v.second + dy[j]; if (avail[x1][y1] && dist[x1][y1] == -1) { dist[x1][y1] = dist[v.first][v.second] + 1; q.push_back(make_pair(x1, y1)); } } } } void solve() { int n, m, q; cin >> n >> m >> q; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char x; cin >> x; if (x == '.') avail[i][j] = 1, dist[i][j] = -1; } } dist[1][1] = 0; bfs(1, 1); int res = 0, min_time = 1e18; for (int i = 0; i < q; i++) { int a, b; cin >> b >> a; int time = (m + n - 2) * b + (a + b) * (dist[n][m] - n - m + 2) / 2; if (time < min_time) { min_time = time; res = 1; } else if (time == min_time) { res++; } } cout << min_time << " " << res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //cout << fixed << setprecision(12); int t = 1; //cin >> t; while(t--) solve(); return 0; } |