도와주세요... (2016 자바 3-H 두개의 사각형, Wrong Answer)

ChaDaEun Reply 7 years 31 weeks ago
두 개의 사각형 문제에서 계속 WA가 뜨네요... 제가 어느 경우를 놓친 걸까요? 아니면 코드를 잘못 쓴 걸까요ㅠㅠ 알고리즘은 Accepted 받으신 분께서 설명받은 방법을 썼습니다.(chunghee님 감사합니다!) 겹치는 부분의 넓이를 이용한 풀이였는데요, 저는 이 겹치는 부분의 넓이가 두번째 사각형과 동일할 경우 inside, 0일 경우 outside, 이 외의 경우 attach로 구분했습니다. 혹시 제가 알고리즘을 잘못 이해한 걸까요ㅠㅠ 외접, 내접은 각각 outside, inside로 판단하였고, 큰 사각형과 작은 사각형이 동일한 경우에는 attach로 판단하였습니다. 너비와 높이는 양의 실수만 고려했습니다. 아래는 코드입니다! --- import java.util.*; public class Main { public static void main(String[] args) { //input Scanner sc = new Scanner(System.in); double x1 = sc.nextDouble(); double y1 = sc.nextDouble(); double w1 = sc.nextDouble(); double h1 = sc.nextDouble(); double x2 = sc.nextDouble(); double y2 = sc.nextDouble(); double w2 = sc.nextDouble(); double h2 = sc.nextDouble(); //큰 사각형과 작은 사각형의 왼쪽, 오른쪽, 위, 아래 끝의 값을 설정한다. double bigLeft = x1 - w1 / 2; double bigRight = x1 + w1 / 2; double bigTop = y1 + h1 / 2; double bigBottom = y1 - h1 / 2; double smallLeft = x2 - w2 / 2; double smallRight = x2 + w2 / 2; double smallTop = y2 + h2 / 2; double smallBottom = y2 - h2 / 2; double overlapLeft; double overlapRight; double overlapTop; double overlapBottom; //determine //겹치는 부분의 상하좌우 끝을 구한다. //left if (smallLeft < bigLeft) { overlapLeft = bigLeft; } else if (smallLeft > bigRight) { overlapLeft = bigRight; } else { overlapLeft = smallLeft; } //right if (smallRight < bigLeft) { overlapRight = bigLeft; } else if (smallRight > bigRight) { overlapRight = bigRight; } else { overlapRight = smallRight; } //top if (smallTop < bigBottom) { overlapTop = bigBottom; } else if (smallTop > bigTop) { overlapTop = bigTop; } else { overlapTop = smallTop; } //bottom if (smallBottom < bigBottom) { overlapBottom = bigBottom; } else if (smallBottom > bigTop) { overlapBottom = bigTop; } else { overlapBottom = smallBottom; } //겹치는 부분의 넓이를 구한다. double overlapArea = (overlapTop - overlapBottom) * (overlapRight - overlapLeft); //넓이를 이용해 포함관계를 판단한다. String str = new String(); //작은 사각형 넓이와 같으면 inside. 큰 사각형 넓이와 동일한 경우는 제외 if (overlapArea == (smallTop - smallBottom) * (smallRight - smallLeft) && overlapArea != (bigTop - bigBottom) * (bigRight - bigLeft)) { str = "inside"; } else if (overlapArea == 0) { str = "outside"; } else { str = "attach"; } //output System.out.println(str); } }
chunghee Reply 7 years 31 weeks ago
잘 하신것 같습니다. 다만, 외접 상황에서 attach 라고 판정하는 부분이 없습니다. 이 경우 overlapped area가 0 이지만 attach가 됩니다.
ChaDaEun Reply 7 years 31 weeks ago
음 그러면 선이나 점이 겹칠 때도 attach인가요? (내접하는 경우) 0 0 4 4 1 0 2 2 (외접하는 경우 - 모서리가 만나는 경우) 0 0 1 1 1 0 1 1 (외접하는 경우 - 꼭짓점이 만나는 경우) 0 0 1 1 1 1 1 1 이 세가지 모두 attach를 출력해야 하나요?
chunghee Reply 7 years 31 weeks ago
네, 주어진 현재의 조건에 따르면 2, 3번이 attach입니다. (이전 discuss 들은 무시하세요, 그때와는 약간 조건이 바뀌었습니다.) 각각의 경우를 보면.. 1. 내접상황 = inside 2. 외접상황 = attach 3. 외접상황 = attach 2, 3 case 의 경우 overlapped rectangle 과 rect2 의 관계를 통해 동일하게 검출할 수 있습니다.
ChaDaEun Reply 7 years 31 weeks ago
드디어 Accepted 나왔습니다!!! chunghee님 정말정말 감사합니다ㅠㅠ
chunghee Reply 7 years 31 weeks ago
축하드립니다 ^^