Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Light Board

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ rows, cols ≤ 50

0 ≤ number of operations ≤ 1,000

Ví dụ

Ví dụ 1
Đầu vào:
board = ["...", "...", "..."]
operations = ["toggle 1 1", "count", "row 0", "status 0 0"]

Kết quả mong đợi:
expected = [5, 1, 0]
Đầu vào:
std::vector<std::string> board = {"...", "...", "..."};
std::vector<std::string> operations = {"toggle 1 1", "count", "row 0", "status 0 0"};

Kết quả mong đợi:
std::vector<long long> expected = {5, 1, 0};
Đầu vào:
string[] board = new string[] {"...", "...", "..."};
string[] operations = new string[] {"toggle 1 1", "count", "row 0", "status 0 0"};

Kết quả mong đợi:
long[] expected = new long[] {5, 1, 0};

Giải thích

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].

Ví dụ 2
Đầu vào:
board = ["#.", "##"]
operations = ["toggle 0 0", "count", "col 1", "status 1 1"]

Kết quả mong đợi:
expected = [2, 2, 1]
Đầu vào:
std::vector<std::string> board = {"#.", "##"};
std::vector<std::string> operations = {"toggle 0 0", "count", "col 1", "status 1 1"};

Kết quả mong đợi:
std::vector<long long> expected = {2, 2, 1};
Đầu vào:
string[] board = new string[] {"#.", "##"};
string[] operations = new string[] {"toggle 0 0", "count", "col 1", "status 1 1"};

Kết quả mong đợi:
long[] expected = new long[] {2, 2, 1};

Giải thích

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].

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

Skip any offset that leaves the board.

Gợi ý 3

Counting can simply loop over the needed cells each time.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.