TOJ1337 Snakes

Buffalo Bill wishes to cross a 1000x1000 square field. A number of snakes are on the field at various positions, and each snake can strike a particular distance in any direction. Can Bill make the trip without being bitten?

Assume that the southwest corner of the field is at (0,0) and the northwest corner at (0,1000). The input consists of a line containing n ≤ 1000, the number of snakes. A line follows for each snake, containing three real numbers: the (x,y) location of the snake and its strike distance. The snake will bite anything that passes closer than this distance from its location.

Bill must enter the field somewhere between the southwest and northwest corner and must leave somewhere between the southeast and northeast corners.

If Bill can complete the trip, give coordinates at which he may enter and leave the field. If Bill may enter and leave at several places, give the most northerly. If there is no such pair of positions, print "Bill will be bitten."

Sample Input

3
500 500 499
0 0 999
1000 1000 200

Output for Sample Input

Bill enters at (0.00, 1000.00) and leaves at (1000.00, 800.00).

Source: Waterloo
Local Contest Jan. 29, 2000

这道题的意思是一个平面上有1000*1000的矩形和很多圆形,看能否从矩形左边出发,不出矩形边界且不接触任何圆形能到达矩形右边。

起初一脸茫然,后来学习了一种新方法----并查集。

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct snake{
    double x,y,r;
}a[1020];
int set[1020];//并查集集合
int find(int i){//并查集查找
    int r = i;
    while(r!=set[r])
        r = set[r];
    return r;
}
void join(int i,int j){//并查集合并
    int b=find(i), c=find(j);
    if(b!=c)
        set[b] = c;
}
int main(){
    int n,i,j,k;
    scanf("%d",&n);
    for(i=0;i<=n;i++)    set[i] = i;
    set[1010] = 1010;
    for(i=1;i<=n;i++){
        scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r);
        for(j=1;j<i;j++){//两蛇攻击范围相交
            if( (a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)<(a[i].r+a[j].r)*(a[i].r+a[j].r) ){
                join(j,i);
            }
        }
        if(a[i].y<a[i].r){//该蛇攻击范围与下边界相交
            join(1010,i);
        }
        if(a[i].y+a[i].r>1000){
            join(0,i);
        }
    }
    if(find(0)==find(1010))//上下边界之间被蛇攻击范围覆盖
        printf("Bill will be bitten.\n");
    else{
        double sy=1000.0,ey=1000.0;
        for(i=1;i<=n;i++){
            if(find(i)==find(0) && a[i].x<a[i].r && a[i].y-sqrt(a[i].r*a[i].r-a[i].x*a[i].x)<sy){
                sy = a[i].y-sqrt(a[i].r*a[i].r-a[i].x*a[i].x);
            }
            if(find(i)==find(0) && 1000-a[i].x<a[i].r && a[i].y-sqrt(a[i].r*a[i].r-(1000-a[i].x)*(1000-a[i].x))<ey){
                ey = a[i].y-sqrt(a[i].r*a[i].r-(1000-a[i].x)*(1000-a[i].x));
            }
        }
        printf("Bill enters at (0.00, %.2f) and leaves at (1000.00, %.2f).\n",sy,ey);
    }
    return 0;
}
时间: 2024-12-28 06:25:11

TOJ1337 Snakes的相关文章

[POJ 2588] Snakes

同swustoj 8 Snakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1015   Accepted: 341 Description Buffalo Bill wishes to cross a 1000x1000 square field. A number of snakes are on the field at various positions, and each snake can strike

[POJ 2588]--Snakes(并查集)

题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS   Memory Limit: 65536K   Description Buffalo Bill wishes to cross a 1000x1000 square field. A number of snakes are on the field at various positions, and each snake can strike a particular

Snakes and Ladders LightOJ - 1151

'Snakes and Ladders' or 'Shap-Ludu' is a game commonly played in Bangladesh. The game is so common that it would be tough to find a person who hasn't played it. But those who haven't played it (unlucky of course!) the rules are as follows. There is a

HackerRank - &quot;Snakes and Ladders: The Quickest Way Up&quot;

A trickier Dijkstra. #include <cmath> #include <cstdio> #include <vector> #include <string> #include <iostream> #include <algorithm> #include <queue> #include <unordered_map> #include <unordered_set> u

LightOJ 1151 Snakes and Ladders 高斯消元

给你一个地图10*10,从1到100,问掷骰子的次数的期望,中间会有传送门从a到b. 高斯消元基础题,学了一发板子,x存放多出来的,a存放系数: 要么是dp[i]=(dp[i+1]+...+dp[i+6]+6)/6; 要么是dp[i]=dp[go[i]]; 值得注意的是哪怕是没有dp[i+6]也要加6,因为说了,如果超界那投得次数也要算上. #include <iostream>#include <functional>#include <algorithm>#incl

CodeChef Consecutive Snakes 三分(整数)

题意 在年度阅兵中,所有的士兵蛇都在阅兵场集合了,但这些蛇的站位不对.整场阅兵必须能从主席台看清楚,所有蛇都应该站成一排.但这些士兵非常懒惰,你必须指挥士兵重新排队,使得所有人的移动距离之和最短. 形式化地,整支阅兵队伍可以视作一条数轴.共有N条蛇,每条蛇是一根长度为L的线段.第i条蛇初始时占据了[Si,Si + L]的区间,蛇与蛇之间可以重合.能从主席台看到的只有区间[A,B],因此所有的蛇都应该处于这个区间内.蛇应该排列成连续一段,之间不能留缝隙,即这些蛇从前往后应该依次占据[X,X+L],

loj Snakes 的 Na&#239;ve Graph 【数论】

题目链接 loj 题解 感谢珂神的指导orz 观察式子\(i \times j \equiv 1 \pmod m\),显然\(i,j\)是模\(m\)意义下成对的逆元,只需统计模\(m\)意义下存在逆元的数的个数,即与\(m\)互质的数的个数\(\varphi(m)\) 每对逆元的连边有两种情况,记逆元对数为\(x\),则方案数为\(2^x\) 真的完了吗?难点才刚开始 模\(m\)意义下有的数逆元为本身!此时不能计入答案 所以我们还需求模\(m\)意义下逆元为本身的数的个数 重新理解一下中国剩

【leetcode】909. Snakes and Ladders

题目如下: 解题思路:天坑题,不在于题目多难,而是要理解题意.题目中有两点要特别注意,一是"You choose a destination square S with number x+1, x+2, x+3, x+4, x+5, or x+6, provided this number is <= N*N." 这里最大可以移动的x+6中的6真的就是数字6啊,不是例子中的N=6的6,可以理解成是掷骰子.二是"Note that you only take a snak

Snakes 的 Na&#239;ve Graph

题解: 首先分析一下这个问题 发现等价于是求n之内与n互素的数的个数,即欧拉函数 这个可以线性筛 但发现还应该减去$x^2==1$的情况 这个东西不是那么好处理 考虑用中国剩余定理拆 因为$p1^{a1}*p2^{a2}*p3^{a3}....$这些是互素的 所以拆完后的方程的解和原方程是唯一对应的 问题变成$x^2 \equiv 1  (mod \ pi^{ai})$ 移项变成$(x-1)(x+1) \equiv1 (mod \ pi^{ai})$ 注意到当$pi>2$时$pi$不可能同时整除