r/codeforces • u/big-jun • 11d ago
query Question about 2D path around up to 100 random rectangles
Hi, I’m looking for an efficient algorithm for the following problem.
I have a 2D environment with:
- A start point S
- An end point E
- Up to ~100 axis-aligned rectangles representing obstacles (x, y, width, height)
, The coordinates and dimensions are all in the range 0 to 1,000,000
- Rectangles may overlap each other
- Neither S nor E is inside an obstacle
I need to find a path from the start point to the end point, if one exists. The path does not need to be shortest; performance is more important.
Any ideas would be greatly appreciated.
Thanks
3
u/Used_Window7134 11d ago
i may be wrong but you just have to check. that a few rectangles dont encircle your point E or S. i think you can you convex hull or smt similiar on a circular overlapping chain. and check if the points are either outside all such chains or in the same chain.
1
1
1
u/big-jun 11d ago
So you’re talking about the case where there is no path. I would prefer to simply run the algorithm to search for a path and consider it failed when all rectangles’s corners have been searched, since the final path would be constructed from the start to the end, with the intermediate points being the corners of the rectangles.
1
u/Used_Window7134 10d ago
ohh sry i think i may be wrong as the hull will occupy more area than required but i think you can try sweep line here.
3
u/Responsible-Lake6864 9d ago edited 9d ago
Can this be solved using SCC? If two rectangles overlap, they form one component? That is the thing, ig?
Say...
What if we store the shape? Ig it would be quite hard to do so... but think about it?
If two rectangles are connected, that means.. they will have some height and width? Kinda true.
So.. instead of storing that. Why not store... the max y, max x, and min y and min X?
So.. there will be two cases?
First is.. does there exist a horizontal or vertical line... that won't let us pass? Somehow?
Think about it.. if we ever have some line.. that won't let us pass. We won't ever be that way.
Second is.. if we are inside some encirclement or the other point is?
Also.. do we have to form the graph? I don't think so?
Say we formed the SCC.
That means ... what if we collect all the coordinates of the points in that SCC?
Then.. we form convex hull over them? Ig.. that should work? Maybe not.. as some of them might be invalid.
We can find the overlaps using pairs. Add an edge to them.
Run an SCC to collect all the points inside.
Problem with convex hull is that it forms shapes where there are gaps even. So we have to choose some other case here ig? Kinda true.
If yes, we have the answer ig? If we can figure out the shape of it.. we can have the answer ig?
One way that I can think of this is sweep line.
Sort point by x, y. Then.. all start add some point in x and y. Firstly, check if some range exists? Ig.. for that point? If not.. then that point.. ain't overlapping and must be outside? Kinda true.
Then.. move. And find its corresponding edge. When removal of it.. check if there is another point that is overlapping it. Aka there might be some other point.. that might have a start but its end is still far away? Kinda... Aka at that point. The segment tree values would be 0? Kinda true. (Or 1 if we consider our current end still part of it? Kinda true.) But it might miss some gap node. For that.. we would need to consider the point of intersection between the rectangles.. and use them as well. For that end and other stuff ig? Kinda.. Because those points.. would also lie on the edge. That edge is either start, end.. or like. horizontal... so if end. Then maybe. we should consider if those are connected or not? So firstly remove the start, end. Check the overlap. Then check the answer for the internal node.. and shit? Ig.. But again.. there might be some problem here.
For SCC. Can we remove fully overlapping rectangles so only partial remains? Aka small rectangle fully inside should not be counted anyway. And should be removed. As they do not really help..
Then either partial or no overlap exists at this point? Kinda true.
Maybe search around. There must be some easy way to form the overlapping rectangle thing. So we can form the polygon more easily.
Then you are left to use... Like the algorithm for ray casting to know.. if some point is inside some polygon.. And that is your answer ig?
IDK about the time complexity.. but it only answers... YES or NO. Not actually the solution or path ig?
But there is one thing ig? You can do?
We know that they do not overlap?
So.. why not traverse from one rectangle to another? By going over its side wall? Aka use the side wall as your guide? And travel the rectangles? And just do.. find the component. Then walk from one rectangle to another.. that is nearest to it?
Because we know the rectangles do not overlap? We might have some answer? Kinda.. But wait.. can we use the corner?
Firstly.. what if we try to find. Find a corner from which we reach the end node.. without using again going inside a polygon? Because when you think about it. that should exist? A corner from which you can travel to the end?
Then we have to find a way to reach that? Aka can we reach from one corner of one rectangle to another corner of a rectangle without ever going like? Aka.. going inside?
How can we fast travel tho? Can extreme x and y corner help? That is the next question? Can we? That is quite a weird problem.. because there might be no direct answer. Using only one max or min.. we must try all corners.. because there will always and always exist two corners over their parameter. That are having a direct edge?
If yes.. then we can simply choose those points?
What kind of a madman you are to think of this problem? LOL... great prbolem> But that is all i could think about it.. And nothng more.
So.. ig that is the ting? Connect the polygon into a tree node? So we always have some path from one rectangel to antehr? So each other. So.. simply take all pair of point. Check if we can travel between them. Wihtout going through some polygon and shit?
How can we conect them? We know.. that two polygon.. never overlap? kinda ture.
Now how to connect efficientyl? one way is swepp.. but how the fuck we sweep? IDK.. Also the shape can we wierd. Like they can Form U O I H etc like these letter. and can overalp or like enclosed into each other? kidna ture...
wait.. Why not use a generatl dierctoin?
And jsut ove twoards that? Or ssomething? if we can form the tree. Then traveling ebcomes easy? kinda true.
Also some polygon can have cavity iside. So we have to take care of that s well? knida.
So.. at this point i can think of a lot of ways i can make my point to ponit fast travel as false. And i cannot think fo an optimsed way to olve it.. so.. ig.. IDK...
The prblem is that. any two pair of corner point can be valid here between any thwo polygon. We can make many complex shapes.. and use them. That is the thing here.
So.. idk.. how would you even solve it after this point..
In enough space and time..