<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國際加速解決方案。 廣告
                ## 一.題目描述 Write a program to solve a Sudoku puzzle by filling the empty cells.? Empty cells are indicated by the character ‘.’.? You may assume that there will be only one unique solution. The following photo is a sudoku puzzle… ![](https://box.kancloud.cn/2016-01-05_568bb5ee977fb.jpg) …and its solution numbers marked in red: ![](https://box.kancloud.cn/2016-01-05_568bb5eea7cee.jpg) ## 二.題目分析 注意題目說明了輸入保證有且只有一個解,所以試探每一個格子的時候,只需要考慮當前行、列、矩形框滿足條件,滿足就進入下一個格子試探,不滿足回溯。 首先,編寫一個函數`isValidSudoku()`用于測試矩陣當前坐標的值是否合法,檢查當前值在行、列以及`3*3`的矩陣內是否有效。 于是,對于矩陣中的每個空位`'.'`,嘗試填入`1~9`?并檢查是否合法,正確則往下一個位置遞歸。 只有正確達到矩陣最右下角位置才算填充成功,此時返回填充結果。 關于**回朔算法**,以下博客寫得淺顯易懂,可以參考參考: [http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741376.html](http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741376.html)? [http://www.cnblogs.com/Creator/archive/2011/05/20/2052341.html](http://www.cnblogs.com/Creator/archive/2011/05/20/2052341.html) ## 三.示例代碼 ~~~ #include <iostream> #include <vector> using namespace std; class Solution { public: bool isValidSudoku(vector<vector<char> > &board, int x, int y) { // 同一列中是否出現相同的數 for (int row = 0; row < 9; ++row) if ((x != row) && (board[row][y] == board[x][y])) return false; // 同一行中是否出現相同的數 for (int col = 0; col < 9; ++col) if ((y != col) && (board[x][col] == board[x][y])) return false; // 3 * 3方格中是否出現相同的數 for (int row = (x / 3) * 3; row < (x / 3 + 1) * 3; ++row) for (int col = (y / 3) * 3; col < (y / 3 + 1) * 3; ++col) if ((x != row) && (y != col) && (board[row][col] == board[x][y])) return false; // 滿足條件,則返回true return true; } bool internalSolveSudoku(vector<vector<char> > &board) { for (int row = 0; row < 9; ++row) { for (int col = 0; col < 9; ++col) { if (board[row][col] == '.') { for (int i = 1; i <= 9; ++i) { board[row][col] = '0' + i; if (isValidSudoku(board, row, col)) if (internalSolveSudoku(board)) return true; // 若當前格子的數不正確,將其重置為'.' // 然后進行下一個嘗試 board[row][col] = '.'; } // 0~9均重復,輸出false return false; } } } return true; } void solveSudoku(vector<vector<char> > &board) { internalSolveSudoku(board); } }; ~~~ 結果: ![](https://box.kancloud.cn/2016-01-05_568bb5eedbc8d.jpg) ## 四.小結 題目規定,輸入保證是有效的,因此每填入一個數,無需檢查整個矩陣是否合法,只需檢查填入數當前行、列和`3*3`矩陣是否合法即可。
                  <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>

                              哎呀哎呀视频在线观看