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. Office 365 – SharePoint 2013 Online 之WebPart开发、部署教程

    1.打开Visual Studio,新建一个项目,选择SharePoint空项目,如下图: 2.选择调试站点和沙盒解决方案,如下图: 3.在项目中,添加一个WebPart,如下图: 4.添加完毕的项目 ...

  2. Android Handler机制(二)---MessageQueue源码解析

    MessageQueue 1.变量 private final boolean mQuitAllowed;//表示MessageQueue是否允许退出 @SuppressWarnings(" ...

  3. Double 数据保留两位小数一:五舍六入

    package com; public class T2 { public static void main(String[] args) { System.out.println(calculate ...

  4. Java 往年试卷参考答案!!!

    仅供参考: 第一题: E C E A D D C A C A C A B A B C C D B C 第二题: True True False 11 12 13 14 No such file fou ...

  5. 说说Angular中的$timeOut定时器

     非常不幸的一点是,人们似乎常常将AngularJS中的$timeOut()函数看做是一个内置的.无须在意的函数.但是,如果你忘记了$timeOut()的回调函数将会造成非常不好的影响,你可能会因此遇 ...

  6. C#生成注册码

    string t = DateTime.Now.Ticks.ToString(); t = DESKey.DESEncrypt(t, DESKey.DesKeyStr); string[] strid ...

  7. Sql Server之旅——第九站 看公司这些DBA们设计的这些复合索引

    这一篇再说下索引的最后一个主题,索引覆盖,当然学习比较好的捷径是看看那些大师们设计的索引,看从中能提取些什么营养的东西,下面我们看 看数据库中一个核心的Orders表. 一:查看表的架构 <1& ...

  8. Apache安装问题:configure: error: APR not found . Please read the documentation

    Linux上安装Apache时,编译出现错误: checking for APR... no configure: error: APR not found .  Please read the do ...

  9. 看美剧英文字幕学英语的利器——“深蓝英文字幕助手”简介

    我从初中开始基本上就是一个英语很烂的人,数理化再好有什么用,工作了,结果发现数理化都没啥用,最有用的还是当年学的最烂的英语.于是在2011年年底开始了学习英语的课程,在学习的过程中,外教经常会放英剧美 ...

  10. MySQL数据类型——数值类型

    1.1.1 整型 整型 占用字节 范围 范围 tinyint 1 -27~27-1 -128~127 smallint 2 -215~215-1 -32768~32767 mediumint 3 -2 ...