티스토리 뷰
//알고리즘
1. 단순 구현
2. 시작점의 x,y 좌표와 도착점의 x,y좌표를 비교하여 차이가 더 큰 쪽부터(x or y)시작.
3. 시작점과 도착점이 같을경우 정답은 0 << 예외
더보기
import java.util.Scanner;
public class 방향전환 {
static int T, X, Y, x, y;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
X = sc.nextInt();
Y = sc.nextInt();
x = sc.nextInt();
y = sc.nextInt();
int sX = Math.abs(X - x);
int sY = Math.abs(Y - y);
int ans = 0;
if (X == x && Y == y) {
System.out.printf("#%d %d\n",tc,ans);
continue;
}
if (sX > sY) {
while (true) {
ans++;
if (X - x >= 0) {
X -= 1;
} else {
X += 1;
}
// 비교
if (X == x && Y == y) {
break;
}
ans++;
if (Y - y >= 0) {
Y -= 1;
} else {
Y += 1;
}
if (X == x && Y == y) {
break;
}
}
} else {
while (true) {
ans++;
if (Y - y >= 0) {
Y -= 1;
} else {
Y += 1;
}
if (X == x && Y == y) {
break;
}
ans++;
if (X - x >= 0) {
X -= 1;
} else {
X += 1;
}
// 비교
if (X == x && Y == y) {
break;
}
}
}
System.out.printf("#%d %d\n",tc,ans);
}
}
}
'Algorithm' 카테고리의 다른 글
SW Expert Academy_최솟값으로 이동하기 (0) | 2020.12.03 |
---|---|
SW Expert Academy_규영이와 인영이의 카드게임 (0) | 2020.12.02 |
SW Expert Academy_보급로_다익스트라 (0) | 2020.11.22 |
SW Expert Academy_벽돌깨기 (0) | 2020.11.21 |
SW Expert Academy_미생물격리 (0) | 2020.11.19 |