ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

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

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

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1004
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscanln(reader, &t)

	for i := 0; i < t; i++ {
		var count int
		var startX, startY, endX, endY, n int
		fmt.Fscanln(reader, &startX, &startY, &endX, &endY)
		fmt.Fscanln(reader, &n)
		for j := 0; j < n; j++ {
			var planetX, planetY, r int
			fmt.Fscanln(reader, &planetX, &planetY, &r)
			isStartContained := checkPlanetContains(startX, startY, planetX, planetY, r)
			isEndContained := checkPlanetContains(endX, endY, planetX, planetY, r)
			if isEndContained && !isStartContained || !isEndContained && isStartContained {
				count++
			}
		}
		fmt.Fprintln(writer, count)
	}

}

func checkPlanetContains(x, y, planetX, planetY, r int) bool {
	dist := (x-planetX)*(x-planetX) + (y-planetY)*(y-planetY)
	if dist > r*r {
		return false
	}
	return true
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1004
import sys

def check_planet_contains(x, y, planet_x, planet_y, r):
    dist = (x-planet_x)**2 + (y-planet_y)**2
    if dist > r*r:
        return False
    return True

if __name__ == "__main__":
    t = int(sys.stdin.readline())
    for i in range(t):
        count = 0
        start_x, start_y, end_x, end_y = list(map(int, sys.stdin.readline().split()))
        n = int(sys.stdin.readline())
        for j in range(n):
            planet_x, planet_y, r = list(map(int, sys.stdin.readline().split()))
            is_start_contained = check_planet_contains(start_x, start_y, planet_x, planet_y, r)
            is_end_contained = check_planet_contains(end_x, end_y, planet_x, planet_y, r)
            if is_end_contained and not is_start_contained or not is_end_contained and is_start_contained:
                count += 1
        print(count)
728x90
๋Œ“๊ธ€