Klasy
Time limit1sMemory limit512 MB
Print the requested subrectangle of an n by n grid filled in spiral order from a corner by a walk that turns only right or only left.
- Level
Medium6 of 10
- Topics
- Simulation, Math, Matrix
- Solved
- No attempts yet
Problem
In Byteotia, children play an interesting variant of hopscotch (called klasy in Polish). A square with side is painted on the pavement, divided into identical cells. The task of one of the children, hereafter called the player, is to number all the cells according to a strict rule. Before the game the player is assigned a cardinal direction (north, south, west, or east) and an allowed turn (right or left).
The player steps onto one of the four corner cells of the square, faces the assigned direction, and starts numbering by writing on the starting cell. On each move the player:
- steps one cell forward, but only if after the move they are still inside the square and the target cell is free (not yet numbered);
- otherwise turns exactly once in the allowed direction and then steps one cell forward.
The player writes the next natural number on each newly visited cell. In addition, on the very first move the player may not turn and must go straight in the assigned direction, so the starting cell has to be chosen carefully. For example, if the square has side , the direction is north and the turn is right, an experienced player would choose the south-west corner as the starting cell, and the fully numbered square would look like this:

Your task is to write a program that prints the contents of a given rectangular fragment of the filled-in square.
Input
The first line of input contains, separated by single spaces: an integer () denoting the side length of the square, one word denoting the cardinal direction (one of POLNOC=north, POLUDNIE=south, ZACHOD=west, WSCHOD=east), and one word denoting the allowed turn (one of PRAWO=right, LEWO=left).
The second line contains four integers , , , (, , ), denoting respectively the first and last column and the first and last row of the fragment to print. Columns are numbered from to from west to east, and rows from to from south to north.
Output
Print exactly lines, each containing numbers separated by single spaces. They represent the successive rows of the requested fragment of the numbered square, listed in order from the northernmost row to the southernmost.