From 01a8948c587ac3aa8ff1b60abd722ce76ef7ef6b Mon Sep 17 00:00:00 2001 From: Talha-Taki002 Date: Sat, 20 May 2023 14:25:48 +0600 Subject: [PATCH 1/4] Created en.md --- 1092/en.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 1092/en.md diff --git a/1092/en.md b/1092/en.md new file mode 100644 index 00000000..e2a802bf --- /dev/null +++ b/1092/en.md @@ -0,0 +1,21 @@ +# LOJ 1092 - Lighted Panels + +## Summary + + +## Prerequisite + + +## Solution + +## Complexity +- Time Complexity: O(T * $$). +- Memory Complexity: O( $$ ). + +## Code + +### C++ + +```cpp + +``` From 686030cab351480be145705c552da3cb5d9757b9 Mon Sep 17 00:00:00 2001 From: "S.M. Zubair Talha Taki" Date: Sat, 24 Jun 2023 16:25:35 +0600 Subject: [PATCH 2/4] Added en.md --- 1092/en.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/1092/en.md b/1092/en.md index e2a802bf..0c898f85 100644 --- a/1092/en.md +++ b/1092/en.md @@ -1,7 +1,5 @@ # LOJ 1092 - Lighted Panels -## Summary - ## Prerequisite @@ -9,8 +7,8 @@ ## Solution ## Complexity -- Time Complexity: O(T * $$). -- Memory Complexity: O( $$ ). +- Time Complexity: O(T * ). +- Memory Complexity: O( ). ## Code From 144bc524cdec903436b808e9087628667e696e0c Mon Sep 17 00:00:00 2001 From: "S.M. Zubair Talha Taki" Date: Mon, 10 Jul 2023 05:45:58 +0600 Subject: [PATCH 3/4] Added tutorial and code --- 1092/en.md | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 3 deletions(-) diff --git a/1092/en.md b/1092/en.md index 0c898f85..b800f853 100644 --- a/1092/en.md +++ b/1092/en.md @@ -2,18 +2,136 @@ ## Prerequisite - +[Bitmask DP](https://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/tutorial/) ## Solution +`Observation 1`: + +`Observation 2`: If there is a sequence of moves that lead to an answer then it's also possible to do the moves in top-down manner, i.e, toggle the necessary cells from the topmost row to the bottommost row. Follows immediately from `observation 1` Hence, dynamic programming at rescue! + +`Approach`: We iterate through each row, starting from the topmost. For each row, we explore every combination of toggle switching, determining which cells to toggle and which to leave unchanged. By selecting specific cells, we toggle all adjacent and diagonal cells that correspond to the immediate upper, lower, and current row cells. + +Before moving on to the next row, we check if all cells in the previous row are lit. If any cells remain unlit, we discard the current combination as a potential path to a local optimum, and proceed to the next combination. This process continues until we reach the bottommost row, which represents a local optimum. + +Among all the local optimums, we select the one with the minimum value as the answer to our problem. ## Complexity -- Time Complexity: O(T * ). -- Memory Complexity: O( ). +- Time Complexity: **O(T * R * 22C)**. +- Memory Complexity: **O(R * 22C)**. + ## Code ### C++ ```cpp +#include + +using namespace std; + + +const int INF = 100; + +const int MAX_ROW = 9; +const int MAX_COL = 9; + +int mask[MAX_ROW], dp[MAX_ROW][1<= 0) { + for(int k = 0; k < 3; ++k) { + tmp[k] = tmp[k] ^ (1<<(j-1)); + } + } + + if (j + 1 < c) { + for(int k = 0; k < 3; ++k) { + tmp[k] = tmp[k] ^ (1<<(j+1)); + } + } + } + + // All cells of the previous row have to lighted. Otherwise there will be no way around for them to be toggled later + if (row == 0 || (tmp[0] == (1<> t; + + for(int ts = 1; ts <= t; ++ts) { + cin >> r >> c; + + memset(dp, -1, sizeof(dp)); + memset(mask, 0, sizeof(mask)); + + for(int i = 0; i < r; ++i) { + for(int j = 0; j < c; ++j) { + char ch; + cin >> ch; + if (ch == '*') { + mask[i] |= 1< Date: Mon, 10 Jul 2023 06:06:39 +0600 Subject: [PATCH 4/4] Update en.md Added Observation 1. Forgot to add that --- 1092/en.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/1092/en.md b/1092/en.md index b800f853..40bf11bb 100644 --- a/1092/en.md +++ b/1092/en.md @@ -5,7 +5,7 @@ [Bitmask DP](https://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/tutorial/) ## Solution -`Observation 1`: +`Observation 1`: At the end of the day, a cell will be toggled if it has been toggled an odd number of times; otherwise, it will remain in the same state. The specific cells causing the toggling are irrelevant; only the parity of the toggles matters. `Observation 2`: If there is a sequence of moves that lead to an answer then it's also possible to do the moves in top-down manner, i.e, toggle the necessary cells from the topmost row to the bottommost row. Follows immediately from `observation 1` Hence, dynamic programming at rescue! @@ -19,7 +19,6 @@ Among all the local optimums, we select the one with the minimum value as the an - Time Complexity: **O(T * R * 22C)**. - Memory Complexity: **O(R * 22C)**. - ## Code ### C++