#P13106. [GCJ 2019 #1A] Pylons
[GCJ 2019 #1A] Pylons
题目描述
Our Battlestarcraft Algorithmica ship is being chased through space by persistent robots called Pylons! We have just teleported to a new galaxy to try to shake them off of our tail, and we want to stay here for as long as possible so we can buy time to plan our next move... but we do not want to get caught!
This galaxy is a flat grid of rows and columns; the rows are numbered from 1 to from top to bottom, and the columns are numbered from 1 to from left to right. We can choose which cell to start in, and we must continue to jump between cells until we have visited each cell in the galaxy exactly once. That is, we can never revisit a cell, including our starting cell.
We do not want to make it too easy for the Pylons to guess where we will go next. Each time we jump from our current cell, we must choose a destination cell that does not share a row, column, or diagonal with that current cell. Let denote the cell in the -th row and -th column; then a jump from a current cell to a destination cell is invalid if and only if any of these is true:
Can you help us find an order in which to visit each of the cells, such that the move between any pair of consecutive cells in the sequence is valid? Or is it impossible for us to escape from the Pylons?
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each consists of one line containing two integers and : the numbers of rows and columns in this galaxy.
输出格式
For each test case, output one line containing Case #x: y
, where is a string of uppercase letters: either POSSIBLE
or IMPOSSIBLE
, according to whether it is possible to fulfill the conditions in the problem statement. Then, if it is possible, output more lines. The -th of these lines represents the -th cell you will visit (counting starting from 1), and should contain two integers and : the row and column of that cell. Note that the first of these lines represents your chosen starting cell.
2
2 2
2 5
Case #1: IMPOSSIBLE
Case #2: POSSIBLE
2 3
1 1
2 4
1 2
2 5
1 3
2 1
1 5
2 2
1 4
提示
Sample Explanation
In Sample Case #1, no matter which starting cell we choose, we have nowhere to jump, since all of the remaining cells share a row, column, or diagonal with our starting cell.
In Sample Case #2, we have chosen the cell in row 2, column 3 as our starting cell. Notice that it is fine for our final cell to share a row, column, or diagonal with our starting cell. The following diagram shows the order in which the cells are visited:
2 4 6 10 8
7 9 1 3 5
Limits
Test set 1 (8 Pts, Visible)
Test set 2 (23 Pts, Hidden)