写一个程序通过填充空格来解决数独。空格用 '.' 表示。

详见:https://leetcode.com/problems/sudoku-solver/description/

class Solution {
public:
bool solveSudoku(vector<vector<char> > &board)
{
for (int i = 0; i < 9; ++i)
for (int j = 0; j < 9; ++j)
{
if ('.' == board[i][j])
{
for (int k = 1; k <= 9; ++k)
{
board[i][j] = '0' + k;
if (isValid(board, i, j) && solveSudoku(board))
return true;
board[i][j] = '.';
}
return false;
}
}
return true;
} bool isValid(vector<vector<char> > &board, int x, int y)
{
int i, j;
for (i = 0; i < 9; i++)
if (i != x && board[i][y] == board[x][y])
return false;
for (j = 0; j < 9; j++)
if (j != y && board[x][j] == board[x][y])
return false;
for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
if (i != x && j != y && board[i][j] == board[x][y])
return false;
return true;
}
};

037 Sudoku Solver 解数独的更多相关文章

  1. [leetcode]37. Sudoku Solver 解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  2. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  4. LeetCode 037 Sudoku Solver

    题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...

  5. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  6. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  7. Java for LeetCode 037 Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. LeetCode OJ:Sudoku Solver(数独游戏)

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  9. Sudoku Solver, 求数独

    问题描述:填充数独表中空元素.空元素为'.' 算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法.然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后 ...

随机推荐

  1. 监听输入框变化(oninput,onpropertychange,onchange)

    oninput,onpropertychange,onchange: oninput是onpropertychange的非IE浏览器版本,支持firefox和opera等浏览器,但有一点不同,它绑定于 ...

  2. “libgomp.so.1: version `GOMP_4.0' not found” || “libstdc++.so.6: version `CXXABI_1.3.8' not found”错误

    类似问题还有 'ImportError ../lib/libstdc++.so.6: version `CXXABI_1.3.7' not found (required by xxx)'.      ...

  3. BZOJ_3529_[Sdoi2014]数表_莫比乌斯反演+树状数组

    Description 有一张 n×m 的数表,其第 i 行第 j 列(1 <= i <= n, 1 <= j <= m)的数值为 能同时整除 i 和 j 的所有自然数之和.给 ...

  4. DB2 Error Messages (Sorted by SQLCODE)

    DB2 Error Messages (Sorted by SQLCODE) DB2 Error Messages (Sorted by SQLCODE) SQLCODE SQLSTATE Descr ...

  5. WPF DelegateCommand CanExecute

     private DelegateCommand searchCommand;         public DelegateCommand SearchCommand         {       ...

  6. POCO库中文编程参考指南(7)Poco::Net::DatagramSocket

    1 构造函数 创建一个未连接的 IPv4 数据报 Socket: DatagramSocket(); 创建一个指定 IP 类型(IPv4 或 IPv6)的数据报 Socket: explicit Da ...

  7. 信息安全:Token

    ylbtech-信息安全:Token Token(计算机术语) 在计算机身份认证中是令牌(临时)的意思,在词法分析中是标记的意思. 1.令牌返回顶部 1.令牌 (信息安全术语) Token, 令牌,代 ...

  8. Python 查看本机WiFi密码

    http://www.lijiejie.com/python-get-all-saved-wifi-passwords/ 很早以前我写过一个,丢了.  今天偶然看到这篇文章 , 也是很久以前写的,他用 ...

  9. <正则吃饺子> :关于Collections中 比较器的简单使用

    在线文档地址: http://tool.oschina.net/apidocs/apidoc?api=jdk-zh sort public static <T extends Comparabl ...

  10. 32bit / 64bit co-exist Linux, ld-linux.so, linux-gate.so.1 etc

    before this, confirm that you don't have 32bit libs notably 32bit libc, e.g. you have /lib64/ld-linu ...