diff --git a/cpp/prevoi19_robots.cpp b/cpp/prevoi19_robots.cpp index 0be62e0..0543cc8 100644 --- a/cpp/prevoi19_robots.cpp +++ b/cpp/prevoi19_robots.cpp @@ -22,45 +22,47 @@ bool isPossible(ll D, const vector &chargers, const Point &start, ll N) { ll min_v = v0 - N; ll max_v = v0 + N; - vector u_candidates = {min_u, max_u, u0}; - vector v_candidates = {min_v, max_v, v0}; + vector > test_points = { + {min_u, min_v}, {min_u, max_v}, {max_u, min_v}, {max_u, max_v}, + {u0, v0}, {min_u, v0}, {max_u, v0}, {u0, min_v}, {u0, max_v} + }; for (const Point &c: chargers) { ll cu = c.x + c.y; ll cv = c.x - c.y; - u_candidates.push_back(cu - D); - u_candidates.push_back(cu + D); - v_candidates.push_back(cv - D); - v_candidates.push_back(cv + D); + + vector > boundary_points = { + {cu - D, cv - D}, {cu - D, cv + D}, + {cu + D, cv - D}, {cu + D, cv + D}, + {cu - D, cv}, {cu + D, cv}, + {cu, cv - D}, {cu, cv + D} + }; + + for (auto [u, v]: boundary_points) { + u = max(min_u, min(max_u, u)); + v = max(min_v, min(max_v, v)); + test_points.push_back({u, v}); + } } - sort(u_candidates.begin(), u_candidates.end()); - u_candidates.erase(unique(u_candidates.begin(), u_candidates.end()), u_candidates.end()); + for (auto [u, v]: test_points) { + if (u < min_u || u > max_u || v < min_v || v > max_v) continue; + if ((u + v) % 2 != 0) continue; - sort(v_candidates.begin(), v_candidates.end()); - v_candidates.erase(unique(v_candidates.begin(), v_candidates.end()), v_candidates.end()); + ll x = (u + v) / 2; + ll y = (u - v) / 2; + Point p = {x, y}; - for (ll u: u_candidates) { - for (ll v: v_candidates) { - if (u < min_u || u > max_u || v < min_v || v > max_v) continue; + if (manhattan(p, start) > N) continue; - ll x = (u + v) / 2; - ll y = (u - v) / 2; - - if ((u + v) % 2 != 0) continue; - - Point p = {x, y}; - if (manhattan(p, start) > N) continue; - - bool valid = true; - for (const Point &c: chargers) { - if (manhattan(p, c) < D) { - valid = false; - break; - } + bool valid = true; + for (const Point &c: chargers) { + if (manhattan(p, c) < D) { + valid = false; + break; } - if (valid) return true; } + if (valid) return true; } return false; }