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. TCP Socket Programming in Node.js

    TCP Socket Programming in Node.js Posted on October 26th, 2011 under Node.jsTags: Client, node.js, S ...

  2. [Python]多线程入门

    原文:http://blog.csdn.net/ice110956/article/details/28421807 Python的多线程有两种实现方法: 函数,线程类 1.函数 调用thread模块 ...

  3. javascript中addEventListener(attachEvent)具体解释

    addEventListener 有三个參数:第一个參数表示事件名称(不含 on,如 "click").第二个參数表示要接收事件处理的函数:第三个參数为 useCapture.样例 ...

  4. TCP/IP具体解释学习笔记--TCP的超时与重传

    1.基本概念 TCP之所以能够安全的将数据在传输中的安全性,是因为它每次给对方发送数据,都会等待对方给个确认,当长时间收不到这个确认,发送端就会重发这个数据. 2.超时时间的測量 要測超时时间,TCP ...

  5. template.helper 多参数

    <script type="text/html" id="text4"> {{detail name classInfo schoolInfo}} ...

  6. Android Studio打开出现:Default activity not found

    昨天项目可以正常打开,没有问题,今天打开的时候就出现了这个问题.可以编译,但是无法生成APK调试.当然,如果选择 Do not launch Activity就可以成功编译.出现这个 Default ...

  7. SpringBoot添加对Mybatis的支持

    1.修改maven配置文件pom.xml,添加对mybatis的支持: <dependency> <groupId>org.mybatis.spring.boot</gr ...

  8. Jquery——几个注意的小知识

    event.stopPropagation() 停止事件冒泡 event.preventDefault()//组织默认行为(例如错误的时候,阻止按钮提交) event.type获取事件类型 event ...

  9. Android 如何预置APK M

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  10. Linux中进程与线程及CPU使用率查询

    一.进程查询: ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' 说明:PCPU是Cpu使用率,8核最多是800. 或者 ps -aux 二.线 ...