uva 11853 Paintball dfs找連通塊,11853paintball

來源:互聯網
上載者:User

uva 11853 Paintball dfs找連通塊,11853paintball

題意:

給出一個矩形湖, 湖裡面有一些圓形地小島, 問能否從左岸乘船到達右岸,如果能,找出最上面的起點和終點。

題解:

如果能從左岸到達右岸,那麼一定不能存在一個連通的島嶼從上岸連到下岸, 所以直接從上到下做dfs,判斷是否存在從上岸到下岸地連通塊,完成判斷。那麼接下來就是如何找出最上方地點了,畫畫圖便發現,對於起點,如果存在跨越上岸和左岸地連通島嶼,那麼起點一定只能在左岸地交點下方,所以,只需在dfs的過程中更新起點和終點位置即可。

代碼:

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.