Connection Status:
Competition Arena > SquareCode
SRM 180 · 2004-01-22 · by Eeyore · Encryption/Compression, String Manipulation
Class Name: SquareCode
Return Type: String[]
Method Name: completeIt
Arg Types: (vector<string>)
Problem Statement

Problem Statement

When Bob learned that he was to be sent abroad to work on an oil rig, he resolved that he would write regularly to his girlfriend.

"We'll have to encrypt our correspondence," he told Alice, "so that my tender feelings remain unknown to the rest of the crew."

Alice showed Bob a simple kind of text encryption that makes use of a square cardboard grille divided into equal-sized cells, exactly one quarter of which are punched out to make holes. For example, a six-by-six grille might be configured as follows, where '.' stands for a hole and 'X' for a cell that has been left intact.


               XXXXXX
               .XXXX.
               X.XX..
               XXX.XX
               X.XXXX
               XX.XX.

To encrypt a message, Bob begins writing plaintext into the grille, one character per hole, proceeding from left to right within each row and running through the rows from top to bottom. Once all holes are filled, he rotates the grille ninety degrees clockwise, and continues the message where he left off. After two more quarter turns, he has written a full square of coded text, and must begin a new one. Upon receipt of the message, Alice uses an identical grille to decrypt each square. If, for example, Bob's plaintext were "supercalifragilisticexpialadociously", Alice would receive the following code.


               idocfc
               sriaeu
               gpxoer
               piicau
               laslla
               iylsti

Not just any grille will do for this purpose, as Bob discovered when he tried to make one by cutting out holes at random. He found that this haphazard grille wouldn't let him write out a full square of text in the prescribed manner. After fewer than four quarter-turns, some holes revealed previously written characters. Alice assured him that this was a grave flaw. An encoding grille must permit one to fill the entire square by making three quarter-turns, and it must show each cell of the square exactly once. Bob, alas, had a lyrical soul and not a mathematical one. He could not perceive the properties that make a grille suitable for use with this encryption technique. Can you?

You are given a String[] describing a grille row by row, in the style shown above, with no more than a quarter of its holes punched out. If it is not possible to make a complete and valid encoding grille from this configuration by punching out zero or more additional holes, then return an empty String[]. Otherwise, punch out the necessary holes in the upper-left quadrant to make a complete and valid grille, and return it in the same format as the input.

Constraints

  • grille contains between 2 and 50 elements, inclusive
  • grille contains an even number of elements
  • if grille contains exactly n elements, then every element of grille consists of n characters
  • every character in grille is either '.' or 'X'
  • no more than one quarter of the characters in grille are '.'
Examples
0)
{"XX..",
 "XX.X",
 ".XXX",
 "XXXX"}
Returns: { "XX..",  "XX.X",  ".XXX",  "XXXX" }

No additional holes are punched: {"XX..", "XX.X", ".XXX", "XXXX" } This was already a good grille.

1)
{"XXX.",
 "XX.X",
 ".XXX",
 "XXXX"}
Returns: { "XXX.",  ".X.X",  ".XXX",  "XXXX" }

One additional hole is punched: {"XXX.", ".X.X", ".XXX", "XXXX"} The grille could have been completed by punching a hole in one of four locations. We chose the leftmost, topmost one.

2)
{"XXX.",
 "XX.X",
 ".X.X",
 "XXXX"}
Returns: { }

An empty String[] is returned: {} The given grille reveals the same character location more than once.

3)
{"XXXXXX",
 "XXXXX.",
 "XXXX..",
 "XXX.XX",
 "X.XXXX",
 "XX.XX."}
Returns: { "XXXXXX",  ".XXXX.",  "X.XX..",  "XXX.XX",  "X.XXXX",  "XX.XX." }

Two additional holes are punched: {"XXXXXX", ".XXXX.", "X.XX..", "XXX.XX", "X.XXXX", "XX.XX." }

4)
{"XXX.XX",
 ".XXXX.",
 "X.XXX.",
 "XXX.XX",
 "X.XXXX",
 "XX.XX."}
Returns: { }

Submissions are judged against all 107 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SquareCode with a public method vector<string> completeIt(vector<string> grille) · 107 test cases · 2 s / 256 MB per case

Submitting as anonymous