题目链接:P1652 圆 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目描述
给出 n 个圆,保证任意两个圆都不相交且不相切。
然后给出两个点 (x1,y1),(x2,y2),保证均不在某个圆上。现在要从 (x1,y1) -> (x2,y2) 画条曲线,问这条曲线最少穿过多少次圆的边界?
输入格式
- 第一行为一个整数 n,表示圆的个数;
- 第二行是 n 个整数,表示 n 个圆的 x 坐标;
- 第三行是 n 个整数,表示 n 个圆的 y 坐标;
- 第四行是 n 个整数,表示 n 个圆的半径 r;
- 第五行是四个整数 x1,y1,x2,y2。
输出格式
仅一个整数,表示最少要穿过多少次圆的边界。
样例 #1
样例输入 #1
7
1 -3 2 5 -4 12 12
1 -1 2 5 5 1 1
8 1 2 1 1 1 2
-5 1 12 1
样例输出 #1
3
提示
【数据范围】
对于 100% 的数据,1 <= n <= 50,|x|,|y| <= 1000,1 <= r <= 1000。
保证圆之间没有公共点。
AC code:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> x(n) , y(n) , r(n);
for(int i = 0 ; i < n ; i ++)
cin>>x[i];
for(int i = 0 ; i < n ; i ++)
cin>>y[i];
for(int i = 0 ; i < n ; i ++)
cin>>r[i];
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
int res = 0;
for(int i = 0 ; i < n ; i ++)
{
int tx1 = x1 - x[i] , ty1 = y1 - y[i];
int tx2 = x2 - x[i] , ty2 = y2 - y[i];
// 若(x1,y1)和(x2,y2)都在同一个圆的内部则跳过
if(sqrt(tx1 * tx1 + ty1 * ty1) < r[i] && sqrt(tx2 * tx2 + ty2 * ty2) < r[i])
continue;
// 若(x1,y1)和(x2,y2)有且只有一个在同一个圆的内部则加1
if(sqrt(tx1 * tx1 + ty1 * ty1) < r[i] || sqrt(tx2 * tx2 + ty2 * ty2) < r[i])
res ++;
}
cout<<res;
return 0;
}