<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 設置二進制矩陣的所有元素所需的最少操作 > 原文: [https://www.geeksforgeeks.org/minimum-operations-required-set-elements-binary-matrix/](https://www.geeksforgeeks.org/minimum-operations-required-set-elements-binary-matrix/) 給定 **N** 行和 **M** 列的二進制矩陣。 矩陣上允許的操作是選擇任何索引(x,y)并在左上角為(0,0)和右下角為(x-1,y-1)的矩形之間切換所有元素。 切換元素意味著將 1 更改為 0,將 0 更改為 1。任務是找到對矩陣的所有元素進行置位(即,使所有元素均為 1)所需的最小操作。 **示例**: ``` Input : mat[][] = 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 Output : 1 In one move, choose (3, 3) to make the whole matrix consisting of only 1s. Input : mat[][] = 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 Output : 3 ``` 這個想法是從端點(N – 1,M – 1)開始,然后以相反的順序遍歷矩陣。 每當我們遇到一個值為 0 的單元格時,請將其翻轉。 *為什么要從終點穿過?* 假設在(x,y)和(x +1,y + 1)單元中有 0。 您不應該在單元格(x,y)之后翻轉單元格(x + 1,y + 1),因為在將(x,y)翻轉為 1 之后,下一步是翻轉(x + 1,y + 1)單元格 ,您將再次將(x,y)翻轉為 0。因此,從第一步開始翻轉(x,y)單元格沒有任何好處。 以下是此方法的實現: ## C++ ```cpp // C++ program to find minimum operations required // to set all the element of binary matrix #include <bits/stdc++.h> #define N 5 #define M 5 using namespace std; // Return minimum operation required to make all 1s. int minOperation(bool arr[N][M]) { ????int ans = 0; ????for (int i = N - 1; i >= 0; i--) ????{ ????????for (int j = M - 1; j >= 0; j--) ????????{ ????????????// check if this cell equals 0 ????????????if(arr[i][j] == 0) ????????????{ ????????????????// increase the number of moves ????????????????ans++; ????????????????// flip from this cell to the start point ????????????????for (int k = 0; k <= i; k++) ????????????????{ ????????????????????for (int h = 0; h <= j; h++) ????????????????????{ ????????????????????????// flip the cell ????????????????????????if (arr[k][h] == 1) ????????????????????????????arr[k][h] = 0; ????????????????????????else ????????????????????????????arr[k][h] = 1; ????????????????????} ????????????????} ????????????} ????????} ????} ????return ans; } // Driven Program int main() { ????bool mat[N][M] = ????{ ????????0, 0, 1, 1, 1, ????????0, 0, 0, 1, 1, ????????0, 0, 0, 1, 1, ????????1, 1, 1, 1, 1, ????????1, 1, 1, 1, 1 ????}; ????cout << minOperation(mat) << endl; ????return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看