#include <iostream> #include <vector> #include <algorithm> using namespace std; const int N = 2020; const long long INF = (1LL<<62); const int DIRS[][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; int p(int x, int y) { return (x<<13)|y; } int x(int p) { return (p>>13); } int y(int p) { return (p&((1<<13)-1)); } int n, m, k; string s[N]; bool vis[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i=0; i<n; i++) cin >> s[i]; vector<int> q = {p(0, 0)}; vis[0][0] = true; int dist = 0; while (!vis[n-1][m-1]) { vector<int> nq; for (int v: q) { for (auto dir: DIRS) { int nx = x(v)+dir[0], ny = y(v)+dir[1]; if (0 <= nx && nx < n && 0 <= ny && ny < m && s[nx][ny] == '.' && !vis[nx][ny]) { vis[nx][ny] = true; nq.push_back(p(nx, ny)); } } } dist++; nq.swap(q); } long long a, b, necessary = (n+m-2), unnecessary = dist - (n+m-2); vector<long long> times; for (int i=0; i<k; i++) { cin >> a >> b; times.push_back(a * necessary + (a+b) * (unnecessary / 2)); } long long best_time = *min_element(times.begin(), times.end()); int cnt = count(times.begin(), times.end(), best_time); cout << best_time << " " << cnt << "\n"; 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; const int N = 2020; const long long INF = (1LL<<62); const int DIRS[][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; int p(int x, int y) { return (x<<13)|y; } int x(int p) { return (p>>13); } int y(int p) { return (p&((1<<13)-1)); } int n, m, k; string s[N]; bool vis[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i=0; i<n; i++) cin >> s[i]; vector<int> q = {p(0, 0)}; vis[0][0] = true; int dist = 0; while (!vis[n-1][m-1]) { vector<int> nq; for (int v: q) { for (auto dir: DIRS) { int nx = x(v)+dir[0], ny = y(v)+dir[1]; if (0 <= nx && nx < n && 0 <= ny && ny < m && s[nx][ny] == '.' && !vis[nx][ny]) { vis[nx][ny] = true; nq.push_back(p(nx, ny)); } } } dist++; nq.swap(q); } long long a, b, necessary = (n+m-2), unnecessary = dist - (n+m-2); vector<long long> times; for (int i=0; i<k; i++) { cin >> a >> b; times.push_back(a * necessary + (a+b) * (unnecessary / 2)); } long long best_time = *min_element(times.begin(), times.end()); int cnt = count(times.begin(), times.end(), best_time); cout << best_time << " " << cnt << "\n"; return 0; } |