37. Sudoku Solver *HARD*
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*的更多相关文章
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- 37. Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- leetcode problem 37 -- Sudoku Solver
解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
随机推荐
- Python Web学习笔记之并发和并行的区别和实现
你吃饭吃到一半,电话来了,你一直到吃完了以后才去接,这就说明你不支持并发也不支持并行.你吃饭吃到一半,电话来了,你停了下来接了电话,接完后继续吃饭,这说明你支持并发.你吃饭吃到一半,电话来了,你一边打 ...
- HTML 语义化标签-新增标签介绍
HTML 基础知识 版权声明:未经博主授权,内容严禁转载 ! HTML语义化标签概念 如果没有语义化标签,上面这些 div 都是没有实际意义的,只是我们提供给浏览器的指令. 和 语义化 代码对比: 什 ...
- PHP安装包TS和NTS的区别
原文链接:http://blog.csdn.net/zhuifengshenku/article/details/38796555 TS指Thread Safety,即线程安全,一般在IIS以ISAP ...
- 20145101《Java程序设计》第5周学习总结
20145101<Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 Java是通过try,catch,throw,throws,finally这5个关键字来实现异常处理 ...
- 20145301赵嘉鑫 《网络对抗》Exp9 Web安全基础实践
20145301赵嘉鑫 <网络对抗>Exp9 Web安全基础实践 实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入攻击原理:SQL 是一门 ANSI 的标准计算机语言,用来访 ...
- IOS项目中的细节处理,如更改状态栏等等
一,状态栏更改为白色 1 在info.plist中添加一个字段:view controller -base status bar 为NO 2 在需要改变状态栏颜色的ViewController中在Vi ...
- extgcd 扩展欧几里得算法模板
#include <bits/stdc++.h> using namespace std; int extgcd (int a,int b,int &x,int &y){ ...
- RMQ 区间最大值 最小值查询
/*RMQ 更新最小值操作 By:draymonder*/ #include <iostream> #include <cstdio> using namespace std; ...
- [luogu 1070]道路游戏(NOIP2009T4)
题目链接 题解: 题目描述好长啊.... 大概就是设一下$f[i]$表示第i秒的最大价值 首先枚举时间,然后因为机器人这一秒无论在哪里都是有可能的,所以要枚举一下每个点,又因为最多走p秒所以再枚举一下 ...
- POJ 1029 False coin
http://poj.org/problem?id=1029 题意: 在一堆硬币中有一个假硬币,重量是重是轻不知道.每次称量多个硬币,并给出称量结果.判断依据题目给出的几次称量结果能否找出假硬币. 思 ...