#include <iostream> #include <vector> #include <map> #include <utility> #include <queue> using namespace std; map<unsigned long long, int> result; vector<pair<pair<int, int>, int>> graph[2003][2003]; int dist[2003][2003][2]; bool visited[2003][2003]; int main() { int n, m, k; cin >> n >> m >> k; bool safe[89]; safe[46] = true; safe[88] = false; char c; for (int x = 1; x <= n; x++) { for (int y = 1; y <= m; y++) { cin >> c; if (safe[(int)c]) { graph[x - 1][y].push_back(make_pair(make_pair(x,y), 0)); graph[x + 1][y].push_back(make_pair(make_pair(x, y), 1)); graph[x][y - 1].push_back(make_pair(make_pair(x, y), 0)); graph[x][y + 1].push_back(make_pair(make_pair(x, y), 1)); } } } queue<pair<int,int>> q; pair<int, int> p; q.push(make_pair(1,1)); long long ac, bc; while (q.size() > 0) { p = q.front(); if (!visited[p.first][p.second]) { if (p.first == n && p.second == m) { ac = dist[p.first][p.second][0]; bc = dist[p.first][p.second][1]; } visited[p.first][p.second] = true; for (pair<pair<int,int>, int> n : graph[p.first][p.second]) { q.push(n.first); dist[n.first.first][n.first.second][0] = dist[p.first][p.second][0]; dist[n.first.first][n.first.second][1] = dist[p.first][p.second][1]; dist[n.first.first][n.first.second][n.second]++; } } q.pop(); } long long a, b; long long r, minr; minr = 100000000000000000; for (int i = 0; i < k; i++) { cin >> a >> b; r = ac * a + bc * b; result[r]++; minr = min(minr, r); } printf("%lld %d", minr, result[minr]); }
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 | #include <iostream> #include <vector> #include <map> #include <utility> #include <queue> using namespace std; map<unsigned long long, int> result; vector<pair<pair<int, int>, int>> graph[2003][2003]; int dist[2003][2003][2]; bool visited[2003][2003]; int main() { int n, m, k; cin >> n >> m >> k; bool safe[89]; safe[46] = true; safe[88] = false; char c; for (int x = 1; x <= n; x++) { for (int y = 1; y <= m; y++) { cin >> c; if (safe[(int)c]) { graph[x - 1][y].push_back(make_pair(make_pair(x,y), 0)); graph[x + 1][y].push_back(make_pair(make_pair(x, y), 1)); graph[x][y - 1].push_back(make_pair(make_pair(x, y), 0)); graph[x][y + 1].push_back(make_pair(make_pair(x, y), 1)); } } } queue<pair<int,int>> q; pair<int, int> p; q.push(make_pair(1,1)); long long ac, bc; while (q.size() > 0) { p = q.front(); if (!visited[p.first][p.second]) { if (p.first == n && p.second == m) { ac = dist[p.first][p.second][0]; bc = dist[p.first][p.second][1]; } visited[p.first][p.second] = true; for (pair<pair<int,int>, int> n : graph[p.first][p.second]) { q.push(n.first); dist[n.first.first][n.first.second][0] = dist[p.first][p.second][0]; dist[n.first.first][n.first.second][1] = dist[p.first][p.second][1]; dist[n.first.first][n.first.second][n.second]++; } } q.pop(); } long long a, b; long long r, minr; minr = 100000000000000000; for (int i = 0; i < k; i++) { cin >> a >> b; r = ac * a + bc * b; result[r]++; minr = min(minr, r); } printf("%lld %d", minr, result[minr]); } |