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 ...
随机推荐
- SNMP学习笔记之iReasoning MIB Browser
0x00 MIB Browser iReasoning MIB浏览器是一个强大和易于使用的工具由iReasoning SNMP API提供支持. MIB浏览器是工程师管理启用SNMP的网络设备和应用程 ...
- tomcat热启动没问题, 访问项目报500解决办法
新建maven项目 添加热启动 启动访问项目报错 报错提示 解决办法 思路:包冲突 在pom.xml中添加servlet <dependency> <groupId>javax ...
- 20145101《Java程序设计》第一周学习总结
20145101 <Java程序设计>第1周学习总结 教材学习内容总结 开学的第一周,通过课上老师的介绍和课下阅读教材我简单的了解java的发展历程,了解了JVM.JRE.JDK分别是什么 ...
- Django组件(一) Django之分页器
Django的分页器(paginator)简介 在页面显示分页数据,需要用到Django分页器组件 from django.core.paginator import Paginator Pagina ...
- codeforces 1097 Hello 2019
又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...
- Source not found :Edit Source Lookup Path 解决方案
作者原创,转载请注明转载地址 在eclipse中用debug调试的时候,出现了以下问题,很是尴尬,经常碰到,所以有必要进行总结一下: 对该问题有两种解决方案, 一种比较文明:解决方法可参考如下网址: ...
- 机器学习-数据可视化神器matplotlib学习之路(二)
之前学习了matplotlib的一些基本画图方法(查看上一节),这次主要是学习在图中加一些文字和其其它有趣的东西. 先来个最简单的图 from matplotlib import pyplot as ...
- Mui --- 学习笔记
1.mui 是选择器,popover 控制显示与隐藏,toggle 自动控制显示或隐藏 function showMenu(){ //mui是选择器 mui('#menu').popover('tog ...
- codeforces 350 div2 D Magic Powder - 2 二分
D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...
- WPF基础学习笔记整理 (八) 命令
基础知识: 命令是应用程序的任务,并跟踪任务是否能够被执行. 命令不包含执行应用程序任务的代码. 命令是比事件更高级的元素.默认的命令目标是当前获得焦点的元素. 良好的Win应用程序,应用程序逻辑不应 ...