home *** CD-ROM | disk | FTP | other *** search
- Problem 18: Expanding square codes.
-
- One method of encoding messages is known as the "expanding square
- code." This method encodes messages by placing the characters of the
- message to be encoded in an odd order(1,3,5,7,9) square matrix row by
- row, and then retrieves them (decoding) in a clockwise expanding
- square spiral from the center of the matrix. If the message is not
- exactly the right length to fill the matrix, the rest of the matrix
- is filled with asterisk characters (*).
-
- For example, the two square matrices below show the order in which
- the characters are placed in a matrix(order 5), and the order in which
- they are to be retrieved. Notice that the order of retrieval begins
- at the center, then proceeds to the right, and then spirals
- clockwise.
-
- order in order out
-
- 1 2 3 4 5 21 22 23 24 25
- 6 7 8 9 10 20 7 8 9 10
- 11 12 13 14 15 19 6 1 2 11
- 16 17 18 19 20 18 5 4 3 12
- 21 22 23 24 25 17 16 15 14 13
-
- Examples:
-
- encode:
- message in: "This is a test message!"
-
- message out: "stssees a a**!egmtiThis "
-
- decode:
- message in: "anreh is io *.enotshAnd t"
-
- message out: "And this is another one."
-
- Your program should be able to encode and to decode messages by
- this method. The input will consist of pairs of lines; the first
- line in each pair will contain either the word encode or the word
- decode in lower case characters. The second line in each pair will
- consist of a message to be either encoded or decoded by the above
- method, containing a maximum of 80 characters. There will be no
- quotation marks (") surrounding the messages, and you should not
- output your messages with "'s.
-
- From the length of the message, you should determine the minimum
- odd order required for the matrix, and perform the specified operation.
- The output for each operation will consist of one blank line, the
- given message, and the resultant message. Each message should be
- on a separate line. A decoded message may not contain the asterisk
- characters used to fill the matrix in the encoding process. You
- may assume that no actual message contains an asterisk.
-
- You may assume that no input line will be longer than 80 characters,
- so that a matrix larger than 9x9 will never be required.
-
- Your program should continue reading input lines and performing
- operations until something other than the words encode or
- decode is encountered.
-
- By the way, this is a TERRIBLE code; try it on a "quick brown fox"
- and see if you can't guess it.
-
- Here is an actual run; the input file consisted of exactly four lines.
- The output file consists of exactly six lines; lines 1 and 4 are
- blank lines.
- This was the input file (the next four lines):
- encode
- now is the time for all
- decode
- imroft thee **lla snow i
- Here was the output file (the next six lines, NOTICE the blank lines!):
-
- now is the time for all
- imroft thee **lla snow i
-
- imroft thee **lla snow i
- now is the time for all
-