判断valid,没有更好的方法,只能brute force。

 class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) { int n;
for (int i = ; i < ; ++i) {
vector<bool> contained(, false);
for (int j = ; j < ; ++j) {
if (board[i][j] == '.') continue;
n = board[i][j] - '' - ;
if (contained[n]) return false;
contained[n] = true;
}
} for (int i = ; i < ; ++i) {
vector<bool> contained(, false);
for (int j = ; j < ; ++j) {
if (board[j][i] == '.') continue;
n = board[j][i] - '' - ;
if (contained[n]) return false;
contained[n] = true;
}
} for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
vector<bool> contained(, false);
for (int k = ; k < ; ++k) {
for (int m = ; m < ; ++m) {
if (board[i*+k][j*+m] == '.') continue;
n = board[i*+k][j*+m] - '' - ;
if (contained[n]) return false;
contained[n] = true;
}
}
}
}
return true;
}
};

求解决方案也只有backtrack。

 class Solution {
public:
void solveSudoku(vector<vector<char> > &board) {
list<int> unsolved;
getUnsolved(board, unsolved);
recursive(board, unsolved);
} bool recursive(vector<vector<char> > &board, list<int> &unsolved) {
if (unsolved.empty()) return true;
int loc = unsolved.front();
int row = loc / ;
int col = loc % ; vector<bool> contained(, false);
int n;
for (int i = ; i < ; ++i) {
if (board[row][i] != '.') {
contained[board[row][i] - '' - ] = true;
}
if (board[i][col] != '.') {
contained[board[i][col] - '' - ] = true;
}
} row = row / ; col = col / ;
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
if (board[row * + i][col * + j] != '.') {
contained[board[row * + i][col * + j] - '' - ] = true;
}
}
} row = loc / ; col = loc % ;
for (int i = ; i < ; ++i) {
if (!contained[i]) {
board[row][col] = i + + '';
unsolved.pop_front();
if (recursive(board, unsolved)) return true;
board[row][col] = '.';
unsolved.push_front(loc);
}
} return false;
} void getUnsolved(vector<vector<char> > &board, list<int> &unsolved) {
for (int i = ; i < ; i++) {
for (int j = ; j < ; ++j) {
if (board[i][j] == '.') {
unsolved.push_back(i * + j);
}
}
}
}
};

用unsolved数组可以避免每次都需要从头扫到尾去找下一个元素。

用contained数组先保存了在该行该格该九宫格里已经存在的数字。这样就可以直接去试验剩下的数字,而不需要每次都再检查一遍插入的值是否合法。

backtrack是一个要有返回值,否则都不知道你backtrack到头了没,是否找到解决方案了。

Leetcode | Valid Sudoku & Sudoku Solver的更多相关文章

  1. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  2. 乘风破浪:LeetCode真题_037_Sudoku Solver

    乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决     这道题 ...

  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@ [36/37] Valid Sudoku / Sudoku Solver

    https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...

  5. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  6. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  7. LeetCode——Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  8. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  9. Valid Sudoku&&Sudoku Solver

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

随机推荐

  1. BestCoder17 1002.Select(hdu 5101) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...

  2. codeforces 476B.Dreamoon and WiFi 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/B 题目意思:给出两个字符串str1, str2,其中,str1 只由 '+' 和 '-' 组成,而 ...

  3. 解决Unable to locate Kerberos realm

    在windows环境下 将服务器上的/etc/krb5.conf 复制到<jdk-home>/jre/lib/security

  4. javascript十六进制数字和ASCII字符之间转换

    var hex="0x29";//十六进制 var charValue = String.fromCharCode(hex);//生成Unicode字符 var charCode ...

  5. 现在不能使用foxmail同步qq记事本功能,可能是对字数的大小有限制

    那么在经过了两个星期的时间完成的主要功能就是幻灯片和站点管理,在之后还有更多的任务要做,本来从时间的安排上不太合理,在这个月底要基本完成,主要是其他的组员也有比较重的任务,那么就是需要随时有一个状态, ...

  6. ytu 2030: 求实数绝对值(水题)

    2030: 求实数绝对值 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 10[Submit][Status][Web Board] ...

  7. Java Hour 14 多线程基础

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. 多线程 这个是基 ...

  8. 电赛总结(四)——波形发生芯片总结之AD9854

    一.特性参数 ·300M内部时钟频率 ·可进行频移键控(FSK),二元相移键控(BPSK),相移键控(PSK),脉冲调频(CHIRP),振幅调制(AM)操作 ·正交的双通道12位D/A转换器 ·超高速 ...

  9. isnull的使用方法

    is null 查看列数据为空 select*from lrb where lrid is null   ISNULL使用指定的替换值替换 NULL. 语法ISNULL ( check_express ...

  10. 【maven 报错】maven项目update之后报错One or more constraints have not been satisfied.

    在右键项目Update Project之后报错:One or more constraints have not been satisfied.Spring 4.1 requires Java 1.6 ...