

1. 문제 풀이 아이디어

- 각 증가값을
a, b, c, d, e, f
로 정의한다.
y > x
인 경우, 경로는a, a+b
로 구성된다.
- 그 외의 경우, 경로는
a+b+e
,a+b+e+f
의 좌표가 된다.
- 각 값은 1 이상이어야 하므로
x > y
일 경우,y
는 최소 4 이상이어야 한다.
- 경로의 길이가 최소가 되려면 앞의 증가값이 최소가 되어야 한다.
- 따라서,
a, b, c, f
는1
,e
는y-3
,d
는x-y+2
가 된다.
2. 나의 정답 코드
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
StringBuilder stringBuilder = new StringBuilder();
int p = Integer.parseInt(bufferedReader.readLine());
for (int i = 0; i < p; i++) {
StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine());
int n = Integer.parseInt(stringTokenizer.nextToken());
int x = Integer.parseInt(stringTokenizer.nextToken());
int y = Integer.parseInt(stringTokenizer.nextToken());
if (y > x) {
stringBuilder.append(n).append(" 2 ").append(x).append(" ").append(y).append('\n');
} else {
if (y < 4) {
stringBuilder.append(n).append(" NO PATH\n");
} else {
stringBuilder.append(n).append(" 6 1 2 3 ").append(x - y + 5).append(" ").append(x + 2).append(" ").append(x + 3).append('\n');
}
}
}
System.out.print(stringBuilder);
bufferedReader.close();
}
}
3. 정리
- 위의 아이디어를 코드에 적용해 입력값을 계산하고 출력하여 문제를 해결한다.
Share article