-
Notifications
You must be signed in to change notification settings - Fork 0
/
Programmers_당구연습.java
57 lines (47 loc) · 1.83 KB
/
Programmers_당구연습.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package programmers.lv2;
import java.util.Arrays;
public class Programmers_당구연습 {
public static void main(String[] args) {
Programmers_당구연습 test = new Programmers_당구연습();
int m = 10;
int n = 10;
int startX = 3;
int startY = 7;
int[][] balls = {{7, 7}, {2, 7}, {7, 3}};
int[] result = test.solution(m, n, startX, startY, balls);
System.out.println(Arrays.toString(result));
}
public int[] solution(int m, int n, int startX, int startY, int[][] balls) {
int[] answer = new int[balls.length];
for(int i=0; i<balls.length; i++) {
int targetX = balls[i][0];
int targetY = balls[i][1];
int minDistance = Integer.MAX_VALUE;
// 상
if(!(startX == targetX && startY <= targetY)) {
int distance = getDistance(startX, startY, targetX, n+(n-targetY));
minDistance = Math.min(minDistance, distance);
}
// 하
if(!(startX == targetX && startY >= targetY)) {
int distance = getDistance(startX, startY, targetX, -1*targetY);
minDistance = Math.min(minDistance, distance);
}
// 좌
if(!(startY == targetY && startX >= targetX)) {
int distance = getDistance(startX, startY, -1*targetX, targetY);
minDistance = Math.min(minDistance, distance);
}
// 우
if(!(startY == targetY && startX <= targetX)) {
int distance = getDistance(startX, startY, m+(m-targetX), targetY);
minDistance = Math.min(minDistance, distance);
}
answer[i] = minDistance;
}
return answer;
}
public int getDistance(int startX, int startY, int targetX, int targetY) {
return (int) (Math.pow(targetX-startX, 2) + Math.pow(targetY-startY, 2));
}
}