URI Online Judge | 2179


By Daniel Ambort, FRSF AR Argentina

Timelimit: 1

Batuke is a dog with repetitive behavior that has a particular routine when he goes out for a walk around his neighborhood. The neighborhood is also particular; a N x N matrix that Batuke runs through with a routine in spiral: Batuke starts doing: 1 cell right, one down, then two left, two up, then 3 right, 3 down, then 4 left and 4 ..., and so on...

Lucas (the owner) takes Batuke by car to the starting cell, and the dog always follows his routine to runs through all the cells in his neighborhood.

For example, if the neighborhood has size N=4 , cells are numbered as follows:

and the path done by Batuke, starting from cell 1,1 (upper left cell is 0,0) is:


In this case, his routine makes Batuke to walk 16 cells in total.

But Batuke doesn’t understand about frontiers and he always does his routine (even if he has to walk several cells more outside his neighborhood), to traverse all his neighborhood’s cells. If the start cell is 2,2; the spiral traversal (only neighborhood’s cells are shown) is: 11,12,16,15,14,10,6,7,8,13,9,5,1,2,3,4. In this case Batuke walks 24 cells in total.

Lucas knows you are studying Systems Information Engineering, and he asks you a program to solve this problem: given a neighborhood of N rows x N columns, and a start cell, you have to display the spiral traversal, and the total cells walked.


The input consist in three integer numbers: N (2 < N ≤ 10 ), F and C. N is the number of rows (and columns) in the neighborhood (cells numbered in 1..NxN, by rows, from up to bottom and left to right). F and C indicate row and column where Batuke starts his traversal.


The output are 2 lines: the first is a line containing a list with the cells of the Batuke's neighborhood ordered by the Batuke's traversal, and separated with a blank space. Second line shows the number of total cells walked by Batuke.

Input Samples Output Samples

4 1 1

6 7 11 10 9 5 1 2 3 4 8 12 16 15 14 13

4 3 3

16 15 11 12 14 10 6 7 8 13 9 5 1 2 3 4