Programming contests

ECN selejtező programozó csapatverseny, 2019. április 17.

April 17, 2019 10:15 AM – April 17, 2019 3:15 PM

Sentence Spiral

Although the English language is a left-to-right, top-to-bottom language, this is not the only way to orient text on a page. Hebrew, for example, is right-to-left, top-to-bottom, and traditional Chinese is top-to-bottom, right-to-left. Well, in the mythical country of Yooperland, text is arranged in a counterclockwise spiral starting from the lower-left corner of a square grid and ending in a central position. For example, the phrase “SECOND WEEK OF DEER CAMP!” can be written in a 5 × 5 grid as follows:

O KEE
FMACW
 P!
DEERD
SECON

For this problem, you will take strings of text written in the standard English left-to-right format and print them out in the square spiral format of Yooperland.

Input Specification

You will be given a series of test cases, followed by a line containing a single 0. The first line of each test case will consist of a single decimal integer N between 4 and 10 inclusive. The next line will contain a string containing no more than N2 characters. There may or may not be one or more additional newline characters between test cases or between a test case and the terminating 0.

Output Specification

The test cases should appear in the output in the order in which they appeared in the input. For each test case, you should print an N × N grid of letters, each line terminated by a newline character. If the input string is smaller than N2 characters in length, the string should be padded on the right with spaces to N2 characters before being placed in the grid. Aside from the trailing spaces and the spaces which were in the input string originally, no extra spaces should appear in the output. Each test case should be followed by an empty line.

Sample Input

  1. 5
  2. SECOND WEEK OF DEER CAMP!
  3. 6
  4. Hockey determines a school's merit
  5. 0
download as text file

Output for Sample Input

  1. O KEE
  2. FMACW
  3.  P!  
  4. DEERD
  5. SECON
  6. enimre
  7. sm s't
  8.  e  le
  9. aritod
  10.  scho 
  11. Hockey
download as text file

Original Problem

NMU Invitational Programming Contest, 2000

University of Debrecen; Faculty of Informatics; v. 03/01/2019