HDU 1426 Sudoku Killer(dfs 解数独)
传送门:
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
据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。
所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。
数独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证,空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都包含1-9这九个数字。
例题:
答案:
对于每组测试数据保证它有且只有一个解。
? 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
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
#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 解数独)的更多相关文章
- hdu 1426 Sudoku Killer (dfs)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )
利用 Dancing Link 来解数独 详细的能够看 lrj 的训练指南 和 < Dancing Links 在搜索中的应用 >这篇论文 Dancing Link 来求解数独 , ...
- HDU 1426 Sudoku Killer【DFS 数独】
自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...
- HDU 1426 Sudoku Killer(搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 题意很明确,让你解一个9*9的数独. DFS即可. #include <cstdio> ...
- hdu 1426 Sudoku Killer
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 #include<stdio.h> #include<math.h> #in ...
- HDU 1426 Sudoku Killer (回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398818 题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位 ...
- HUD 1426 Sudoku Killer (DFS)
链接 : Here! 思路 : 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数 ...
- 【原创】一个基于简单剪枝的DFS解数独程序
问题来源:leetCode Sudoku Solver Write a program to solve aSudoku puzzle by filling the empty cells. Empt ...
随机推荐
- 转-------基于R-CNN的物体检测
基于R-CNN的物体检测 原文地址:http://blog.csdn.net/hjimce/article/details/50187029 作者:hjimce 一.相关理论 本篇博文主要讲解2014 ...
- c#高级写法
Linq的参考资料:https://www.cnblogs.com/liqingwen/p/5801249.html 1.判断str字符串中的逗号个数 string str = "1,2,3 ...
- PHP Mongodb API参考
<?php /*** Mongodb类** examples: * $mongo = new HMongodb("127.0.0.1:11223"); * $mongo-&g ...
- 支付宝小程序与微信小程序开发功能和语法糖不同
最近开始负责公司webapp数据打通支付宝小程序,之前已经打通了微信小程序,现在根据支付宝小程序的开发文档在之前的模板上面做修改. 在修改模板的过程中,总结一下双方功能和语法糖的不同之处. 框架: a ...
- JQuery选择器——《锋利的JQuery》
刚学CSS的时候我们已经接触了选择器,其实就是按照一定的规则选择出来我们想要获取到的元素.在这里,既然选择了用jQuery选择器,首先来谈谈JQuery选择器的优势: 1.简洁的写法:$()函数在很多 ...
- AngularJS 实现 Table的一些操作(示例大于实际)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script ty ...
- 引用SQLHelper类configurationmanager 不存在
在使用Sqlhelper类时,出现cs0103错误 当前上下文中不存在名称configurationmanager 解决方案,除了using引用using System.Configuration外, ...
- git获取别人远程dev分支上的代码
我们在使用 git clone xxx.git 下载代码的时候,获取到的只是 master上的代码 假入有个 dev 分支我们想获取上面的代码怎么办! #下载dev分支上的代码并切换到dev分支 g ...
- java中如何遍历实体类的属性和数据类型以及属性值
package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTa ...
- pdf转为html查看pdf.js
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...