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. Android 用Fragment创建一个选项卡

    本文结合之前的动态创建fragment来进行一个实践,来实现用Fragment创建一个选项卡 本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html,转 ...

  2. 3D Touch介绍:电子秤App与快捷操作

    随着iPhone6s与6s plus的到来,苹果给我们展现了一种全新的交互方式:重按手势.你可能知道,这个特性已经在Apple Watch和MacBook上推出了,不过那时叫Force Touch,就 ...

  3. Sublime Text 3 如何修改默认快捷键

    修改之前先备份快捷键的配置 问题所在 Sublime Text 3 出来了这么长时间,虽然是 Beta 版,还是决定尝试一波 在安装完之后,就想根据自己的习惯调整快捷键. 结果却发现,在 ST3 中, ...

  4. 开始对函数式编程 产生了尊崇感,因为Spring4.x ,Grooxy,Lisp,网易出来伞哥和他的博客

    1  无意看到"丢弃重口味的xml配置--spring4用groovy配置bean",这篇文章,里面说到spring4开始可以使用Groovy进行配置,可以取代xml方式和注解方式 ...

  5. .NET项目工程生成一份项目帮助文档chm--Sandcastle工具

    Sandcastle的,由Microsoft创建的,是从创建MSDN风格的文档中使用的工具.NET程序集和关联的XML注释文件.目前的版本是 2010年6月发布.这是命令行并没有GUI前端,项目管理功 ...

  6. 使用CSS3滤镜让图片反转颜色

    CSS提供的滤镜也是一大亮点,我一直痴迷其中,有些滤镜的效果很有用,可是有些的滤镜效果可能只是为了玩玩儿,CSS常见的滤镜有这些:grayscale, blur, sepia,所有常见的过滤器.但是如 ...

  7. WebService的介绍概念 收藏

    WebService学习总结(二)——WebService相关概念介绍 一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨 ...

  8. .Net Attribute详解(下) - 使用Attribute武装枚举类型

    接上文.Net Attribute详解(上)-Attribute本质以及一个简单示例,这篇文章介绍一个非常实用的例子,相信你一定能够用到你正在开发的项目中.枚举类型被常常用到项目中,如果要使用枚举To ...

  9. 【转发】NPAPI开发详解,Windows版

    NPAPI开发详解,Windows版 9 jiaofeng601, +479 9人支持,来自Meteor.猪爪.hanyuxinting更多 .是非黑白 .Yuan Xulei.hyolin.Andy ...

  10. u盘安装CENTOS后,启动missing operating system ,只能用U盘才能启动系统

    好久之前就想把家里闲置的那台老的不能再老的笔记本换成linux的,用来学习 从N久之前用光盘安装的时候发现光驱坏掉了之后就没有再装过,最近又想安装于是就试了U盘安装 U盘安装过程也很简单,只需要制作一 ...