모든 챌린지
쉬움 0명 해결 정답률 0%

Checkerboard Pattern

Draw a checkerboard of # and . cells with the given number of rows and columns.

배우게 될 내용

  • Use r + c to decide between two alternating values
  • Build a rectangular grid row by row
  • Handle a grid with zero rows

문제

번역본 보기

A checkerboard alternates two colors in both directions. Here a dark cell is # and a light cell is .. Number rows and columns from 0; the cell in row r and column c is # when r + c is even and . when it is odd, so the top-left cell is always #.

Implement checkerboard_pattern(rows, cols).

Input

rows: The number of rows.

cols: The number of columns.

Output

Return rows strings from top to bottom, each exactly cols characters long with no spaces. When rows is 0, return an empty list.

제약 조건

0 ≤ rows ≤ 60, 1 ≤ cols ≤ 60

예시

예시 1
입력:
rows = 3
cols = 4

예상 출력:
expected = ["#.#.", ".#.#", "#.#."]
입력:
long long rows = 3;
long long cols = 4;

예상 출력:
std::vector<std::string> expected = {"#.#.", ".#.#", "#.#."};
입력:
long rows = 3;
long cols = 4;

예상 출력:
string[] expected = new string[] {"#.#.", ".#.#", "#.#."};

설명

Row 0 starts with #, row 1 with ., row 2 with # again: ["#.#.", ".#.#", "#.#."].

예시 2
입력:
rows = 2
cols = 1

예상 출력:
expected = ["#", "."]
입력:
long long rows = 2;
long long cols = 1;

예상 출력:
std::vector<std::string> expected = {"#", "."};
입력:
long rows = 2;
long cols = 1;

예상 출력:
string[] expected = new string[] {"#", "."};

설명

One column: cell (0, 0) has an even sum and cell (1, 0) an odd one: ["#", "."].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Neighbouring cells differ by one in either r or c, so r + c changes parity.

힌트 2

Decide each cell from (r + c) mod 2.

힌트 3

Every row has exactly cols characters.

예상 복잡도

O(rows * cols) time, O(rows * cols) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

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

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

다른 사람의 풀이 보기

커뮤니티 솔루션

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