백준 1004 어린왕자

jathazp·2022년 2월 4일
0

algorithm

목록 보기
55/57
//const [n...arr] = require('fs').readFileSync('입력.txt').toString().trim().split('\r\n');
let input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');

function isInclude(x, y, a, b, r) {
    if (Math.sqrt((x - a) ** 2 + (y - b) ** 2) > r) return false;
    return true;
}

let idx = 0;
let tc = parseInt(input[idx++]);

for (let i = 0; i < tc; i++) {
    let cnt = 0;
    let [x1, y1, x2, y2] = input[idx++].split(' ').map(x => +x);
    let planets = parseInt(input[idx++]);
    for (let j = 0; j < planets; j++) {
        let [a, b, r] = input[idx++].split(' ').map((x) => parseInt(x));
        if (isInclude(x1, y1, a, b, r) != isInclude(x2, y2, a, b, r)) cnt++;

    }
    console.log(cnt);
}

핵심은 점을 포함하고 있는지 판단하는 함수의 구현
출발점과 도착점을 모두 포함하거나 둘다 포함하지 않는다면
우회해서 피할 수 있는 원들이기 때문에 카운트하지 않는다.

반면 둘중 하나의 점을 포함한다면 반드시 거쳐야 하는 원이므로
카운트 값을 하나 늘려준다
즉, 판별은

if (isInclude(x1, y1, a, b, r) != isInclude(x2, y2, a, b, r)) cnt++;

와 같은 코드로 할 수 있다.

0개의 댓글