King and Queen
Time limit2sMemory limit256 MB
Given an n by m board with a queen on (x, y), count the connected regions of cells the king (no diagonal moves) can reach without stepping on a queen-attacked cell, and print each region's size sorted.
- Level
Medium6 of 10
- Topics
- Geometry, Math, Implementation
- Solved
- No attempts yet
Problem
After a long and exhausting chess battle, only a black king and a white queen remain on a board of cells. Anyone who has played even a little chess knows that in this situation the king's life is in serious danger. The situation is made worse by the fact that the king is wounded and therefore cannot move diagonally. Thus, in one move the king can move to any cell sharing a side with his current cell. The queen, as a reminder, attacks any cell on the same vertical, horizontal, or diagonal line as herself.
The king knows that the queen is too lazy to even chase him. He is sure she will stay in the cell with coordinates . This means the king can choose any cell on the board and then live out his whole life in the region of the board that contains that cell. A region of the board is a set of cells such that the wounded king can travel from any cell of the set to any other without passing through cells that the queen attacks.
Now the king wants to know the number of regions on the board and the size of each of them. Help him do this.
Input
The first line contains a single integer not exceeding , the number of test cases. The descriptions of the test cases follow, one per line.
A test case consists of four integers , , , , the dimensions of the board and the coordinates of the cell where the queen stands.
Output
For each test case, print the answer in the following format. First print an integer , the number of regions on the board. Then print numbers, the sizes of those regions. Print the sizes in nondecreasing order. Separate the numbers with spaces or newlines.