원본 : https://school.programmers.co.kr/learn/courses/30/lessons/169198
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
import java.util.ArrayList;
import java.util.List;
class Solution {
public int[] solution(int m, int n, int startX, int startY, int[][] balls) {
int[] answer = new int[balls.length];
int i = 0;
for (int[] ball : balls) {
int[] upReflectBall = new int[]{ball[0], n - ball[1] + n};
int[] downReflectBall = new int[]{ball[0], -ball[1]};
int[] leftReflectBall = new int[]{-ball[0], ball[1]};
int[] rightReflectBall = new int[]{m - ball[0] + m, ball[1]};
List<Integer> distanceList = new ArrayList<>();
int upReflectDistance = (int) (Math.pow(startX - upReflectBall[0], 2) + Math.pow(startY - upReflectBall[1], 2));
int downReflectDistance = (int) (Math.pow(startX - downReflectBall[0], 2) + Math.pow(startY - downReflectBall[1], 2));
int rightReflectDistance = (int) (Math.pow(startX - rightReflectBall[0], 2) + Math.pow(startY - rightReflectBall[1], 2));
int leftReflectDistance = (int) (Math.pow(startX - leftReflectBall[0], 2) + Math.pow(startY - leftReflectBall[1], 2));
if (startX != ball[0]) {
distanceList.add(upReflectDistance);
distanceList.add(downReflectDistance);
if (startY == ball[1] && startX > ball[0]) {
distanceList.add(rightReflectDistance);
}
if (startY == ball[1] && startX < ball[0]) {
distanceList.add(leftReflectDistance);
}
}
if (startY != ball[1]) {
distanceList.add(leftReflectDistance);
distanceList.add(rightReflectDistance);
if (startX == ball[0] && startY > ball[1]) {
distanceList.add(upReflectDistance);
}
if (startX == ball[0] && startY < ball[1]) {
distanceList.add(downReflectDistance);
}
}
if (startY - ball[1] / startX - ball[0] == m / n) {
int[] upRigthReflectBall = new int[]{m - ball[0] + m, n - ball[1] + n};
int[] downLeftReflectBall = new int[]{-ball[0], -ball[1]};
distanceList.add((int) (Math.pow(upRigthReflectBall[0] - startX, 2) + Math.pow(upRigthReflectBall[1] - startY, 2)));
distanceList.add((int) (Math.pow(downLeftReflectBall[0] - startX, 2) + Math.pow(downLeftReflectBall[1] - startY, 2)));
} else if (startY - ball[1] / startX - ball[0] == -m / n) {
int[] upLeftReflectBall = new int[]{-ball[0], n - ball[1] + n};
int[] downRightReflectBall = new int[]{m - ball[0] + m, -ball[1]};
distanceList.add((int) (Math.pow(upLeftReflectBall[0] - startX, 2) + Math.pow(upLeftReflectBall[1] - startY, 2)));
distanceList.add((int) (Math.pow(downRightReflectBall[0] - startX, 2) + Math.pow(downRightReflectBall[1] - startY, 2)));
}
distanceList.sort(null);
answer[i++] = distanceList.get(0);
}
return answer;
}
}
광물 캐기 (연습문제) (0) | 2023.05.23 |
---|---|
무인도 여행 (연습문제) (0) | 2023.05.23 |
숫자 변환하기 (연습문제) (0) | 2023.05.23 |
과제 진행하기 (연습문제) (0) | 2023.05.08 |
연속된 부분 수열의 합 (연습문제) (0) | 2023.05.08 |
댓글 영역