中級 0件解決済み 正答率0%

Light Board

Simulate a board of lights where pressing one flips it and its neighbors, and answer count queries.

このチャレンジで学べること

  • Convert text rows into a grid of booleans
  • Visit neighbors with bounds checks
  • Count values in a row, a column or the whole grid

問題

翻訳を表示

A stage designer controls a rectangular board of lights. # is a light that is on and . is off. Pressing a light flips it and its four neighbors (above, below, left and right) that exist on the board. Process these operations in order:

  • toggle r c: press the light in row r, column c (no output).
  • count: output how many lights are on.
  • row r: output how many lights are on in row r.
  • col c: output how many lights are on in column c.
  • status r c: output 1 if that light is on, otherwise 0.

Rows and columns are numbered from 0, and every index is on the board.

Implement light_board(board, operations).

Input

board: The starting board, one string per row; all rows have the same length.

operations: The operations, processed in order.

Output

Return the outputs of count, row, col and status in the order they happen.

制約

1 ≤ rows, cols ≤ 50

0 ≤ number of operations ≤ 1,000

例

例 1
入力:
board = ["...", "...", "..."]
operations = ["toggle 1 1", "count", "row 0", "status 0 0"]

期待される出力:
expected = [5, 1, 0]
入力:
std::vector<std::string> board = {"...", "...", "..."};
std::vector<std::string> operations = {"toggle 1 1", "count", "row 0", "status 0 0"};

期待される出力:
std::vector<long long> expected = {5, 1, 0};
入力:
string[] board = new string[] {"...", "...", "..."};
string[] operations = new string[] {"toggle 1 1", "count", "row 0", "status 0 0"};

期待される出力:
long[] expected = new long[] {5, 1, 0};

解説

Pressing the center turns on a plus shape of 5 lights. Row 0 has only its middle light on, and the corner (0, 0) is still off: [5, 1, 0].

例 2
入力:
board = ["#.", "##"]
operations = ["toggle 0 0", "count", "col 1", "status 1 1"]

期待される出力:
expected = [2, 2, 1]
入力:
std::vector<std::string> board = {"#.", "##"};
std::vector<std::string> operations = {"toggle 0 0", "count", "col 1", "status 1 1"};

期待される出力:
std::vector<long long> expected = {2, 2, 1};
入力:
string[] board = new string[] {"#.", "##"};
string[] operations = new string[] {"toggle 0 0", "count", "col 1", "status 1 1"};

期待される出力:
long[] expected = new long[] {2, 2, 1};

解説

The corner press flips (0, 0), (1, 0) and (0, 1): the board becomes .# / .#, so 2 lights are on, both in column 1: [2, 2, 1].

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

List the five cells a press affects as offsets from the pressed cell.

ヒント 2

Skip any offset that leaves the board.

ヒント 3

Counting can simply loop over the needed cells each time.

目安の計算量

O(q × rows × cols) time, O(rows × cols) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

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