모든 챌린지
보통 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.