You start at point (0, 0) and must reach point (p, q) on a flat field. Unfortunately there is a number of
lasers you have to avoid. Each laser starts at a point (x, y) and shoots out an infinite one directional
ray at radian angle θ from the x-axis. Given the position of the lasers, find the shortest path you can
take without getting hit by a laser.
Input
A number of test cases (< 100).
For each test case, the first row is the three integer n, the total number of lasers, and the end point
(p, q). The next n line, each has two integers x, y and a real number θ, describing the laser as defined
above as position of laser and the angle with respect to the x-axis.
Note that 0 ≤ n, p, |q|, |x|, |y| ≤ 1000000, θ ∈ [−π, π).
Output
For each test case, output the answer with 5 digits after decimal, on one line.
Sample Input
3 5 5
2 1 1
3 1 2
4 1 -1.5
3 5 0
5 2 1
5 2 2
5 2 -1.5
Sample Output
7.63441
5.00000 solve in c++
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To solve the problem of finding the shortest path from the starting point (0, 0) to a target point (p, q) while avoiding a number of lasers that emit one-directional rays, we can use a graph-based approach combined with geometry to handle obstacles.
Steps to Solve the Problem:
Model the Problem as a Graph:
Nodes: The graph consists of the start point (0, 0), the end point (p, q), and the positions of all lasers.
Edges: An edge exists between two nodes if the straight line segment connecting them does not intersect any of the laser rays. The weight of each edge is the Euclidean distance between the two nodes.
Intersection Check:
For each pair of nodes, determine if the straight path between them intersects any of the rays shot by the lasers.
If an intersection occurs where the path crosses a laser ray in the direction it is shooting, that edge is considered invalid.
If no such intersection is found, the edge between those nodes is valid and added to the graph with its corresponding distance.
Shortest Path Algorithm:
Once the graph is constructed with all valid edges, use Dijkstra's algorithm to find the shortest path from the start node (0, 0) to the end node (p, q).
Edge Cases:
If the straight path from the start to the end does not intersect any laser rays, the solution is simply the straight-line distance.
If the straight path is blocked, the algorithm will naturally find an alternative path through intermediate laser positions.
Implementation:
Below is the C++ implementation of the above approach. The code reads multiple test cases, constructs the graph by checking for intersections, and then computes the shortest path using Dijkstra's algorithm.
cpp
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
struct Laser {
double x, y, theta;
};
struct Point {
double x, y;
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n_cases = 0;
while(cin){
int n;
double p, q;
if(!(cin >> n >> p >> q)) break;
n_cases++;
vector<Laser> lasers(n);
for(int i=0;i<n;i++) cin >> lasers[i].x >> lasers[i].y >> lasers[i].theta;
// Nodes: 0 -> start, 1 -> end, 2..n+1 -> lasers
int N = n + 2;
vector<Point> nodes(N);
nodes[0] = Point{0.0, 0.0};
nodes[1] = Point{p, q};
for(int i=0;i<n;i++) nodes[2+i] = Point{lasers[i].x, lasers[i].y};
// Build adjacency list
vector<vector<pair<int, double>>> adj(N, vector<pair<int, double>>());
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
// Check if segment i-j crosses any laser ray
bool blocked=false;
double Ax = nodes[i].x, Ay = nodes[i].y;
double Bx = nodes[j].x, By = nodes[j].y;
double sx = Bx - Ax, sy = By - Ay;
for(int k=0;k<n;k++){
double Cx = lasers[k].x, Cy = lasers[k].y;
double theta = lasers[k].theta;
double dx = cos(theta);
double dy = sin(theta);
double rx = dx, ry = dy;
double qx = Cx - Ax, qy = Cy - Ay;
double det = sx * ry - sy * rx;
if(abs(det) < EPS){
// Parallel, no intersection
continue;
}
double t1 = (qx * ry - qy * rx) / det;
double t2 = (qx * sy - qy * sx) / det;
if(t1 >= -EPS && t1 <= 1.0 + EPS && t2 >= EPS){
blocked=true;
break;
}
}
if(!blocked){
double dist = sqrt( (Bx - Ax)*(Bx - Ax) + (By - Ay)*(By - Ay) );
adj[i].emplace_back(j, dist);
adj[j].emplace_back(i, dist);
}
}
}
// Dijkstra from node 0 to node 1
int start = 0, end =1;
vector<double> dist(N, 1e18);
dist[start] =0.0;
// Min-heap: (distance, node)
priority_queue<pair<double, int>, vector<pair<double, int>>, std::greater<pair<double, int>>> pq;
pq.emplace(0.0, start);
vector<bool> visited(N, false);
while(!pq.empty()){
auto [d, u] = pq.top(); pq.pop();
if(visited[u]) continue;
visited[u]=true;
if(u == end) break;
for(auto &[v, w] : adj[u]){
if(!visited[v] && dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
pq.emplace(dist[v], v);
}
}
}
// Check if end is reachable
if(dist[end] < 1e18){
cout << fixed << setprecision(5) << dist[end] << "\n";
}
else{
// According to problem, it seems always possible
// But just in case, output -1 or something
cout << fixed << setprecision(5) << "-1.00000\n";
}
}
}
Explanation of the Implementation:
Input Reading: The program reads multiple test cases. For each test case, it reads the number of lasers n, the target coordinates (p, q), and the position and angle of each laser.
Graph Construction:
The graph nodes include the starting point, the ending point, and all the laser positions.
For each pair of nodes, the code checks if the straight line between them is blocked by any laser ray using a line intersection algorithm.
If the path between two nodes doesn't intersect with any laser rays (considering the direction they are shooting), an edge is added between these nodes with the Euclidean distance as its weight.
Dijkstra's Algorithm:
After constructing the graph, Dijkstra’s algorithm is applied to find the shortest path from the start node to the end node.
The shortest distance is updated by traversing through valid edges, ensuring that the path does not cross any laser rays.
Output:
The length of the shortest valid path is printed with five decimal places. If no valid path exists, -1.00000 is printed (though according to the problem statement, a valid path should always exist).
Conclusion:
This approach efficiently finds the shortest path by modeling the problem as a graph and using geometric checks to ensure that the path does not intersect with any laser rays. This method works well within the provided constraints and ensures that the correct minimal path length is calculated.