中級 0件解決済み 正答率0%

Decode the Pattern

Work out the rule behind a family of letter squares from its first members, then build the member for any n.

このチャレンジで学べること

  • Describe a picture with a rule in the row and column numbers
  • Measure a ring number as a maximum of two distances
  • Map a number to a letter

問題

翻訳を表示

A puzzle book shows the first three members of a pattern family and asks for the rest:

n = 1    n = 2    n = 3

A        BBB      CCCCC
         BAB      CBBBC
         BBB      CBABC
                  CBBBC
                  CCCCC

Study them before reading on and try to name the rule. Here it is, stated exactly: member n is a square of 2n − 1 rows, each 2n − 1 letters long with no spaces. Number rows and columns from 0 and let the centre be position n − 1. The cell in row r and column c shows the letter that comes d places after A in the alphabet, where d = max(|r − (n − 1)|, |c − (n − 1)|) is how many rings the cell lies away from the centre. So the centre is A, the ring around it is B, and the outer edge is the n-th letter.

Implement decode_the_pattern(n).

Input

n: Which member of the family to build (at most 26, so the letters never run past Z).

Output

Return the 2n − 1 rows from top to bottom. When n is 0, return an empty list.

制約

0 ≤ n ≤ 26

例

例 1
入力:
n = 2

期待される出力:
expected = ["BBB", "BAB", "BBB"]
入力:
long long n = 2;

期待される出力:
std::vector<std::string> expected = {"BBB", "BAB", "BBB"};
入力:
long n = 2;

期待される出力:
string[] expected = new string[] {"BBB", "BAB", "BBB"};

解説

Only the centre (1, 1) is 0 rings away, every other cell is 1 ring away: ["BBB", "BAB", "BBB"].

例 2
入力:
n = 4

期待される出力:
expected = ["DDDDDDD", "DCCCCCD", "DCBBBCD", "DCBABCD", "DCBBBCD", "DCCCCCD", "DDDDDDD"]
入力:
long long n = 4;

期待される出力:
std::vector<std::string> expected = {"DDDDDDD", "DCCCCCD", "DCBBBCD", "DCBABCD", "DCBBBCD", "DCCCCCD", "DDDDDDD"};
入力:
long n = 4;

期待される出力:
string[] expected = new string[] {"DDDDDDD", "DCCCCCD", "DCBBBCD", "DCBABCD", "DCBBBCD", "DCCCCCD", "DDDDDDD"};

解説

The edge cells are 3 rings out (D); row 3 is the middle row: ["DDDDDDD", "DCCCCCD", "DCBBBCD", "DCBABCD", "DCBBBCD", "DCCCCCD", "DDDDDDD"].

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Mark the centre of each sample square and see how the letters change as you move away from it.

ヒント 2

Cells on the same ring share a letter; the ring number uses the larger of the row and column distances to the centre.

ヒント 3

Check your rule on the n = 3 sample before building any n.

目安の計算量

O(n^2) time, O(n^2) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。