본문 바로가기

알고리즘(백준 등) 공부

SWEA 11285. 다트 게임

보드에는 중심이 원점이고 반지름이 20,40,60,80,100,120,140,160,180,200 (단위는 mm)인 10개의 원이 그려져 있고 각각의 화살은 꽂힌 지점을 감싸는 가장 가까운 원(경계선에 꽂힌 경우도 포함)의 반지름이 20 * (11 - p)인 경우 p점을 획득한다. (1 ≤ p ≤ 10) 주어진 화살들의 위치를 통해 얻은 점수의 합을 구하는 문제이다.

 

화살의 위치와 원점과의 거리를 구한 후 화살을 포함하는 가장 가까운 원을 큰 원부터 순회를 통해 찾은 후 점수 공식을 적용하여 점수의 합을 구하면 된다.

for(int i=0; i<n; i++) {
    st = new StringTokenizer(br.readLine());
    int x = Integer.parseInt(st.nextToken()), y = Integer.parseInt(st.nextToken());
    double distance = Math.sqrt(x*x + y*y);
    if(distance > 200) continue;
    int current = 200;
    for(int radius=200; radius>0; radius-=20) {
        if(distance > radius) break;
        current = radius;
    }
    sum += -(current/20) + 11;
}

 

 

결과 코드는 다음과 같다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        
        StringBuilder sb = new StringBuilder();
        for (int tc = 1; tc <= T; tc++) {
            sb.append('#').append(tc).append(' ');
            int n = Integer.parseInt(br.readLine());
            long sum = 0;
            StringTokenizer st;
            for(int i=0; i<n; i++) {
                st = new StringTokenizer(br.readLine());
                int x = Integer.parseInt(st.nextToken()), y = Integer.parseInt(st.nextToken());
                double distance = Math.sqrt(x*x + y*y);
                if(distance > 200) continue;
                int current = 200;
                for(int radius=200; radius>0; radius-=20) {
                    if(distance > radius) break;
                    current = radius;
                }
                sum += -(current/20) + 11;
            }
            sb.append(sum).append('\n');
        }
        
        br.close();
        System.out.print(sb);
    }
}

'알고리즘(백준 등) 공부' 카테고리의 다른 글

SWEA 10993. 군주제와 공화제  (0) 2026.06.04
SWEA 11112. 셀로판지  (0) 2026.06.03
SWEA 11315. 오목 판정  (0) 2026.06.02
SWEA 11316. 주기 찾기  (0) 2026.06.01
SWEA 11387. 몬스터 사냥  (0) 2026.06.01