Bladeren bron

更新 'main.cc'

cyh 3 jaren geleden
bovenliggende
commit
3e4fe9853d
1 gewijzigde bestanden met toevoegingen van 98 en 96 verwijderingen
  1. 98 96
      main.cc

+ 98 - 96
main.cc

@@ -1,97 +1,99 @@
-/*
+/*
- * @lc app=leetcode.cn id=37 lang=cpp
+ * @lc app=leetcode.cn id=37 lang=cpp
- *
+ *
- * [37] 解数独
+ * [37] 解数独
- */
+ */
-#include <bits/stdc++.h>
+#include <bits/stdc++.h>
-using namespace std;
+using namespace std;
-// @lc code=start
+// @lc code=start
-
+
-void print(const vector<vector<char>>& board) {
+void print(const vector<vector<char>>& board) {
-  for (int i = 0; i < 9; ++i) {
+  for (int i = 0; i < 9; ++i) {
-    for (int j = 0; j < 9; ++j) {
+    for (int j = 0; j < 9; ++j) {
-      cout << board[i][j] << ",";
+      cout << board[i][j] << ",";
-    }
+    }
-    cout << endl;
+    cout << endl;
-  }
+  }
-}
+}
-
+
-class Solution {
+class Solution {
- public:
+ public:
-  void solveSudoku(vector<vector<char>>& board) {
+  void solveSudoku(vector<vector<char>>& board) {
-    vector<pair<int, int>> empty;
+    // 需要求解的位置坐标,最长81
-    for (int i = 0; i < 9; ++i) {
+    vector<pair<int, int>> empty;
-      for (int j = 0; j < 9; ++j) {
+    for (int i = 0; i < 9; ++i) {
-        if (board[i][j] == '.') {
+      for (int j = 0; j < 9; ++j) {
-          empty.emplace_back(i, j);
+        if (board[i][j] == '.') {
-        }
+          empty.emplace_back(i, j);
-      }
+        }
-    }
+      }
-    int n = empty.size();
+    }
-    function<bool(int, int)> vaild = [&](int i, int j) -> bool {
+    int n = empty.size();
-      array<int, 9> exist1 = {0, 0, 0, 0, 0, 0, 0, 0, 0};
+    // 检查一个位置合法性,无递归,空间O(m),m==9
-      array<int, 9> exist2 = {0, 0, 0, 0, 0, 0, 0, 0, 0};
+    function<bool(int, int)> vaild = [&](int i, int j) -> bool {
-      array<int, 9> exist3 = {0, 0, 0, 0, 0, 0, 0, 0, 0};
+      array<int, 9> exist1 = {0, 0, 0, 0, 0, 0, 0, 0, 0};
-      for (int k = 0; k < 9; ++k) {
+      array<int, 9> exist2 = {0, 0, 0, 0, 0, 0, 0, 0, 0};
-        if (board[i][k] != '.') {
+      array<int, 9> exist3 = {0, 0, 0, 0, 0, 0, 0, 0, 0};
-          if (exist1[board[i][k] - '1']) {
+      for (int k = 0; k < 9; ++k) {
-            return false;
+        if (board[i][k] != '.') {
-          }
+          if (exist1[board[i][k] - '1']) {
-          exist1[board[i][k] - '1'] = 1;
+            return false;
-        }
+          }
-        if (board[k][j] != '.') {
+          exist1[board[i][k] - '1'] = 1;
-          if (exist2[board[k][j] - '1']) {
+        }
-            return false;
+        if (board[k][j] != '.') {
-          }
+          if (exist2[board[k][j] - '1']) {
-          exist2[board[k][j] - '1'] = 1;
+            return false;
-        }
+          }
-      }
+          exist2[board[k][j] - '1'] = 1;
-      int r = i / 3 * 3;
+        }
-      int c = j / 3 * 3;
+      }
-      for (int k = 0; k < 3; ++k) {
+      int r = i / 3 * 3;
-        for (int l = 0; l < 3; ++l) {
+      int c = j / 3 * 3;
-          if (board[r + k][c + l] != '.') {
+      for (int k = 0; k < 3; ++k) {
-            if (exist3[board[r + k][c + l] - '1']) {
+        for (int l = 0; l < 3; ++l) {
-              return false;
+          if (board[r + k][c + l] != '.') {
-            }
+            if (exist3[board[r + k][c + l] - '1']) {
-            exist3[board[r + k][c + l] - '1'] = 1;
+              return false;
-          }
+            }
-        }
+            exist3[board[r + k][c + l] - '1'] = 1;
-      }
+          }
-      return true;
+        }
-    };
+      }
-
+      return true;
-    function<void(int)> f = [&](int i) {
+    };
-      if (i == n) {
+    // 递归回溯求解,内存O(1),调用深度最多n(n<=81)
-        throw exception();
+    function<void(int)> f = [&](int i) {
-      }
+      if (i == n) {
-      for (int j = 0; j < 9; ++j) {
+        throw exception();
-        board[empty[i].first][empty[i].second] = j + '1';
+      }
-        if (vaild(empty[i].first, empty[i].second)) {
+      for (int j = 0; j < 9; ++j) {
-          f(i + 1);
+        board[empty[i].first][empty[i].second] = j + '1';
-        }
+        if (vaild(empty[i].first, empty[i].second)) {
-        board[empty[i].first][empty[i].second] = '.';
+          f(i + 1);
-      }
+        }
-    };
+        board[empty[i].first][empty[i].second] = '.';
-    try {
+      }
-      f(0);
+    };
-    } catch (const exception& e) {
+    try {
-    }
+      f(0);
-    // cant be solved
+    } catch (const exception& e) {
-  }
+    }
-};
+    // cant be solved
-// @lc code=end
+  }
-int main() {
+};
-  Solution s;
+// @lc code=end
-  vector<vector<char>> b = {{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
+int main() {
-                            {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
+  Solution s;
-                            {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
+  vector<vector<char>> b = {{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
-                            {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
+                            {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
-                            {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
+                            {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
-                            {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
+                            {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
-                            {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
+                            {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
-                            {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
+                            {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
-                            {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
+                            {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
-  s.solveSudoku(b);
+                            {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
-  print(b);
+                            {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
+  s.solveSudoku(b);
+  print(b);
 }
 }