Find the connection block for the dfs of the WordPress 11853 Paintball, 11853 paintball

Source: Internet
Author: User

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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.