uva 11853 Paintball_uva

來源:互聯網
上載者:User

題意:有一個1000x1000的矩形,給若干個圓形障礙(原點,半徑,x,y,r),判斷能否從矩形的最左邊到最右邊。如果不能,輸出“IMPOSSIBLE”,如果可以,輸出最左邊的起始左邊和最右邊的終點座標,如果存在多個起始座標或終止座標,則輸出最北邊的座標。

思路:劉汝佳的演算法指導書上的思路,先判斷圓形障礙物是不是把矩形從中間分開了(從上邊界開始DFS,沿著圓形障礙物走,如果能夠到達下邊界,說明圓形障礙物將舉列區域一分為二,無論如何也不能從最左邊走到最右邊)。如果dfs沒有到達下邊界,可以在dfs的過程中判斷出最優解,先初始化ans1,ans2(起始縱座標,終止縱座標)為1000,然後從上邊界沿著圓形障礙物dfs,如果dfs到的圓形與左邊界或者右邊界相交,則更新ans1,ans2的值。最後如果DFS到下邊界,直接輸出"IMPOSSIBLE"即可,否則直接輸出ans1,ans2的結果。

教訓:

WA了好幾次,找不出來哪裡錯了,原來是輸入要輸入多組測試案例,而我看範例之輸入了一個用例,上了個迴圈。

import java.util.Arrays;import java.util.Scanner;public class Main {static int n;static int maxn=1000;static Point[] c = new Point[maxn+10];static boolean[] vis = new boolean[maxn+10];static double ans1,ans2;public static void main(String[] args) {Scanner scan = new Scanner(System.in);while(scan.hasNext()){Arrays.fill(c, null);Arrays.fill(vis, false);n = scan.nextInt();for(int i=0;i<n;i++){c[i] = new Point(scan.nextInt(),scan.nextInt(),scan.nextInt());vis[i] = false;}boolean isOk = true;ans1 = ans2 = maxn;for(int i=0;i<n;i++){if(!vis[i]&&c[i].y+c[i].r>=maxn){if(!dfs(i)){isOk = false;break;}}}if(!isOk){System.out.println("IMPOSSIBLE");}else{System.out.printf("0.00 %.2f 1000.00 %.2f\n",ans1,ans2);}}}public static boolean dfs(int i){vis[i] = true;if(c[i].y-c[i].r<=0)return false;if(c[i].x-c[i].r<=0)ans1=Math.min(ans1, c[i].y-Math.sqrt(c[i].r*c[i].r-c[i].x*c[i].x));if(c[i].x+c[i].r>=maxn)ans2=Math.min(ans2, c[i].y-Math.sqrt(c[i].r*c[i].r-(maxn-c[i].x)*(maxn-c[i].x)));for(int j=0;j<n;j++){if(!vis[j]){if(can(i,j)&&!dfs(j)){return false;}}}return true;}public static boolean can(int u,int v){int dx = c[u].x-c[v].x;int dy = c[u].y-c[v].y;return dx*dx+dy*dy-(c[u].r+c[v].r)*(c[u].r+c[v].r)<=0;}static class Point{int x;int y;int r;public Point(int x,int y,int r){this.x = x;this.y = y;this.r = r;}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.