判断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. Java for LeetCode 029 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  2. percona-xtrabackup备份mysql

    title: 1.percona-xtrabackup备份mysql date: 2016-04-10 23:19:12 tags: mysql categories: mysql --- 一.per ...

  3. UESTC 1215 (思维题 旋转)

    Secrete Master Plan Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  4. oracle sql developer数据表注释为乱码问题---解决

    参考网址:http://blog.163.com/jackie_howe/blog/static/19949134720121126879265/ 内容: 在windows中创建一个名为“NLS_LA ...

  5. vijos 1025 背包 *

    链接:点我 输入顺序又反了 #include<cstdio> #include<iostream> #include<algorithm> #include< ...

  6. Lua和C之间的交互

    转自:http://blog.csdn.net/sumoyu/article/details/2592693 (一) Lua 调C函数 什么样类型的函数可以被Lua调用   typedef int ( ...

  7. Intent实现页面跳转和传值

    *Intent称为意图,是Android各大组件连接的桥梁 1.Activity页面跳转 同一个包内 Intent intent = new Intent(); intent.setClass(Mai ...

  8. jq生成目录文件树jQuery Ztree基本用法

    转自:http://www.cnblogs.com/linjiqin/p/4547452.html 1.首先在页面上有<ul/>标签 ? 1 <ul id="tree&qu ...

  9. TextView属性大全

    今天研究了TextView一天了,发现网上有一篇讲TextView属性的,非常全,收藏一下先. 发现TextView有一个比较大的问题,就是文字排版的问题,遇到数字,字母,符号等就会有问题,目前还没有 ...

  10. jmeter性能测试实战-web登录测试

    一.项目背景: 网站信息: 操作系统类型 二.需求: 登录并发测试 三.场景: 1s增加两个线程,运行2000次 分别看20.40.60并发下的表现 四.监控: 成功率.响应时间.标准差.cpu.me ...