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. SQL for SQLite

    语法 verb + subject + predicate commannds(命令) SQL由命令组成,以分号为结束.命令有token组成,token由white space分隔,包括空格.tab. ...

  2. Swift-Switch穿透

    switch vegetable {        case "celery":            print("Add some raisins and make ...

  3. Kotlin偏好设置

    Kotlin的强悍震精了我,android中每个应用都会用到SharedPreference在Kotlin中使用竟是如此简单! package com.android.extkt import and ...

  4. Java ThreadLocal

    ThreadLocal类,代表一个线程局部变量,通过把数据放在ThreadLocal中,可以让每个线程创建一个该变量的副本.也可以看成是线程同步的另一种方式吧,通过为每个线程创建一个变量的线程本地副本 ...

  5. 转:JQuery.Ajax之错误调试帮助信息

    今天发现一篇讲Ajax比较好的文章,汇总下,作为自己的知识储备. 下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. typ ...

  6. php鼠标滚动加载

    http://www.thinkphp.cn/extend/772.html 滚动距离js判断 i = 1; //设置当前页数 $(function() { var totalpage = 6; // ...

  7. CMPP3.0 长短信实现方案

    长短信息:是指超过70个汉字,140个字节的信息内容 一.CMPP协议相关字段分析 CMPP协议具体部分请参考<中国移动互联网短信网关接口协议(V3.0.0).doc> CMPP_SUBM ...

  8. SQL Server 2012实施与管理实战指南(笔记)——Ch4数据库连接组件

    4.数据库连接组件 访问数据库有多种不同的技术,包括ADO,ODBC,OLEDB,ADO.NET等这些都有一些共性.首先要建立连接(Connection),然后通过命令(Command)对数据库进行访 ...

  9. 自定义日志阅读器——包括了一个load取Tomcat日志的分析器

    最近在写往公司产品里添加Tomcat适配器,以支持Tomcat.有一些功能需要摘取到Tomcat的部分日志.没有合适的工具,也不想去网上找了,就自己写了一个. 简单的画了一下设计方案: 下面直接上代码 ...

  10. R语言数据的输入

    键盘输入 调用edit函数,比如我们要让用户输入一个长度为5的向量并赋值给变量a,那么可以: a<-vector() a<-edit(a) 另外也可以用函数fix来直接编辑变量,而不需要再 ...