ปานกลาง 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก