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

数独填数,深搜+暴力。

自己也优化了很多,结果一直TLE。当然还没有优化够,比方说按照可供选择的多少排序,从少的开始深搜。但觉得太麻烦了。看得TLE快绝望了,结果看discuss要从后面搜,果然。。。

但是这个题目的数据给得也是够了,没有道理前面搜TLE,后面搜16ms的啊。。。真的是

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; char chess[15][15];
char test[15][15];
int m,n,flag;
vector<char>kefang[15][15]; bool pend(int row,int col,char value)
{
int i,j,temp1,temp2;
if(row%3==0)
temp1=row/3;
else
temp1=row/3+1;
if(col%3==0)
temp2=col/3;
else
temp2=col/3+1;
for(i=(temp1-1)*3+1;i<=temp1*3;i++)
{
for(j=(temp2-1)*3+1;j<=temp2*3;j++)
{
if(i==row&&j==col)continue;
if(value==test[i][j])return false;
}
} for(i=1;i<=9;i++)
{
if( i!=col && value==test[row][i])
return false;
}
for(i=1;i<=9;i++)
{
if( i!=row && value==test[i][col])
return false;
} return true;
} void find(int i_r,int j_r)
{
if(j_r==1)
{
for(m=i_r-1;m>=1;m--)
{
for(n=9;n>=1;n--)
{
if(chess[m][n]=='0')
return;
}
}
}
else
{
m=i_r;
for(n=j_r-1;n>=1;n--)
{
if(chess[m][n]=='0')
return;
}
for(m=i_r-1;m>=1;m--)
{
for(n=9;n>=1;n--)
{
if(chess[m][n]=='0')
return;
}
}
}
m=0;
n=0;
} void dfs(int i,int j,char u)
{
if(flag)
return;
test[i][j]=u; if(i<=1&&j<=1)
{
int h,k;
for(h=1;h<=9;h++)
{
for(k=1;k<=9;k++)
{
chess[h][k]=test[h][k];
}
}
flag=1;
return;
} find(i,j);
char temp_c;
int m_temp=m;
int n_temp=n;
int size=kefang[m_temp][n_temp].size();
int xu;
for(xu=0;xu<size;xu++)
{
temp_c=kefang[m_temp][n_temp][xu];
if(pend(m_temp,n_temp,temp_c))
{
dfs(m_temp,n_temp,temp_c);
}
}
if(m==0&&n==0)
{
int h,k;
for(h=1;h<=9;h++)
{
for(k=1;k<=9;k++)
{
chess[h][k]=test[h][k];
}
}
flag=1;
return;
} test[i][j]='0';
} void solve()
{
char temp_c;
int i,j;
for(i=9;i>=1;i--)
{
for(j=9;j>=1;j--)
{
if(chess[i][j]=='0')
{
int size=kefang[i][j].size();
int xu;
for(xu=0;xu<size;xu++)
{
temp_c=kefang[i][j][xu];
if(pend(i,j,temp_c))
{
dfs(i,j,temp_c);
if(flag)
return;
}
}
}
}
} } void init()
{
int g,d;
for(g=1;g<=9;g++)
{
for(d=1;d<=9;d++)
{
char temp_c;
for(temp_c='1';temp_c<='9';temp_c++)
{
if(pend(g,d,temp_c))
{
kefang[g][d].push_back(temp_c);
}
}
}
}
} int main()
{
int Test,i,j;
scanf("%d",&Test); while(Test--)
{
for(i=1;i<=9;i++)
{
scanf("%s",chess[i]+1);
for(j=1;j<=9;j++)
{
test[i][j]=chess[i][j];
}
}
for(i=1;i<=9;i++)
for(j=1;j<=9;j++)
kefang[i][j].clear();
flag=0;
init();
solve();
for(i=1;i<=9;i++)
{
cout<<chess[i]+1<<endl;
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2676:Sudoku 数独的更多相关文章

  1. POJ 2676 Sudoku (数独 DFS)

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

  2. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

  3. 深搜+回溯 POJ 2676 Sudoku

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

  4. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  5. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

    题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...

  6. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  7. POJ 2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12005   Accepted: 5984   Special ...

  8. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  9. POJ 2676 Sudoku(深搜)

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

  10. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

随机推荐

  1. REVISITING FINE-TUNING FOR FEW-SHOT LEARNING

    https://arxiv.org/pdf/1910.00216.pdf   明天看

  2. delphi对ZIP解压

    Delphi 对GZIP解压 作者:admin 来源:未知 日期:2010/5/9 13:08:46 人气:获取失败 标签: QQ空间新浪微博腾讯微博腾讯朋友QQ收藏百度空间百度贴吧更多0 呵呵,终于 ...

  3. iOS dismissViewControllerAnimated:completion:使用方法

    我们都知道dismissViewControllerAnimated:completion:方法是针对被present出来的控制器的,一般我们这样使用:在一个控制器中present另外一个控制器A,然 ...

  4. java#内部类和嵌套类

    内容思路来自Java编程思想,个人读书做的笔记,仅个人复习之用,故他人参考请自行辨别内容是否有错误. 在类的类部可以定义类,叫做内部类.如果这个内部类被static修饰,此时内部的类叫做嵌套类. 内部 ...

  5. linux 下office软件推荐

    概述 最近想使用LINUX下搭建服务器,所以查找一些需要用的软件. linux下最好的office解决办法 其实因为我是不怎么使用office的,我也不知道不同office有什么不一样,直到有一次写奖 ...

  6. spring core:@AliasFor的派生性

    spring对Annotation的派生性应用可谓炉火纯青,在spring core:@Component的派生性讲过支持层次上派生性,而属性上派生的需求则借助了@AliasFor,它是从spring ...

  7. MQ的调用

    mq调用(相关dll) using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collecti ...

  8. Ceph 概念理解

    简介 Ceph是一个可靠地.自动重均衡.自动恢复的分布式存储系统,根据场景划分可以将Ceph分为三大块,分别是对象存储.块设备存储和文件系统服务. 在虚拟化领域里,比较常用到的是Ceph的块设备存储, ...

  9. 吴裕雄--天生自然java开发常用类库学习笔记:取得当前日期

    import java.util.* ; // 导入需要的工具包 class DateTime{ // 以后直接通过此类就可以取得日期时间 private Calendar calendar = nu ...

  10. C语言中可变参数的原理——printf()函数

    函数原型: int printf(const char *format[,argument]...) 返 回 值: 成功则返回实际输出的字符数,失败返回-1. 函数说明: 使用过C语言的人所再熟悉不过 ...