문제
풀이
진입/이탈을 세는 매커니즘은 다음과 같습니다.
- 출발점이 해당 행성계 안에 있는지 확인합니다.
- 도착점이 해당 행성계 안에 있는지 확인합니다.
- 출발,도착점 둘다 해당 행성계 안에 있거나 밖에 있는거 아니면 count를 증가시켜줍니다. (=진입/이탈횟수를 저장하는 변수)
#include<iostream>
#include<vector>
#include<tuple>
#include<cmath>
using namespace std;
int main()
{
int t; //테스트케이스
cin>>t;
for(int i=0; i<t; i++)
{
int startX,startY,endX,endY;
cin>>startX>>startY>>endX>>endY;
pair<int,int> start=make_pair(startX,startY); //출발점
pair<int,int> end=make_pair(endX,endY); //도착점
int n; //행성계 갯수
cin>>n;
vector<tuple<int,int,int>> v; //행성계들의 (x,y,반지름) 이 저장됨
for(int i=0; i<n; i++)
{
int x,y,r;
cin>>x>>y>>r;
v.push_back(make_tuple(x,y,r));
}
int count=0;
for(int i=0; i<n; i++)
{
double distance_from_start=sqrt(pow(get<0>(v[i])-start.first,2)+pow(get<1>(v[i])-start.second,2));
double distance_from_end=sqrt(pow(get<0>(v[i])-end.first,2)+pow(get<1>(v[i])-end.second,2));
bool is_start_in_planet=distance_from_start < get<2>(v[i]); //출발점이 해당 행성계에 있는가
bool is_end_in_planet=distance_from_end < get<2>(v[i]); //도착점이 해당 행성계에 있는가?
if(is_start_in_planet!=is_end_in_planet) //출발,도착점중 하나는 행성계 안에 있고 하나는 밖에 있을때만 count++
count++;
}
cout<<count<<"\n";
}
}
'알고리즘 > 백준 문제풀이' 카테고리의 다른 글
[C++] 하키(1358번) (0) | 2022.09.09 |
---|---|
[C++] 백준-터렛(1002번) (0) | 2022.09.06 |
[C++] 백준-택시 기하학(3053번) (0) | 2022.09.05 |
[C++] 백준-참외밭(2477번) (0) | 2022.09.01 |
[C++] 백준-직각삼각형(4153번) (0) | 2022.08.31 |