보통 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

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위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

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