Snailography
InterviewTime limit5sMemory limit1024 MB
Fill an n by n odd-sized grid with the message letters in an outward spiral from the center, then read the grid row by row.
- Level
Easy3 of 10
- Topics
- Simulation, Matrix, String
- Solved
- No attempts yet
Problem
Snails have many enemies, including snakes, turtles, and birds. To keep their routes from being detected, snails need to communicate their travel paths in code.

The encryption technique: The message contains only letters. Let the message length be L. Use the smallest odd integer N such that N×N ≥ L. Then fill an N×N table as follows. Put the first letter of the message in the center cell. Place the remaining letters by moving around the center cell in a spiral. The figure shows the placement order in a 7×7 table. From the center cell, move up, then right, then down, then left, then up again, and so on.
Below are some encryption examples. Cells that remain after the message is placed are filled with '#'.
Message: ABCDEFGH
Encryption:
#BC
HAD
GFE
Message: ABCDEFGHIJKLMNOPQRSTUVW
Encryption:
#JKLM
#IBCN
WHADO
VGFEP
UTSRQ
Message: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
Encryption:
#Zabcde
vYJKLMf
uXIBCNg
tWHADOh
sVGFEPi
rUTSRQj
qponmlk
Using the table size and the original message, encrypt the message so that snails can live longer lives.
Input
The first line contains an odd integer n (1 ≤ n ≤ 19), the table size to use. The second line contains the message to encrypt, a string of 1 to 361 (19×19) letters, both lowercase and uppercase. The message fits in the table.
Output
Print the encrypted message on one line in row-major order: row 1, then row 2, then row 3, and so on. Print a newline character after the last row.
The output does not include '#' characters.