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.

const int SodukuSize = ;
bool row_mask[SodukuSize][SodukuSize];
bool col_mask[SodukuSize][SodukuSize];
bool area_mask[SodukuSize][SodukuSize]; bool initSudokuMask(vector< vector<char> > &board){
//reset the memory
memset(row_mask, false, sizeof(row_mask));
memset(col_mask, false, sizeof(col_mask));
memset(area_mask, false, sizeof(area_mask)); //check each rows and cols
for(int r=; r<board.size(); r++){
for (int c=; c<board[r].size(); c++){
if (!isdigit(board[r][c])) {
continue;
};
int idx = board[r][c] - '' - ; //check the rows/cols/areas
int area = (r/) * + (c/);
if (row_mask[r][idx] || col_mask[c][idx] || area_mask[area][idx] ){
return false;
}
row_mask[r][idx] = col_mask[c][idx] = area_mask[area][idx] = true;
}
}
return true;
} bool recursiveSudoKu(vector< vector<char> > &board, int row, int col){ if (row >= SodukuSize) {
return true;
} if (col >= SodukuSize){
return recursiveSudoKu(board, row+, );
} if (board[row][col] != '.'){
return recursiveSudoKu(board, row, col+);
}
//pick a number for empty cell
int area;
for(int i=; i<SodukuSize; i++){
area = (row/) * + (col/);
if (row_mask[row][i] || col_mask[col][i] || area_mask[area][i] ){
continue;
}
//set the number and sovle it recursively
board[row][col] = i + '';
row_mask[row][i] = col_mask[col][i] = area_mask[area][i] = true;
if (recursiveSudoKu(board, row, col+) == true){
return true;
}
//backtrace
board[row][col] = '.';
row_mask[row][i] = col_mask[col][i] = area_mask[area][i] = false;
}
return false;
}

37. Sudoku Solver *HARD*的更多相关文章

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

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

  2. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  3. 【LeetCode】37. Sudoku Solver

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

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

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

  5. 37. Sudoku Solver

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  6. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  7. leetcode problem 37 -- Sudoku Solver

    解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  8. 【LeetCode题意分析&解答】37. Sudoku Solver

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

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

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

随机推荐

  1. gcc5.2版本安装详解

    gcc5.2版本安装详解 1.下载gcc-5.2安装包 gcc各版本浏览地址:http://ftp.gnu.org/gnu/gcc/gcc-5.2浏览地址:http://ftp.gnu.org/gnu ...

  2. MySQL中INSERT的一般用法

    原文链接:http://www.blogjava.net/midnightPigMan/archive/2014/12/15/421406.html MySQL中INSERT的一般用法 INSERT语 ...

  3. 在Android Studio中创建项目和模拟器

    北京电子科技学院 实      验      报      告 课程:移动平台应用开发实践  班级:201592  姓名:杨凤  学号:20159213 成绩:___________  指导老师:娄嘉 ...

  4. Windows10 蓝屏 DRIVER_IRQL_NOT_LESS_OR_EQUAL (vfilter.sys)的可能解决方法

    早上我的笔记本从休眠中开机的时候突然出现了蓝屏,这个蓝屏在前几天出现过了.两次提示的终止代码都一样.我的笔记本型号是DELL XPS15 9560 我的笔记本配置: 类别 型号 内存 16GB DDR ...

  5. UVa 10891 Game of Sum - 动态规划

    因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低. 用$dp[i][j]$表示从$[i, j]$开始游戏,先手能够取得的最高分. 转移通过枚举取的数的个数$k$来转移.因为你希望先手得分 ...

  6. BZOJ 1063 道路设计(树形DP)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1063 题意:给出一个无环图( 也就是树,但是也有可能是森林),代表一个国家的城市.1是首 ...

  7. 2018 leetcode

    2018-8-6 Lowest Common Ancestor of a Binary Tree(二叉树) Merge Intervals(排序) 2018-8-7 Maximal Square (动 ...

  8. C#学习笔记(九):函数、代码查询和调试

    代码查询和调试 代码查询 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  9. WebForm、MVC、流式计算

    11月.NET技术讨论会圆满结束,会议纪要及相关文档如下如下:  1.WebForm预编译演示 文档:http://gitlab.light.fang.com/kongguDonet.Demo/Pre ...

  10. 用python + hadoop streaming 编写分布式程序(三) -- 自定义功能

    又是期末又是实训TA的事耽搁了好久……先把写好的放上博客吧 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍 ...