Find the connection block for the dfs of the WordPress 11853 Paintball, 11853 paintball
Question:
A rectangular lake is provided, with some small circular islands in the lake. I asked if I could take a boat from the left bank to the right bank. If I could, I found the top starting point and the end point.
Question:
If you can reach the right bank from the left bank, there must be no connected island connected from the shore to the lower bank, so you can directly do dfs from top to bottom, determine whether a connected block exists from the shore to the downstream to complete the judgment. The next step is how to find the top location, and draw a picture to find that for the starting point, if there is a connection between the landing and the left bank, then the starting point must be at the bottom of the Left Bank, you only need to update the start point and end point in the dfs process.
Code:
#include <queue>#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1000 + 10;const double w = 1000;struct Ball{ double x, y, r; Ball(double x = 0, double y = 0, double r = 0) : x(x), y(y), r(r) {}}ball[maxn];double lft, rht;int n;bool vis[maxn];bool intersect(int a, int b){ return ball[a].r + ball[b].r >= sqrt((ball[b].x-ball[a].x)*(ball[b].x-ball[a].x)+(ball[b].y-ball[a].y)*(ball[b].y-ball[a].y));}void check_cycle(int u){ if (ball[u].x - ball[u].r < 0) { lft = min(lft, ball[u].y - sqrt(ball[u].r*ball[u].r - ball[u].x*ball[u].x)); } if (ball[u].x + ball[u].r > w) { rht = min(rht, ball[u].y - sqrt(ball[u].r*ball[u].r - (w-ball[u].x)*(w-ball[u].x))); }}bool dfs(int u){ if (vis[u]) return false; vis[u] = true; if (ball[u].y - ball[u].r < 0) return true; for (int i = 0; i < n; i++) { if (intersect(i, u) && dfs(i)) return true; } check_cycle(u); return false;}int main(){// freopen("/Users/apple/Desktop/in.txt", "r", stdin); while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%lf%lf%lf", &ball[i].x, &ball[i].y, &ball[i].r); } lft = rht = w; bool flag = false; memset(vis, false, sizeof(vis)); for (int i = 0; i < n; i++) { if (ball[i].y+ball[i].r>=w && dfs(i)) { flag = true; break; } } if (flag) printf("IMPOSSIBLE\n"); else { printf("0.00 %.2f 1000.00 %.2f\n", lft, rht); } } return 0;}