#include <iostream> #include <queue> using namespace std; struct Field { bool safe; int visitAt; Field() { } }; Field **board; int n, m; int BFS() { queue<pair<int, int>> visited; visited.push(make_pair(0, 0)); board[0][0].visitAt = 0; while (!visited.empty()) { pair<int, int> coords = visited.front(); visited.pop(); // cout << "qwe " << coords.first << " " << coords.second << endl; if (coords.first == n - 1 && coords.second == m - 1) { return board[coords.first][coords.second].visitAt; } if (coords.first - 1 >= 0 && board[coords.first - 1][coords.second].visitAt < 0 && board[coords.first - 1][coords.second].safe) { board[coords.first - 1][coords.second].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first - 1, coords.second)); } if (coords.first + 1 < n && board[coords.first + 1][coords.second].visitAt < 0 && board[coords.first + 1][coords.second].safe) { board[coords.first + 1][coords.second].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first + 1, coords.second)); } if (coords.second - 1 >= 0 && board[coords.first][coords.second - 1].visitAt < 0 && board[coords.first][coords.second - 1].safe) { board[coords.first][coords.second - 1].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first, coords.second - 1)); } if (coords.second + 1 < m && board[coords.first][coords.second + 1].visitAt < 0 && board[coords.first][coords.second + 1].safe) { board[coords.first][coords.second + 1].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first, coords.second + 1)); } } return -1; } int main() { ios_base::sync_with_stdio(0); int k; long long a, b; char c; cin >> n; cin >> m; cin >> k; board = new Field*[n]; for (int i = 0; i < n; ++i) { board[i] = new Field[m]; for (int j = 0; j < m; ++j) { cin >> c; board[i][j].safe = (c == '.'); board[i][j].visitAt = -1; } } int steps = BFS(); long long stepsUp = n + m - 2; long long stepsDown = (steps - stepsUp) / 2; stepsUp = steps - stepsDown; long long result = -1; int howMany; for (int i = 0; i < k; ++i) { cin >> a; cin >> b; long long time = a * stepsUp + b * stepsDown; if (result < 0 || time < result) { result = time; howMany = 1; } else if (time == result) { ++howMany; } } cout << result << " " << howMany << endl; 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 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 | #include <iostream> #include <queue> using namespace std; struct Field { bool safe; int visitAt; Field() { } }; Field **board; int n, m; int BFS() { queue<pair<int, int>> visited; visited.push(make_pair(0, 0)); board[0][0].visitAt = 0; while (!visited.empty()) { pair<int, int> coords = visited.front(); visited.pop(); // cout << "qwe " << coords.first << " " << coords.second << endl; if (coords.first == n - 1 && coords.second == m - 1) { return board[coords.first][coords.second].visitAt; } if (coords.first - 1 >= 0 && board[coords.first - 1][coords.second].visitAt < 0 && board[coords.first - 1][coords.second].safe) { board[coords.first - 1][coords.second].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first - 1, coords.second)); } if (coords.first + 1 < n && board[coords.first + 1][coords.second].visitAt < 0 && board[coords.first + 1][coords.second].safe) { board[coords.first + 1][coords.second].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first + 1, coords.second)); } if (coords.second - 1 >= 0 && board[coords.first][coords.second - 1].visitAt < 0 && board[coords.first][coords.second - 1].safe) { board[coords.first][coords.second - 1].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first, coords.second - 1)); } if (coords.second + 1 < m && board[coords.first][coords.second + 1].visitAt < 0 && board[coords.first][coords.second + 1].safe) { board[coords.first][coords.second + 1].visitAt = board[coords.first][coords.second].visitAt + 1; visited.push(make_pair(coords.first, coords.second + 1)); } } return -1; } int main() { ios_base::sync_with_stdio(0); int k; long long a, b; char c; cin >> n; cin >> m; cin >> k; board = new Field*[n]; for (int i = 0; i < n; ++i) { board[i] = new Field[m]; for (int j = 0; j < m; ++j) { cin >> c; board[i][j].safe = (c == '.'); board[i][j].visitAt = -1; } } int steps = BFS(); long long stepsUp = n + m - 2; long long stepsDown = (steps - stepsUp) / 2; stepsUp = steps - stepsDown; long long result = -1; int howMany; for (int i = 0; i < k; ++i) { cin >> a; cin >> b; long long time = a * stepsUp + b * stepsDown; if (result < 0 || time < result) { result = time; howMany = 1; } else if (time == result) { ++howMany; } } cout << result << " " << howMany << endl; return 0; } |