传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1426

Sudoku Killer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9804    Accepted Submission(s): 2944

Problem Description
自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视。
据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。
所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。

数独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证,空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都包含1-9这九个数字。

例题:

答案:

 
Input
本题包含多组测试,每组之间由一个空行隔开。每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开。其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数。
 
Output
对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开。两组解之间要一个空行。
对于每组测试数据保证它有且只有一个解。
 
Sample Input
7 1 2 ? 6 ? 3 5 8
? 6 5 2 ? 7 1 ? 4
? ? 8 5 1 3 6 7 2
9 2 4 ? 5 6 ? 3 7
5 ? 6 ? ? ? 2 4 1
1 ? 3 7 2 ? 9 ? 5
? ? 1 9 7 5 4 8 6
6 ? 7 8 3 ? 5 1 9
8 5 9 ? 4 ? ? 2 3
 
Sample Output
7 1 2 4 6 9 3 5 8
3 6 5 2 8 7 1 9 4
4 9 8 5 1 3 6 7 2
9 2 4 1 5 6 8 3 7
5 7 6 3 9 8 2 4 1
1 8 3 7 2 4 9 6 5
2 3 1 9 7 5 4 8 6
6 4 7 8 3 2 5 1 9
8 5 9 6 4 1 7 2 3
 
Author
linle
 
Source
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1045 2614 1067 1455 2952 
 
分析:
唉,没有心情分析了,先贴一下代码,找时间回来分析
code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<memory.h>
using namespace std;
void putresult(int b[][])//输出结果
{
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
if(j!=)
cout<<b[i][j]<<' ';
else
cout<<b[i][j];
}
cout<<endl;
}
}
void char2int(char a[][],int b[][])//字符数组转整型数组
{
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
if(a[i][j]=='?')
{
b[i][j/]=;
}
else if(a[i][j]>=''&&a[i][j]<='')
{
b[i][j/]=a[i][j]-'';
}
}
}
}
bool judge(int curx,int cury,int num,const int b[][])
{
int i,j; for(i=; i<; i++) //列
if(i!=cury && b[curx][i]==num)
return true; for(i=; i<; i++) //行
if(i!=curx && b[i][cury]==num)
return true; int x,y;
x = curx/*;
y = cury/*;
for(i=; i<; i++) //小九宫格
for(j=; j<; j++)
if(x+i!=curx && y+j!=cury && b[x+i][y+j]==num)
return true;
return false;
}
bool dfs(int b[][])
{
//找到当前第一个'?'位置。如果没有找到,表示所有位置都已填上,即为正确结果,递归结束
int i,j;
for(i=; i<; i++)
for(j=; j<; j++)
if(b[i][j]==) //找'?'的位置
goto label;
label:
if(i>= && j>=) //找到正确结果了,递归结束
return true;
//记录坐标
int curx = i,cury = j;
//确定该位置的可以填的数字
bool temp[]; //记录哪些数字可以填
int num=; //记录当前位置可以填的数字的个数
for(i=; i<=; i++)
if(judge(curx,cury,i,b)) //判断这个位置可不可以放这个数字
temp[i] = false;
else
{
temp[i] = true;
num++;
}
if(num==)
return false;
//确定下一个位置
for(i=; i<=; i++)
if(temp[i])
{
//这个数在这个位置可以填
if(judge(curx,cury,i,b))
continue;
b[curx][cury] = i;
if(dfs(b))
return true;
}
b[curx][cury] = ; //回退
return false;
} int main()
{
char a[][];
int b[][];
int i;
for(i=; i<; i++)
{
cin.getline(a[i],,'\n');
}
char2int(a,b);
if(dfs(b))
{
putresult(b);
}
while(cin.getline(a[],,'\n'))
{ cout<<endl;
char a[][];
int b[][];
int i;
for(i=; i<; i++)
{
cin.getline(a[i],,'\n');
}
char2int(a,b);
if(dfs(b))
{
putresult(b);
}
}
return ;
}

HDU 1426 Sudoku Killer(dfs 解数独)的更多相关文章

  1. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )

    利用 Dancing Link 来解数独 详细的能够看    lrj 的训练指南 和 < Dancing Links 在搜索中的应用 >这篇论文 Dancing Link 来求解数独 , ...

  4. HDU 1426 Sudoku Killer【DFS 数独】

    自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...

  5. HDU 1426 Sudoku Killer(搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 题意很明确,让你解一个9*9的数独. DFS即可. #include <cstdio> ...

  6. hdu 1426 Sudoku Killer

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 #include<stdio.h> #include<math.h> #in ...

  7. HDU 1426 Sudoku Killer (回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398818 题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位 ...

  8. HUD 1426 Sudoku Killer (DFS)

    链接 : Here! 思路 : 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数 ...

  9. 【原创】一个基于简单剪枝的DFS解数独程序

    问题来源:leetCode Sudoku Solver Write a program to solve aSudoku puzzle by filling the empty cells. Empt ...

随机推荐

  1. vs2013下c++调用python脚本函数 出现的一些问题总结

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9530834.html 首先是配置: 使用VS2013创建工程. 将libs中的python27 ...

  2. 【ExtJS】关于自定义组件

    一.命名规范 在你编码过程中对类,名字空间以及文件名使用统一的命名规则对你代码的组织,结构化以及可读性有很大的好处. 1.类命名规范: 类名最好只包含字母,在多数情况下,数字是不鼓励使用的,除非非要用 ...

  3. unity烘焙记录

    1.Unity Android 阴影不显示.阴影显示不正确 解决 http://blog.csdn.net/xuetao0605/article/details/50626181 2.阴影强度问题 不 ...

  4. php二维数组排序方法(array_multisort usort)

    一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...

  5. (一)安装Python

    一.安装python 打开 Python官网,找到“Download”, 在其下拉菜单中选择自己的平台(Windows/Mac),一般的Linux平台已经自带的Python,所以不需要安装,通过打开“ ...

  6. python学习(四)--POST请求

    from urllib import requestimport urllibimport re #网页版有道翻译是ajax的post请求. #浏览器请求地址http://fanyi.youdao.c ...

  7. python 之闭包

    原文 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_sum(*args): a ...

  8. SQL:exec sp_executesql 用法

    --這種是無效的過程 declare @sql nvarchar(500), @where nvarchar(500),@i nvarchar(64),@p nvarchar(50),@id int ...

  9. Android 判断当前Fragment是否可见(Visible)

    判断当前Fragment是否可见 public abstract class BaseFragment extends Fragment { /** Fragment当前状态是否可见 */ prote ...

  10. Write a makefile to compile *.c and link to executable target

    https://wenku.baidu.com/view/b1ec946027d3240c8447ef9a.html GNU+make中文手册V3.8 <=========From Docs== ...