์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step9] 1002 : ํ„ฐ๋ › (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 19. 22:32

www.acmicpc.net/problem/1002

 

1002๋ฒˆ: ํ„ฐ๋ ›

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

www.acmicpc.net


๋ฌธ์ œ

์กฐ๊ทœํ˜„๊ณผ ๋ฐฑ์Šนํ™˜์€ ํ„ฐ๋ ›์— ๊ทผ๋ฌดํ•˜๋Š” ์ง์›์ด๋‹ค. ํ•˜์ง€๋งŒ ์›Œ๋‚™ ์กด์žฌ๊ฐ์ด ์—†์–ด์„œ ์ธ๊ตฌ์ˆ˜๋Š” ์ฐจ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ์€ ์กฐ๊ทœํ˜„๊ณผ ๋ฐฑ์Šนํ™˜์˜ ์‚ฌ์ง„์ด๋‹ค.

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

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

 

์ž…๋ ฅ

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

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

 

์ถœ๋ ฅ

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

 

์˜ˆ์ œ ์ž…๋ ฅ 1

3

0 0 13 40 0 37

0 0 3 0 7 4

1 1 1 1 1 5

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

2

1

0


ํ’€์ด

     ์กฐ๊ทœํ˜„๊ณผ ๋ฐฑ์Šนํ™˜์˜ ์ขŒํ‘œ์™€ ๋ฅ˜์žฌ๋ช…๊ณผ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ๊ฐ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ์˜ ์ˆ˜

== ๋‘ ์ ์˜ ์ขŒํ‘œ์™€ ๋ฐ˜์ง€๋ฆ„์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‘ ์›์˜ ๊ต์ฐจ์ ์˜ ๊ฐœ์ˆ˜

A(x1, y1) ๋ฐ˜์ง€๋ฆ„ r1, B(x2, y2) ๋ฐ˜์ง€๋ฆ„ r2 ์ผ๋•Œ ๋‘ ์ ์˜ ๊ฑฐ๋ฆฌ distance = ๋ฃจํŠธ(( x1 - x2 )^2 + ( y1 - y2 )^2) ๋ผ ํ•œ๋‹ค.

   1. ๋‘ ์›์ด ์ผ์น˜ํ•  ๋•Œ             : distance == 0 && r1 == r2

   2. ๋‘ ์›์ด ๊ต์ฐจํ•˜์ง€ ์•Š์„ ๋•Œ  : r1 + r2 < distance || r1 - r2 > distance 

   3. ๋‘ ์›์ด 1๋ฒˆ ๊ต์ฐจํ•  ๋•Œ       : r1 - r2 = distance || r1 + r2 = distance

   4. ๋‘ ์›์ด 2๋ฒˆ ๊ต์ฐจํ•  ๋•Œ       : r1 - r2 < distance < r1 + r2

์ด์ง€๋งŒ ์˜ค์ฐจ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด distance^2,  (r1 - r2)^2, (r1 + r2)^2์„ ๋น„๊ตํ•˜๊ฒ ๋‹ค.

=> Math.pow() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

import java.io.*;
import java.util.StringTokenizer;

public class B1002 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int t = Integer.parseInt(br.readLine());
		
		for(int i = 0; i < t; i++) {
			
			StringTokenizer st = new StringTokenizer(br.readLine());
			int x1 = Integer.parseInt(st.nextToken());
			int y1 = Integer.parseInt(st.nextToken());
			int r1 = Integer.parseInt(st.nextToken());
			int x2 = Integer.parseInt(st.nextToken());
			int y2 = Integer.parseInt(st.nextToken());
			int r2 = Integer.parseInt(st.nextToken());
			
			int distance = (int)((Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)));
			int add = (int) Math.pow(r1 + r2, 2);
			int sub = (int) Math.pow(r1 - r2, 2);
			
			if(x1 == x2 && y1 == y2 && r1 == r2) {
				bw.write("-1\n");
			} else if ( distance > add || distance < sub) {
				bw.write("0\n");
			} else if ( distance == add || distance == sub) {
				bw.write("1\n");
			} else if ( sub > distance || distance < add ){
				bw.write("2\n");
			}
		}
		bw.flush();
	}
}