Все задачи
Легко Решено: 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. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

Принятых решений пока нет. Решите задачу и займите первое место.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.