# [Algorithm/JS] ๋ฐฑ์ค€ 1002๋ฒˆ ํ„ฐ๋ ›

๐Ÿ”— ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ (opens new window)

# Question

์กฐ๊ทœํ˜„๊ณผ ๋ฐฑ์Šนํ™˜์€ ํ„ฐ๋ ›์— ๊ทผ๋ฌดํ•˜๋Š” ์ง์›์ด๋‹ค. ํ•˜์ง€๋งŒย ์›Œ๋‚™ ์กด์žฌ๊ฐ์ด ์—†์–ด์„œ ์ธ๊ตฌ์ˆ˜๋Š” ์ฐจ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ์€ ์กฐ๊ทœํ˜„๊ณผ ๋ฐฑ์Šนํ™˜์˜ ์‚ฌ์ง„์ด๋‹ค. ์ด์„์›์€ ์กฐ๊ทœํ˜„๊ณผ ๋ฐฑ์Šนํ™˜์—๊ฒŒ ์ƒ๋Œ€ํŽธ ๋งˆ๋ฆฐ(๋ฅ˜์žฌ๋ช…)์˜ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ผ๋Š” ๋ช…๋ น์„ ๋‚ด๋ ธ๋‹ค. ์กฐ๊ทœํ˜„๊ณผ ๋ฐฑ์Šนํ™˜์€ ๊ฐ๊ฐ ์ž์‹ ์˜ ํ„ฐ๋ › ์œ„์น˜์—์„œ ํ˜„์žฌ ์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ–ˆ๋‹ค.

์กฐ๊ทœํ˜„์˜ ์ขŒํ‘œ (x1, y1)์™€ ๋ฐฑ์Šนํ™˜์˜ ์ขŒํ‘œ (x2, y2)๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ์กฐ๊ทœํ˜„์ด ๊ณ„์‚ฐํ•œ ๋ฅ˜์žฌ๋ช…๊ณผ์˜ ๊ฑฐ๋ฆฌ r1๊ณผ ๋ฐฑ์Šนํ™˜์ด ๊ณ„์‚ฐํ•œ ๋ฅ˜์žฌ๋ช…๊ณผ์˜ ๊ฑฐ๋ฆฌ r2๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

# Input

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

ํ•œ ์ค„์— x1, y1, r1, x2, y2, r2๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. x1, y1, x2, y2๋Š” -10,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๊ณ , r1, r2๋Š” 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

# Output

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ฌดํ•œ๋Œ€์ผ ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

# Input

3
0 0 13 40 0 37
0 0 3 0 7 4
1 1 1 1 1 5
1
2
3
4

# Output

2
1
0
1
2
3

# Solution

์ด ๋ฌธ์ œ๋Š” ๊ฐ ์ขŒํ‘œ(x1, y1), (x2, y2)๊ฐ€ ์›์˜ ์ค‘์‹ฌ์ด๊ณ , ๋ฐ˜์ง€๋ฆ„์ด r1, r2 ์ผ ๋•Œ ๋‘ ์›์˜ ์ ‘์ ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

๋จผ์ € ๋‘ ์› ์ค‘์‹ฌ์˜ ๊ฑฐ๋ฆฌ์™€ ๋‘ ๋ฐ˜์ง€๋ฆ„์˜ ํ•ฉ, ๋‘ ๋ฐ˜์ง€๋ฆ„์˜ ์ฐจ์ด๋ฅผ ์•Œ์•„์•ผํ•œ๋‹ค.

  1. dist = ๋‘ ์› ์ค‘์‹ฌ์˜ ๊ฑฐ๋ฆฌ
  2. rsum = ๋‘ ๋ฐ˜์ง€๋ฆ„์˜ ํ•ฉ
  3. rsub = ๋‘ ๋ฐ˜์ง€๋ฆ„์˜ ์ฐจ

# ์ ‘์ ์ด ๋ฌดํ•œ๋Œ€์ผ ๊ฒฝ์šฐ

๋‘ ์›์˜ ์ ‘์ ์ด ๋ฌดํ•œ๋Œ€์ผ ๊ฒฝ์šฐ๋Š” ๋‘ ์›์ด ์™„์ „ํžˆ ๊ฒน์ณ์กŒ์„ ๊ฒฝ์šฐ์ด๋‹ค. ์ฆ‰, ํฌ๊ธฐ๊ฐ€ ๋™์ผ(rsub = 0)ํ•˜๊ณ , ์ขŒํ‘œ๊ฐ€ ๊ฐ™๋‹ค(dist = 0).

dist === 0 || rsub === 0

# ์ ‘์ ์ด ์—†์„ ๊ฒฝ์šฐ

  1. ๋‘ ์›์ด ์กฐ๊ธˆ๋„ ํฌํ•จ๊ด€๊ณ„๋ฅผ ์ด๋ฃจ์ง€ ์•Š์„ ๊ฒฝ์šฐ (dist > rsum)
  2. ํ•œ ์› ์•ˆ์— ๋‹ค๋ฅธ ์›์ด ์ ‘์ ์—†์ด ์™„์ „ํžˆ ํฌํ•จ๋  ๊ฒฝ์šฐ (dist < rsub)

dist > rsum || dist < rsub

# ์ ‘์ ์ด ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ

  1. ํ•œ ์› ์•ˆ์— ๋‹ค๋ฅธ ์›์ด ์™„์ „ํžˆ ํฌํ•จ๋˜์–ด ์ ‘์ ์ด ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ (dist = rsum)
  2. ๋‘ ์›์ด ํฌํ•จ๊ด€๊ณ„๋ฅผ ์ด๋ฃจ์ง€ ์•Š์œผ๋‚˜ ํ•˜๋‚˜์˜ ์ ‘์ ์ด ์žˆ์„ ๊ฒฝ์šฐ (dist = rsub)

dist === rsum || dist === rsub

# ์ ‘์ ์ด ๋‘๊ฐœ์ผ ๊ฒฝ์šฐ

๋‘ ์› ์ค‘์‹ฌ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋‘ ๋ฐ˜์ง€๋ฆ„ ํ•ฉ๋ณด๋‹ค ์ž‘๊ณ , ๋‘ ๋ฐ˜์ง€๋ฆ„ ์ฐจ์ด๋ณด๋‹ค ํด ๊ฒฝ์šฐ

dist < rsum && dist > sub

const fs = require('fs');
const [count, ...input] = fs.readFileSync('../input.txt').toString().trim().split('\n');

input.map((t) => {
  const [x1, y1, r1, x2, y2, r2] = t.split(' ').map(Number);
  const dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); // ๋‘ ์ขŒํ‘œ์˜ ๊ฑฐ๋ฆฌ
  const rsum = r1 + r2; // ๋‘ ์›์˜ ๋ฐ˜์ง€๋ฆ„ ํ•ฉ
  const rsub = Math.abs(r1 - r2); // ๋‘ ๋ฐ˜์ง€๋ฆ„ ์ฐจ์˜ ์ ˆ๋Œ“๊ฐ’

  if ((dist === 0) & (rsub === 0)) return console.log(-1);
  if (dist > rsum || dist < rsub) return console.log(0);
  if (dist === rsum || dist === rsub) return console.log(1);
  console.log(2);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

ํ’€์ด ์ฐธ๊ณ  (opens new window)

Last Updated: 2022. 6. 5. ์˜คํ›„ 3:42:39