Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11694   Accepted: 5812   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

 
(转)

大致题意:

九宫格问题,也有人叫数独问题

把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字。

0是待填位置,其他均为已填入的数字。

要求填完九宫格并输出(如果有多种结果,则只需输出其中一种)

如果给定的九宫格无法按要求填出来,则输出原来所输入的未填的九宫格

解题思路:

DFS试探,失败则回溯

用三个数组进行标记每行、每列、每个子网格已用的数字,用于剪枝

bool row[10][10];    //row[i][x]  标记在第i行中数字x是否出现了

bool col[10][10];    //col[j][y]  标记在第j列中数字y是否出现了

bool grid[10][10];   //grid[k][x] 标记在第k个3*3子格中数字z是否出现了

row 和 col的标记比较好处理,关键是找出grid子网格的序号与 行i列j的关系

即要知道第i行j列的数字是属于哪个子网格的

首先我们假设子网格的序号如下编排:

由于1<=i、j<=9,我们有: (其中“/”是C++中对整数的除法)

a= i/3 , b= j/3  ,根据九宫格的 行列 与 子网格 的 关系,我们有:

不难发现 3a+b=k

即 3*(i/3)+j/3=k

 

又我在程序中使用的数组下标为 1~9,grid编号也为1~9

因此上面的关系式可变形为 3*((i-1)/3)+(j-1)/3+1=k

 

 

有了这个推导的关系式,问题的处理就变得非常简单了,直接DFS即可

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int map[][]; bool row[][]; //row[i][x] 标记在第i行中数字x是否出现了
bool col[][]; //col[j][y] 标记在第j列中数字y是否出现了
bool grid[][]; //grid[k][z ] 标记在第k个3*3子格中数字z是否出现了 bool DFS(int x,int y){
if(x==)
return true;
bool flag=false;
if(map[x][y]!=){
if(y==)
flag=DFS(x+,);
else
flag=DFS(x,y+);
//return flag;
if(flag) //回溯
return true;
else
return false;
}else{
int k=*((x-)/)+(y-)/+;
for(int i=;i<=;i++) //枚举数字1~9填空
if(!row[x][i] && !col[y][i] && !grid[k][i]){
map[x][y]=i;
row[x][i]=true;
col[y][i]=true;
grid[k][i]=true;
if(y==)
flag=DFS(x+,);
else
flag=DFS(x,y+);
if(flag) //回溯,继续枚举
return true;
else{
map[x][y]=;
row[x][i]=false;
col[y][i]=false;
grid[k][i]=false;
}
}
}
return false;
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t); while(t--){
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(grid,false,sizeof(grid));
char str[][];
for(int i=;i<=;i++){
//scanf("%s",str[i]+1); //为啥这样输入就不行了呢??纳闷ing,明天再弄吧
for(int j=;j<=;j++){
cin>>str[i][j];
map[i][j]=str[i][j]-'';
if(map[i][j]){
int k=*((i-)/)+(j-)/+;
row[i][map[i][j]]=true;
col[j][map[i][j]]=true;
grid[k][map[i][j]]=true;
}
}
}
DFS(,);
for(int i=;i<=;i++){
for(int j=;j<=;j++)
printf("%d",map[i][j]);
printf("\n");
}
}
return ;
}

下面这个很快:

#include <iostream>
#include<cstdio>
#include<cstring> using namespace std; int num,v[][],map[][];
//bool pd[10][10]; //判断输入的时候是否为零 bool judge(int x,int y,int k){
int i,j,it,jt;
for(i=;i<;i++){
if(map[i][y]==k) return false;
if(map[x][i]==k) return false;
}
it=(x/)*;
jt=(y/)*;
for(i=;i<;i++)
for(j=;j<;j++)
if(map[i+it][j+jt]==k)
return false;
return true;
} int dfs(int cap){
int i,x,y;
if(cap<) return ; for(i=;i<=;i++){
x=v[cap][];
y=v[cap][];
if(judge(x,y,i)){
map[x][y]=i;
if(dfs(cap-)) return ;
map[x][y]=;
}
}
return ;
} int main(){
int t,i,j;
char c;
scanf("%d\n",&t);
while(t--){
num=;
for(i=;i<;i++,getchar())
for(j=;j<;j++){
scanf("%c",&c);
map[i][j]=c-'';
if(map[i][j]==){ //将为空的点的坐标全部记录下来,等下需要用暴力解决
v[num][]=i;
v[num++][]=j;
}
}
dfs(num-);
for(i=;i<;i++){
for(j=;j<;j++)
printf("%d",map[i][j]);
printf("\n");
}
}
return ;
}

POJ 2676 Sudoku (DFS)的更多相关文章

  1. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  2. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  3. POJ 1190 生日蛋糕(DFS)

    生日蛋糕 Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit Status Descrip ...

  4. poj2676 Sudoku(DFS)

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

  5. POJ - 3074 Sudoku (搜索)剪枝+位运算优化

    In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...

  6. POJ 2531-Network Saboteur(DFS)

    Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9435   Accepted: 4458 ...

  7. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  8. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  9. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

随机推荐

  1. (转)Unity3D研究院之IOS&Android收集Log文件

    转自:http://www.xuanyusong.com/archives/2477 有段时间没有写过文章了,不知道大伙儿还记得雨松MOMO吗? 嘿嘿. 开发项目的时候尤其在处理与服务器交互这块,如果 ...

  2. 一次jdbc乱码解决

    今天我做了一个小实验,从sqlserver 2010中将一张表转移到mysql中,使用的是基本的jdbc,前面复制的好好地,不知道怎么了,到了第三万行,突然出现了下面的异常 Incorrect str ...

  3. Android 八款开源 Android 游戏引擎

    原文地址 本文内容 Angle Rokon LGame AndEngine libgdx jPCT Alien3d Catcake 最近无意间看到一篇关于 Android 搜索引擎的文章,于是搜索了, ...

  4. 使用Genymotion模拟器或者手机运行ionic4程序

    1.使用命令行 #添加android, 如果是ios ,设置ioscordova platform add android #编译成apkionic build #开启cordova run andr ...

  5. Jenkins 集成Unity3D Xcode

    如果Mac 上没有安装brew.先安装:ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)& ...

  6. 【树莓派】crontab设置Linux设备定时重启

    简介:设置Linux设备定时重启或者关机 问题:有台设备每天总需要使用的人手动重启一下才可以正常工作,但是检查了日志,看起来服务一切都正常.时间和正确时间相差4mins. 解决办法: 1.增加定时任务 ...

  7. SqlServer插入1000条记录

    1.想在SqlServer中插入指定数量的测试记录怎么办? 2.解决: DECLARE @var INT ) BEGIN INSERT INTO test (Name) VALUES (convert ...

  8. MapReduce任务参数调优(转)

    http://blog.javachen.com/2014/06/24/tuning-in-mapreduce/ 本文主要记录Hadoop 2.x版本中MapReduce参数调优,不涉及Yarn的调优 ...

  9. Java并发容器之CopyOnWriteArraySet与ConcurrentSkipListSet

    一:CopyOnWriteArraySet CopyOnWriteArraySet底层其实是通过CopyOnWriteArrayList来实现的,通过组合一个CopyOnWriteArrayList作 ...

  10. MVC5为WebAPI添加命名空间的支持1

    前言 默认情况下,微软提供的MVC框架模板中,WebAPI路由是不支持Namespace参数的.这导致一些比较大型的项目,无法把WebApi分离到单独的类库中. 本文将提供解决该问题的方案. 微软官方 ...