아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Spiraling Into Control

메모리 제한1024 MB

요약
홀수 N과 목표 이동 횟수 K가 주어질 때, 나선형으로 번호가 매겨진 격자에서 1번 방에서 중앙 방까지 지름길을 이용해 정확히 K번 이동하는 경로를 출력하거나 불가능함을 판별한다.
난이도

보통10점 중 7점

유형
구현, 행렬, 시뮬레이션, 그리디
정답자
아직 제출이 없습니다

문제

As punishment for being naughty, Dante has been trapped in a strange house with many rooms. The house is an N×N\mathbf{N} \times \mathbf{N} grid of rooms, with N\mathbf{N} odd and greater than 11. The upper left room is numbered 11, and then the other rooms are numbered 22, 33, ..., N2\mathbf{N}^2, in a clockwise spiral pattern. That is, the numbering proceeds along the top row of the grid and then makes a 90 degree turn to the right whenever a grid boundary or an already numbered room is encountered, and finishes in the central room of the grid. Because N\mathbf{N} is odd, there is always a room in the exact center of the house, and it is always numbered N2\mathbf{N}^2.

For example, here are the room numberings for houses with N=3\mathbf{N} = 3 and N=5\mathbf{N} = 5:

Dante starts off in room 11 and is trying to reach the central room (room N2\mathbf{N}^2). Throughout his journey, he can only make moves from his current room to higher-numbered, adjacent rooms. (Two rooms must share an edge — not just a corner — to be adjacent.)

Dante knows that he could walk from room to room in consecutive numerical order — i.e., if he is currently in room xx, he would move to room x+1x+1, and so on. This would take him exactly N2−1\mathbf{N}^2 - 1 moves. But Dante wants to do things his way! Specifically, he wants to reach the central room in exactly K\mathbf{K} moves, for some K\mathbf{K} strictly less than N2−1\mathbf{N}^2 - 1.

Dante can accomplish this by taking one or more shortcuts. A shortcut is a move between rooms that are not consecutively numbered.

For example, in the 5×55 \times 5 house above,

  • If Dante is at 11, he cannot move to 1717, but he can move to 22 or to 1616. The move to 22 is not a shortcut, since 1+1=21 + 1 = 2. The move to 1616 is a shortcut, since 1+1≠161 + 1 \neq 16.
  • From 22, it is possible to move to 33 (not a shortcut) or to 1717 (a shortcut), but not to 11, 1616, or 1818.
  • From 2424, Dante can only move to 2525 (not a shortcut).
  • It is not possible to move out of room 2525.

As a specific example using the 5×55 \times 5 house above, suppose that K\mathbf{K} = 44. One option is for Dante to move from 11 to 22, then move from 22 to 1717 (which is a shortcut), then move from 1717 to 1818, then move from 1818 to 2525 (which is another shortcut). This is illustrated below (the red arrows represent shortcuts):

Can you help Dante find a sequence of exactly K\mathbf{K} moves that gets him to the central room, or tell him that it is impossible?

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case consists of one line with two integers N\mathbf{N} and K\mathbf{K}, where N\mathbf{N} is the dimension of the house (i.e. the number of rows of rooms, which is the same as the number of columns of rooms), and K\mathbf{K} is the exact number of moves that Dante wants to make while traveling from room 11 to room N2\mathbf{N}^2.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1).

If no valid sequence of exactly K\mathbf{K} moves will get Dante to the central room, yy must be IMPOSSIBLE.

Otherwise, yy must be an integer: the number of times that Dante takes a shortcut, as described above. (Notice that because Dante wants to finish in strictly less than N2−1\mathbf{N}^2 - 1 moves, he must always use at least one shortcut.) Then, output yy more lines of two integers each. The ii-th of these lines represents the ii-th time in Dante's journey that he takes a shortcut, i.e., he moves from some room a_ia\_i to another room b_ib\_i such that a_i+1<b_ia\_i + 1 \lt b\_i.

Notice that because these lines follow the order of the journey, a_i<a_i+1a\_i \lt a\_{i+1} for all 1≤i<y1 \le i \lt y.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 1≤K<N2−11 \le \mathbf{K} \lt \mathbf{N}^2 - 1.
  • Nmod  2≡1 \mathbf{N} \mod 2 \equiv 1. (N\mathbf{N} is odd.)

힌트

Sample Case #1 is described in the problem statement. Dante's route is 1→2→17→18→251 \to 2 \to 17 \to 18 \to 25. Because 1→21 \to 2 and 17→1817 \to 18 are moves between consecutively numbered rooms, they are not included in the output. Only the shortcuts (2→172 \to 17 and 18→2518 \to 25) are included.

In Sample Case #2, there is no solution. (Recall that there is no way for Dante to move diagonally.)

In Sample Case #3, observe that 2222 appears both as the end of one shortcut and the start of the next. It would not be valid to include the line 11 22 25 in the output; each line must represent a single shortcut.

There is another solution that uses only one shortcut: Dante can move from 1→2→3→4→5→61 \to 2 \to 3 \to 4 \to 5 \to 6, then move from 6→196 \to 19 (a shortcut), then move from 19→20→21→22→23→24→2519 \to 20 \to 21 \to 22 \to 23 \to 24 \to 25. This is also valid; there is no requirement to minimize (or maximize) the number of shortcuts taken.

In Sample Case #4, Dante cannot get to the central room (99, in this case) in just one move.

예제1

  1. 예제 1

    입력
    4
    5 4
    5 3
    5 12
    3 1
    
    예상 출력
    Case #1: 2
    2 17
    18 25
    Case #2: IMPOSSIBLE
    Case #3: 2
    11 22
    22 25
    Case #4: IMPOSSIBLE