728x90
문제
지난주에, 민식주식회사는 IIHF(International Ice Hockey Federation)로부터 긴급한 전화를 받았다.
IIHF는 같은 팀이 링크안에 너무 많으면 알람이 울리는 시스템을 설치해달라고 요청했다. 시스템은 다음과 같이 3개의 부분으로 이루어진다.
- 디지털카메라가 링크의 사진을 매 1초마다 찍는다.
- 디지털카메라가 찍은 사진에서 각 선수의 위치를 뽑아낸다.
- 하키 링크 안에 같은 팀 선수가 총 몇 명인지 계산한다.
하키 링크는 (X, Y)가 가장 왼쪽 아래 모서리인 W * H 크기의 직사각형과, 반지름이 H/2이면서 중심이 (X, Y+R), (X+W, Y+R)에 있는 두 개의 원으로 이루어져 있다. 아래 그림을 참고한다.
선수들의 위치가 주어질 때, 링크 안 또는 경계에 있는 선수가 총 몇 명인지 구하는 프로그램을 작성하시오.
입력
첫째 줄에 수 W H X Y P가 주어진다. P는 선수의 수이다. W와 H는 100보다 작거나 같은 자연수이고, H는 짝수이다. X와 Y는 절댓값이 100보다 작거나 같은 정수이다. P는 최대 50인 자연수이다. 둘째 줄부터 P개의 줄에 각 선수들의 x좌표와 y좌표가 주어진다. 이 좌표는 절댓값이 300보다 작거나 같은 정수이다.
출력
첫째 줄에 링크 안에 있는 선수의 수를 출력한다.
먼저 사각형의 범위 안에 있는지 여부를 확인한다.
그리고 왼쪽 원, 오른쪽 원을 순서대로 확인한다.
원을 확인하기 위해서 원의 중심에서 선수의 좌표의 거리를 구한다. 이 거리가 반지름보다 작거나 같으면 원의 내부 또는 경계에 있는 것이다.
package Silver.s4;
import java.util.Scanner;
public class BOJ_1358_하키 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int W = sc.nextInt();
int H = sc.nextInt();
int X = sc.nextInt();
int Y = sc.nextInt();
int P = sc.nextInt();
int cnt = 0;
for (int i = 0; i < P; i++) {
int px = sc.nextInt();
int py = sc.nextInt();
// 사각형
if (px >= X && px <= X + W && py >= Y && py <= Y + H)
cnt++;
// 원
else {
int circleY = Y + (H / 2); // 원 중심의 y 좌표
// 왼쪽 원
int disLeft = (int) (Math.pow(X - px, 2) + Math.pow(circleY - py, 2));
int disRight = (int) (Math.pow((X + W) - px, 2) + Math.pow(circleY - py, 2));
if (disLeft <= (int) Math.pow(circleY - Y, 2))
cnt++;
// 오른쪽 원
else if (disRight <= (int) Math.pow(circleY - Y, 2))
cnt++;
}
}
System.out.println(cnt);
}
}
728x90
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 3036 - 링 (0) | 2022.08.26 |
---|---|
[백준] 2981 - 검문 (자바/Java) 유클리드 호제법 (0) | 2022.08.25 |
[백준] 1004 - 어린 왕자 (자바/Java) (0) | 2022.08.25 |
[백준] 1764 - 듣보잡 (0) | 2022.08.25 |
[백준] 10815 - 숫자 카드 (자바/Java) (0) | 2022.08.25 |