Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17953   Accepted: 8688   Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in
this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Source

最近重装电脑,换成linux的ubuntu系统,还学了emacs的一点皮毛,被弄的晕头转向。。。

题目要ac倒是没有什么难度,最裸的暴力,没有加什么剪枝。分分钟水过。

15854066 ksq2013 2676 Accepted 704K 844MS G++ 1849B 2016-07-30 21:30:13
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int thd[4]={0,3,6,9},sdk[10][10];
bool col[10][10],row[10][10];
bool jud(int x,int y,int z)
{
int xs,xt,ys,yt;
for(int i=1;i<=3;i++)
if(x<=thd[i]){
xs=thd[i-1]+1;
xt=thd[i];
break;
}
for(int i=1;i<=3;i++)
if(y<=thd[i]){
ys=thd[i-1]+1;
yt=thd[i];
break;
}
for(int i=xs;i<=xt;i++)
for(int j=ys;j<=yt;j++)
if(!(sdk[i][j]^z))
return false;//this answer is ;
return true;
}
bool dfs()
{
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
if(!sdk[i][j]){
for(int k=1;k<=9;k++){
if((!col[j][k])&&(!row[i][k])&&jud(i,j,k)){
sdk[i][j]=k;
col[j][k]=row[i][k]=1;
if(dfs())return true;//selected the right answer;
col[j][k]=row[i][k]=0;
sdk[i][j]=0;
}
}
return false;//it was impossible to deduce an answer in a certain unit,then it's unreachable;
}
return true;//the units were filled correctly;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
memset(col,false,sizeof(col));
memset(row,false,sizeof(row));
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++){
char ch;
cin>>ch;
sdk[i][j]=ch-'0';
col[j][sdk[i][j]]=true;
row[i][sdk[i][j]]=true;//Init();
}
dfs();
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++)
printf("%d",sdk[i][j]);
putchar('\n');
}
}
return 0;
}

看了网上的题解后,发现反着搜,即从右下角向左上角搜更快逼近正解,于是稍稍修改了程序,果然快了许多倍。

15854143 ksq2013 2676 Accepted 704K 16MS G++ 1849B 2016-07-30 21:44:56
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int thd[4]={0,3,6,9},sdk[10][10];
bool col[10][10],row[10][10];
bool jud(int x,int y,int z)
{
int xs,xt,ys,yt;
for(int i=1;i<=3;i++)
if(x<=thd[i]){
xs=thd[i-1]+1;
xt=thd[i];
break;
}
for(int i=1;i<=3;i++)
if(y<=thd[i]){
ys=thd[i-1]+1;
yt=thd[i];
break;
}
for(int i=xs;i<=xt;i++)
for(int j=ys;j<=yt;j++)
if(!(sdk[i][j]^z))
return false;//this answer is ;
return true;
}
bool dfs()
{
for(int i=9;i>=1;i--)
for(int j=9;j>=1;j--)
if(!sdk[i][j]){
for(int k=1;k<=9;k++){
if((!col[j][k])&&(!row[i][k])&&jud(i,j,k)){
sdk[i][j]=k;
col[j][k]=row[i][k]=1;
if(dfs())return true;//selected the right answer;
col[j][k]=row[i][k]=0;
sdk[i][j]=0;
}
}
return false;//it was impossible to deduce an answer in a certain unit,then it's unreachable;
}
return true;//the units were filled correctly;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
memset(col,false,sizeof(col));
memset(row,false,sizeof(row));
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++){
char ch;
cin>>ch;
sdk[i][j]=ch-'0';
col[j][sdk[i][j]]=true;
row[i][sdk[i][j]]=true;//Init();
}
dfs();
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++)
printf("%d",sdk[i][j]);
putchar('\n');
}
}
return 0;
}

poj2676 Sudoku的更多相关文章

  1. poj2676 Sudoku(DFS)

    做了很久还是参考了别人的答案orz,其实也不难啊.我要开始学一下怎么写搜索了... 题目链接:poj2676 Sudoku 题解:暴力搜索,DFS每个空白格子所放数字. #include<cst ...

  2. POJ2676 – Sudoku(数独)—DFS

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24081   Accepted: 11242   Specia ...

  3. 【DLX算法】poj2676 Sudoku

    DLX算法求解精确覆盖问题模板.赛场上可以参见白书. #include<cstdio> #include<cstring> #include<vector> usi ...

  4. POJ2676 Sudoku(dfs)

    题目链接. 题目大意: 就是数独游戏.横竖,每一个9宫方块,必须有1~9,且不重复. 分析: 直接DFS.一开始在原图上搜,会TLE.把要补全的空格,放入数组,这样就不用遍历整个图寻找要填的空格了. ...

  5. POJ2676 Sudoku [数独]

    好题,也非常有用,犯了几个错误 1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举 2.Generic Programin ...

  6. POJ2676 Sudoku 舞蹈链 DLX

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的数独,求解.SPJ 题解 DLX + 矩阵构建  (两个传送门) 代码 #includ ...

  7. poj2676 Sudoku(搜索)

    题目链接:http://poj.org/problem?id=2676 题意:9*9的方格,0代表没数字,其他代表数字,请在格子中填入1~9的数字,使得在每行,每列和每个3*3的方块中,1~9的数字每 ...

  8. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...

  9. 【转】Dancing Links题集

    转自:http://blog.csdn.net/shahdza/article/details/7986037 POJ3740 Easy Finding [精确覆盖基础题]HUST1017 Exact ...

随机推荐

  1. 8款超实用JavaScript框架

    下面盘点了8款实用的JavaScript框架: 1. Hammer.js Hammer.js是被广泛使用的轻量级JavaScript框架,它提供了常用触摸操作的规范,比如收缩.拖放.双击和删除等等.它 ...

  2. Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论

    Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论 创建用户自定义的类加载器 要创建用户自定义的类加载器,只需要扩展java.lang.ClassLoader类,然后覆盖它的f ...

  3. iOS音频开发之`AudioStreamBasicDescription`

    这个类提供了对于音频文件的描述 An audio stream is a continuous series of data that represents a sound, such as a so ...

  4. 2016京东Android研发校招笔试题

    一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...

  5. IOS整体项目层级构建

    在创建IOS项目时,若有一个比较明确的层级架构,将对于今后代码的维护或者功能的扩展很有帮助:本文将通过一个实例来展现我对于层级的一些观点:里面有一些零碎的知识点可能无法全部介绍,到时提供源代码进行下载 ...

  6. OC 中的block存储位置

    以下所有在ARC情况下: 一.block块的存储位置(block块入口地址):可能存放在2个地方:代码区.堆区(程序分5个区,还有常量区.全局区和栈区,对于MRC情况下代码还可能存在栈区.关于分区详细 ...

  7. Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析

    在Android开发中为了inflate一个布局文件,大体有2种方式,如下所示: // 1. get a instance of LayoutInflater, then do whatever yo ...

  8. iOS做新浪微博sso授权登录遇到的一些坑

    新浪微博sso授权第三方登录,这里没有借助第三方框架,如shareSKD和友盟等,直接参考新浪官方SDK和文档. 过程中遇到几个坑,找了很久,好歹最后解决了,记录如下 问题1: _NSInlineDa ...

  9. XML,DTD,XSD,XSL的区别

    XML=可扩展标记语言(eXtensible Markup Language). 可扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可用 方便的方式建立,虽然XML ...

  10. javaScript基础语法(下)

    4.逻辑控制语句 4.1条件结构 条件结构分为if结构和switch结构. 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript ...