[랜덤 마라톤] 자라나는 직교 나선(10437)

lhs's avatar
Jan 05, 2025
[랜덤 마라톤] 자라나는 직교 나선(10437)
notion image
notion image

1. 문제 풀이 아이디어

notion image
  • 각 증가값을 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, f1, ey-3, dx-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

LHS's Study Space