#include <iostream> #include <list> #include <map> struct Route { int row; int column; int down; int up; }; using Routes = std::list<Route>; bool is_ok(char **data, int row, int column, int rows, int columns) { if (row < 1 || column < 1 || rows < row || columns < column) return false; return data[row][column] != 'X'; } Route solve(char **data, Routes &routes, int rows, int columns) { while (!routes.empty()) { Route route = routes.front(); routes.pop_front(); const int row = route.row; const int column = route.column; if (row == rows && column == columns) return route; if (is_ok(data, row - 1, column, rows, columns)) { routes.push_back({row - 1, column, route.down + 1, route.up}); data[row - 1][column] = 'X'; } if (is_ok(data, row + 1, column, rows, columns)) { routes.push_back({row + 1, column, route.down, route.up + 1}); data[row + 1][column] = 'X'; } if (is_ok(data, row, column - 1, rows, columns)) { routes.push_back({row, column - 1, route.down + 1, route.up}); data[row][column - 1] = 'X'; } if (is_ok(data, row, column + 1, rows, columns)) { routes.push_back({row, column + 1, route.down, route.up + 1}); data[row][column + 1] = 'X'; } } return {}; } int main() { int rows, columns, k; std::cin >> rows >> columns >> k; char **data; data = new char *[rows + 1]; for (int row = 1; row <= rows; ++row) { data[row] = new char[columns + 1]; for (int column = 1; column <= columns; ++column) { std::cin >> data[row][column]; } } Routes routes; routes.push_back({1, 1, 0, 0}); data[1][1] = 'X'; Route route = solve(data, routes, rows, columns); const long long int best_down = route.down; const long long int best_up = route.up; int down, up; std::map<long long int, int> cost; for (int i = 0; i < k; ++i) { std::cin >> up >> down; cost[best_down * down + best_up * up]++; } const auto best = cost.begin(); printf("%lld %d\n", best->first, best->second); 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 | #include <iostream> #include <list> #include <map> struct Route { int row; int column; int down; int up; }; using Routes = std::list<Route>; bool is_ok(char **data, int row, int column, int rows, int columns) { if (row < 1 || column < 1 || rows < row || columns < column) return false; return data[row][column] != 'X'; } Route solve(char **data, Routes &routes, int rows, int columns) { while (!routes.empty()) { Route route = routes.front(); routes.pop_front(); const int row = route.row; const int column = route.column; if (row == rows && column == columns) return route; if (is_ok(data, row - 1, column, rows, columns)) { routes.push_back({row - 1, column, route.down + 1, route.up}); data[row - 1][column] = 'X'; } if (is_ok(data, row + 1, column, rows, columns)) { routes.push_back({row + 1, column, route.down, route.up + 1}); data[row + 1][column] = 'X'; } if (is_ok(data, row, column - 1, rows, columns)) { routes.push_back({row, column - 1, route.down + 1, route.up}); data[row][column - 1] = 'X'; } if (is_ok(data, row, column + 1, rows, columns)) { routes.push_back({row, column + 1, route.down, route.up + 1}); data[row][column + 1] = 'X'; } } return {}; } int main() { int rows, columns, k; std::cin >> rows >> columns >> k; char **data; data = new char *[rows + 1]; for (int row = 1; row <= rows; ++row) { data[row] = new char[columns + 1]; for (int column = 1; column <= columns; ++column) { std::cin >> data[row][column]; } } Routes routes; routes.push_back({1, 1, 0, 0}); data[1][1] = 'X'; Route route = solve(data, routes, rows, columns); const long long int best_down = route.down; const long long int best_up = route.up; int down, up; std::map<long long int, int> cost; for (int i = 0; i < k; ++i) { std::cin >> up >> down; cost[best_down * down + best_up * up]++; } const auto best = cost.begin(); printf("%lld %d\n", best->first, best->second); return 0; } |