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. 在uboot中加入cmd_run命令,运行环境变量

    在学习uboot的过程中会经常烧录程序,每次都要敲一些下载指令.这样是不是很麻烦,有什么办法能快速的烧写呢.很简单,将需要敲击的指令编译到uboot中,以环境变量的形式存在.但是环境变量很好加,如何运 ...

  2. Nodejs学习笔记(四)与MySQL交互(felixge/node-mysql)

    原文链接:http://www.cnblogs.com/zhongweiv/p/nodejs_mysql.html 介绍使用felixge/node-mysql进行SQL的增删改查以及断线重连等操作.

  3. 20145220韩旭飞《网络对抗》Exp2 后门原理与实践

    20145220韩旭飞<网络对抗>Exp2 后门原理与实践 常用后门工具实践 Windows获得Linux Shell 在Windows下,先使用ipconfig指令查看本机IP: 使用n ...

  4. 20145304 Exp7 网络欺诈技术防范

    20145304 Exp7 网络欺诈技术防范 实验后回答问题 1.通常在什么场景下容易受到DNS spoof攻击 在公共网络下,如一些购物场所.咖啡馆.快餐店等提供的网络下:当自己常使用的无线网被有恶 ...

  5. java.lang.NoClassDefFoundError错误

    根据前文,很明显NoClassDefFoundError的错误是因为在运行时类加载器在classpath下找不到需要加载的类,所以我们需要把对应的类加载到classpath中,或者检查为什么类在cla ...

  6. C#对两种类型动态库的使用

    一.托管:如果一个动态库本身是基于.NET的,那么可以直接在工程引用里右键添加引用,如微软的COM技术[因为你依托的是微软的框架,所以需要regsvr32注册] 二.非托管:如果不是基于.NEt的,那 ...

  7. 高斯日记|2013年蓝桥杯B组题解析第一题-fishers

    高斯日记 大数学家高斯有个好习惯:无论如何都要记日记. 他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210 后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几 ...

  8. centos6搭建redis集群搭建(单机多节点)

    一.安装redis 1.安装gcc环境 yum install gcc-c++ 2.下载源码包并解压 wget http://download.redis.io/releases/redis-3.2. ...

  9. 【Tomcat部署】Linux环境部署war包到tomcat

    以turbine为例. 一.部署 1.下载或者生成war包(从maven上下载war包,并改名字为turbine.war) 2.将turbine.war拷贝到$TOMCAT_HOME/webapps中 ...

  10. 第七章 对称加密算法--DES

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第7章“初等加密算法--对称加密算法” 7.1.对称加密算法 特点: 加密与解密使用同一个密钥 是使用最广的算法 常见对称加密 ...