簡単 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 つずつ開いてみましょう。

ヒント 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 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

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