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. sqlmap笔记

    sqlmap -u "注入链接" --其他参数或命令 (-v 1表示回显出注入过程) [判断指定字段是否存在注入点]当链接包含两个参数时,可用-p开关选择要注入的参数,例检测id是 ...

  2. Android提交数据到JavaWeb服务器实现登录

    之前学习Android提交数据到php服务器没有成功,在看了两三个星期的视频之后,现在终于实现了与服务器的交互.虽然完成的不是PHP端的,但是在这个过程还是学到了不少东西的.现在我先来展示一下我的成果 ...

  3. Android九宫格界面实现点击每个格点击跳转界面

    刚开始有个任务就是做一个九宫格界面,后来有个任务就是实现点击每个格并跳转界面实现每个格的功能.下面我就介绍一下我是如何实现该功能的 首先写一下我的想法是: 登录成功后显示一个九宫格界面,每个九宫格的每 ...

  4. Android 自定义控件(一)

    本文用一个简单的例子来说明一下自定义控件的步骤实现,自定义控件有几种实现类型,分别为继承自view完全自定义,继承现有的 控件实现特定效果,继承viewgroup实现布局类等: 本文研究的是继承自vi ...

  5. OBST(Optimal Binary Tree最优二叉搜索树)

    二叉搜索树 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的 ...

  6. ASP.NET MVC 在控制器中获取某个视图动态的HTML代码

    如果我们需要动态的用AJAX从服务器端获取HTML代码,拼接字符串是一种不好的方式,所以我们将HTML代码写在cshtml文件中,然后通过代码传入model,动态获取cshtml中的HTML代码 当然 ...

  7. Bigfish的重新安装

    昨天把ofbiz安装并成功运行起来,今天接着再把bigfish下载下来. 上次安装配置bigfish时,记得是费了一番周折的,最后也不知道怎么瞎折腾,最终居然跑起来了. 今天有意识整理了下关键步骤,记 ...

  8. MySQL锁机制总结(二)

    前言: Mysql是一个支持插件式存储引擎的数据库系统,本文讨论的锁机制也主要包含两部分SERVER层的锁和存储引擎的锁,存储引擎是指innodb,其它存储引暂不讨论. 1. 数据库中锁相关的基本概念 ...

  9. cocoapods降级版本

    1.卸载当前版本sudo gem uninstall cocoapods 2.下载旧版本sudo gem install cocoapods -v 0.39.0 PS: 如果之前装有多版本,执行1之后 ...

  10. POI读写Word docx文件

    使用POI读写word docx文件 目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx ...