#P13106. [GCJ 2019 #1A] Pylons

    ID: 12890 Type: RemoteJudge 20000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>2019Special JudgeGoogle Code Jam

[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 RR rows and CC columns; the rows are numbered from 1 to RR from top to bottom, and the columns are numbered from 1 to CC 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 (i,j)(i, j) denote the cell in the ii-th row and jj-th column; then a jump from a current cell (r,c)(r, c) to a destination cell (r,c)(r', c') is invalid if and only if any of these is true:

  • r=rr = r'
  • c=cc = c'
  • rc=rcr - c = r' - c'
  • r+c=r+cr + c = r' + c'

Can you help us find an order in which to visit each of the R×CR \times C 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, TT. TT test cases follow. Each consists of one line containing two integers RR and CC: the numbers of rows and columns in this galaxy.

输出格式

For each test case, output one line containing Case #x: y, where yy 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 R×CR \times C more lines. The ii-th of these lines represents the ii-th cell you will visit (counting starting from 1), and should contain two integers rir_i and cic_i: 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)

  • T=16.\text{T} = 16.
  • 2R5.2 \leqslant \text{R} \leqslant 5.
  • 2C5.2 \leqslant \text{C} \leqslant 5.

Test set 2 (23 Pts, Hidden)

  • 1T100.1 \leqslant \text{T} \leqslant 100.
  • 2R20.2 \leqslant \text{R} \leqslant 20.
  • 2C20.2 \leqslant \text{C} \leqslant 20.