#include <cstdint> #include <iostream> #include <string> #include <vector> #include <cstring> #include <limits> #include <set> struct Speed { Speed(std::uint64_t a, std::uint64_t b) : a{a} , b{b} { } std::uint64_t a; std::uint64_t b; }; enum class Dir : std::uint8_t { up, down, left, right, none, }; Dir reverse(Dir dir) { switch (dir) { case Dir::up: return Dir::down; case Dir::down: return Dir::up; case Dir::left: return Dir::right; case Dir::right: return Dir::left; case Dir::none: return Dir::none; } return Dir::none; } std::string to_string(Dir d) { switch (d) { case Dir::up: return "up"; case Dir::down: return "down"; case Dir::left: return "left"; case Dir::right: return "right"; case Dir::none: return "none"; } return "empty"; } static std::vector<std::vector<Dir>> all_paths; static std::uint64_t n, m, k; int stop = 100; void find_path( std::uint64_t x, std::uint64_t y, char** map, Dir last_move, std::vector<Dir> current_path) { // std::cout << "find_path, x=" << x << " y=" << y << " stop: " << stop // << " lm: " << to_string(last_move) << "\n"; // char** new_map = new char*[n + 2]; // for (std::uint64_t i = 0; i < n + 2; ++i) { // new_map[i] = new char[m + 2]; // std::memcpy(new_map[i], map[i], m+2); // } current_path.emplace_back(last_move); // new_map[y][x] = '*'; map[y][x] = '*'; // made it to the top if (x == m && y == n) { all_paths.emplace_back(current_path); return; } // if (--stop < 0) { // return; // } // for (std::uint64_t i = 0; i < n + 2; ++i) { // for (std::uint64_t j = 0; j < m + 2; ++j) { // if (y == i && x == j) { // std::cout << "@"; // } // else { // std::cout << map[i][j]; // } // } // std::cout << "\n"; // } std::uint64_t x_cp, y_cp; for (auto current_move : {Dir::up, Dir::down, Dir::left, Dir::right}) { // don't move back if (current_move == reverse(last_move)) { continue; } x_cp = x; y_cp = y; switch (current_move) { case Dir::up: y_cp -= 1; break; case Dir::down: y_cp += 1; break; case Dir::left: x_cp -= 1; break; case Dir::right: x_cp += 1; break; } // std::cout << "x=" << x_cp << " y=" << y_cp << "\n"; //don't step back on same path if(map[y_cp][x_cp] == '*') { continue; } // We can go this way if (map[y_cp][x_cp] != 'X') { // current_path.push_back(current_move); // std::cout << "before next, x=" << x_cp << " y=" << y_cp << " stop: " << stop // << " cm: " << to_string(current_move) << "\n"; find_path(x_cp, y_cp, map, current_move, current_path); } } // for (std::uint64_t i = 0; i < n + 2; ++i) { // delete[] new_map[i]; // } // delete[] new_map; } int main(void) { std::cin >> n >> m >> k; char** map = new char*[n + 2]; //[m+2]; for (std::uint64_t i = 0; i < n + 2; ++i) { map[i] = new char[m + 2]; } for (std::uint64_t i = 0; i < m + 2; ++i) { map[0][i] = 'X'; } for (std::uint64_t i = 1; i < n + 1; ++i) { map[i][0] = 'X'; for (std::uint64_t j = 1; j < m + 1; ++j) { std::cin >> map[i][j]; } map[i][m + 1] = 'X'; } for (std::uint64_t i = 0; i < m + 2; ++i) { map[n + 1][i] = 'X'; } std::vector<Speed> speeds; speeds.reserve(k); std::uint64_t a, b; while (k--) { std::cin >> a >> b; speeds.emplace_back(a, b); } std::vector<Dir> cp; find_path(1, 1, map, Dir::none, cp); // for (std::uint64_t i = 0; i < n + 2; ++i) { // for (std::uint64_t j = 0; j < m + 2; ++j) { // std::cout << map[i][j]; // } // std::cout << "\n"; // } // std::uint64_t l=0; // for(const auto& path : all_paths) { // std::cout << "p" << ++l << ": "; // for(const auto& dir : path ) { // std::cout << to_string(dir) << " "; // } // std::cout << "\n"; // } std::uint64_t min_global = std::numeric_limits<std::uint64_t>::max(); std::uint64_t min_global_count = 0; for (const auto& speed : speeds) { std::uint64_t min_time = std::numeric_limits<std::uint64_t>::max(); for (const auto& path : all_paths) { std::uint64_t total_time = 0; for (const auto move : path) { if (move == Dir::right || move == Dir::down) { total_time += speed.a; } if (move == Dir::left || move == Dir::up) { total_time += speed.b; } } if(total_time < min_time) { min_time = total_time; } } if(min_time < min_global) { min_global = min_time; min_global_count = 0; } if(min_time == min_global) { ++min_global_count; } } std::cout << min_global << " " << min_global_count << "\n"; // std::cout << "\n"; for (std::uint64_t i = 0; i < n + 2; ++i) { delete[] map[i]; } delete[] map; 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | #include <cstdint> #include <iostream> #include <string> #include <vector> #include <cstring> #include <limits> #include <set> struct Speed { Speed(std::uint64_t a, std::uint64_t b) : a{a} , b{b} { } std::uint64_t a; std::uint64_t b; }; enum class Dir : std::uint8_t { up, down, left, right, none, }; Dir reverse(Dir dir) { switch (dir) { case Dir::up: return Dir::down; case Dir::down: return Dir::up; case Dir::left: return Dir::right; case Dir::right: return Dir::left; case Dir::none: return Dir::none; } return Dir::none; } std::string to_string(Dir d) { switch (d) { case Dir::up: return "up"; case Dir::down: return "down"; case Dir::left: return "left"; case Dir::right: return "right"; case Dir::none: return "none"; } return "empty"; } static std::vector<std::vector<Dir>> all_paths; static std::uint64_t n, m, k; int stop = 100; void find_path( std::uint64_t x, std::uint64_t y, char** map, Dir last_move, std::vector<Dir> current_path) { // std::cout << "find_path, x=" << x << " y=" << y << " stop: " << stop // << " lm: " << to_string(last_move) << "\n"; // char** new_map = new char*[n + 2]; // for (std::uint64_t i = 0; i < n + 2; ++i) { // new_map[i] = new char[m + 2]; // std::memcpy(new_map[i], map[i], m+2); // } current_path.emplace_back(last_move); // new_map[y][x] = '*'; map[y][x] = '*'; // made it to the top if (x == m && y == n) { all_paths.emplace_back(current_path); return; } // if (--stop < 0) { // return; // } // for (std::uint64_t i = 0; i < n + 2; ++i) { // for (std::uint64_t j = 0; j < m + 2; ++j) { // if (y == i && x == j) { // std::cout << "@"; // } // else { // std::cout << map[i][j]; // } // } // std::cout << "\n"; // } std::uint64_t x_cp, y_cp; for (auto current_move : {Dir::up, Dir::down, Dir::left, Dir::right}) { // don't move back if (current_move == reverse(last_move)) { continue; } x_cp = x; y_cp = y; switch (current_move) { case Dir::up: y_cp -= 1; break; case Dir::down: y_cp += 1; break; case Dir::left: x_cp -= 1; break; case Dir::right: x_cp += 1; break; } // std::cout << "x=" << x_cp << " y=" << y_cp << "\n"; //don't step back on same path if(map[y_cp][x_cp] == '*') { continue; } // We can go this way if (map[y_cp][x_cp] != 'X') { // current_path.push_back(current_move); // std::cout << "before next, x=" << x_cp << " y=" << y_cp << " stop: " << stop // << " cm: " << to_string(current_move) << "\n"; find_path(x_cp, y_cp, map, current_move, current_path); } } // for (std::uint64_t i = 0; i < n + 2; ++i) { // delete[] new_map[i]; // } // delete[] new_map; } int main(void) { std::cin >> n >> m >> k; char** map = new char*[n + 2]; //[m+2]; for (std::uint64_t i = 0; i < n + 2; ++i) { map[i] = new char[m + 2]; } for (std::uint64_t i = 0; i < m + 2; ++i) { map[0][i] = 'X'; } for (std::uint64_t i = 1; i < n + 1; ++i) { map[i][0] = 'X'; for (std::uint64_t j = 1; j < m + 1; ++j) { std::cin >> map[i][j]; } map[i][m + 1] = 'X'; } for (std::uint64_t i = 0; i < m + 2; ++i) { map[n + 1][i] = 'X'; } std::vector<Speed> speeds; speeds.reserve(k); std::uint64_t a, b; while (k--) { std::cin >> a >> b; speeds.emplace_back(a, b); } std::vector<Dir> cp; find_path(1, 1, map, Dir::none, cp); // for (std::uint64_t i = 0; i < n + 2; ++i) { // for (std::uint64_t j = 0; j < m + 2; ++j) { // std::cout << map[i][j]; // } // std::cout << "\n"; // } // std::uint64_t l=0; // for(const auto& path : all_paths) { // std::cout << "p" << ++l << ": "; // for(const auto& dir : path ) { // std::cout << to_string(dir) << " "; // } // std::cout << "\n"; // } std::uint64_t min_global = std::numeric_limits<std::uint64_t>::max(); std::uint64_t min_global_count = 0; for (const auto& speed : speeds) { std::uint64_t min_time = std::numeric_limits<std::uint64_t>::max(); for (const auto& path : all_paths) { std::uint64_t total_time = 0; for (const auto move : path) { if (move == Dir::right || move == Dir::down) { total_time += speed.a; } if (move == Dir::left || move == Dir::up) { total_time += speed.b; } } if(total_time < min_time) { min_time = total_time; } } if(min_time < min_global) { min_global = min_time; min_global_count = 0; } if(min_time == min_global) { ++min_global_count; } } std::cout << min_global << " " << min_global_count << "\n"; // std::cout << "\n"; for (std::uint64_t i = 0; i < n + 2; ++i) { delete[] map[i]; } delete[] map; return 0; } |