๊ธฐ๋ก๋ฐฉ

BOJ_1004 : ์–ด๋ฆฐ ์™•์ž ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1004 : ์–ด๋ฆฐ ์™•์ž

Soom_1n 2022. 12. 31. 18:29

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1004๋ฒˆ: ์–ด๋ฆฐ ์™•์ž

์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์— ๋Œ€ํ•ด ์ฒซ์งธ ์ค„์— ์ถœ๋ฐœ์  (x1, y1)๊ณผ ๋„์ฐฉ์  (x2, y2)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ํ–‰์„ฑ๊ณ„์˜ ๊ฐœ์ˆ˜ n์ด ์ฃผ

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์‹œ์ž‘์ ๊ณผ ๋์ ์„ ์„ ์œผ๋กœ ์ด์„๋•Œ ์ตœ์†Œ๋กœ ํ†ต๊ณผํ•˜๋Š” ํ–‰์„ฑ๊ณ„์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๋ฐ˜๋“œ์‹œ ํ†ต๊ณผํ•ด์•ผํ•˜๋Š” ํ–‰์„ฑ๊ณ„๋Š” ์‹œ์ž‘์ , ๋์  ์ค‘ ํ•˜๋‚˜๋ฅผ ์› ์•ˆ์— ํฌํ•จํ•˜๊ณ  ๋‹ค๋ฅธ ํ•œ ์ ์ด ๋ฐ–์— ์žˆ์–ด์•ผํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        for (int i = 0; i < t; i++) {
            int sx = sc.nextInt();
            int sy = sc.nextInt();
            int ex = sc.nextInt();
            int ey = sc.nextInt();
            int n = sc.nextInt();
            int answer = 0;
            for (int j = 0; j < n; j++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                int r = sc.nextInt();

                double s = Math.sqrt(Math.pow(sx-x, 2) + Math.pow(sy-y, 2));
                double e = Math.sqrt(Math.pow(ex-x, 2) + Math.pow(ey-y, 2));

                if (s > r && e < r)
                    answer++;
                else if (s < r && e > r)
                    answer++;
            }
            System.out.println(answer);
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ํ•œ ํ–‰์„ฑ๊ณ„์˜ ์ขŒํ‘œ์™€ ์‹œ์ž‘์  ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ(s), ๋์  ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ(e)๋ฅผ ๊ตฌํ•œ๋‹ค.
  • ํ–‰์„ฑ๊ณ„์˜ ๋ฐ˜์ง€๋ฆ„์ธ r๋กœ ์‹œ์ž‘์ ๊ณผ ๋์ ์ด ํ–‰์„ฑ๊ณ„ ๋ฒ”์œ„์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    • ๋‘ ์  ์ค‘ ํ•˜๋‚˜๋งŒ ํฌํ•จ๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ขŒํ‘œ๊ฐ€ ์‚ฌ์šฉ๋˜์„œ Pair ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด x,y๋ฅผ ์ €์žฅํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ๊ณ„์‚ฐ์ด ๊ฐ„๋‹จํ•ด์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฒŒ ๋˜์—ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1063 : ํ‚น  (0) 2023.01.04
BOJ_1057 : ํ† ๋„ˆ๋จผํŠธ  (0) 2023.01.01
BOJ_1205 : ๋“ฑ์ˆ˜ ๊ตฌํ•˜๊ธฐ  (0) 2022.12.30
BOJ_11656 : ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด  (0) 2022.12.30
BOJ_13305 : ์ฃผ์œ ์†Œ  (0) 2022.12.29