समस्या
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 rowr, columnc(no output).count: output how many lights are on.row r: output how many lights are on in rowr.col c: output how many lights are on in columnc.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
उदाहरण
इनपुट:
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].
इनपुट:
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।