好题,也非常有用,犯了几个错误

1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举

2.Generic Programing写错了,,,本来那个memset想写成Generic Programing的,,,然后,永远仅仅有第一组结果对

不说了,泪哈,,,

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
int map[10][10];
char tmp[10][10];
bool row[10][10];
bool col[10][10];
bool grid[10][10];
bool DFS(int x,int y)
{
int here=3*((x-1)/3)+(y-1)/3+1;
if(x==10)
return true;
if(map[x][y])
{
bool flag;
return y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);
}
else
{
for(int num=1;num<=9;num++)
{
if(!row[x][num]&&!col[y][num]&&!grid[here][num])
{
map[x][y]=num;
row[x][num]=true;
col[y][num]=true;
grid[here][num]=true;
bool flag;
y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);
if(flag)
return true;
else
{
map[x][y]=0;
row[x][num]=false;
col[y][num]=false;
grid[here][num]=false;
}
}
}
}
return false;
}
int main()
{
//freopen("/home/rainto96/in.txt","r",stdin);
int test;
cin>>test;
while(test--)
{
memset(map,0,sizeof(map));
memset(tmp,0,sizeof(tmp));
memset(grid,0,sizeof(grid));
memset(col,0,sizeof(col));
memset(row,0,sizeof(row));
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
cin>>tmp[i][j];
int here=3*((i-1)/3)+(j-1)/3+1;
map[i][j]=tmp[i][j]-'0';
if(map[i][j])
{
row[i][map[i][j]]=true;
col[j][map[i][j]]=true;
grid[here][map[i][j]]=true;
}
}
}
DFS(1,1);
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
cout<<map[i][j];
}
cout<<'\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. Sudoku 数独游戏

    #include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...

  4. POJ Sudoku 数独填数 DFS

    题目链接:Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18105   Accepted: 8772   Sp ...

  5. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  6. valid sudoku(数独)

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  7. POJ 2676 Sudoku (数独 DFS)

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

  8. 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 ...

  9. 2015南阳CCPC H - Sudoku 数独

    H - Sudoku Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny g ...

随机推荐

  1. JPA的Embeddable注解

    来源于http://zjsword2000.blog.163.com/blog/static/4583983320083184844734/ 在hibernate中实现自定义类型,只要实现UserTy ...

  2. hdu1171(DP求两份物品的价值相差最小)

    题目信息: 给出一些物品的价值和个数.分成两份,是这两份的价值相差最小(DP方法) http://acm.hdu.edu.cn/showproblem.php? pid=1171 AC代码: /** ...

  3. 公司内网Debian Server上的openvpnserver搭建并通过SSH隧道转发到广域网

    因为戴维营公司在长沙的学员,研发人员和北京的研发人员须要协同研发,故须要让北京的兄弟们增加到长沙办公室的内网,訪问版本号 管理server,于是採用在内网server上搭建一个OpenVPN服务,并把 ...

  4. NET Core RC2

    .NET Core RC2/RTM 明确了时间表 .NET Core 经过了将近2年的开发,去年12月份发布的RC1版本,明确来说那只是一个beta版本,自从RC1发布以来,看到github里的RC2 ...

  5. thinkphp 3.2.3 入门示例2(URL传参数的几种方式)

    原文:thinkphp中URL传参数的几种方式 在thinkphp中,url传参合asp.net中原理类似,下面就单个参数和多个参数传递方式进行一个简单讲解 1.传单个参数 单个参数这种比较简单,例如 ...

  6. 小心LinkedHashMap的get()方法(转)

    这是一个来自实际项目的例子,在这个案例中,有同事基于jdk中的LinkedHashMap设计了一个LRUCache,为了提高性能,使用了 ReentrantReadWriteLock 读写锁:写锁对应 ...

  7. Scriptcase价格调整(五折销售)

    芬兰诺基亚!芬兰诺基亚!最大手机公司倒闭了!王八蛋老板埃洛普,吃喝嫖赌欠下了3.5个亿,带着他的小姨子跑了!我们没有办法,拿着手机抵工资!原价都是一千多.两千多.三千多的手机,统统二百块!统统二百块! ...

  8. A Game of Thrones(17) - Bran

    It seemed as though he had been falling for years. Fly, a voice whispered in the darkness, but Bran ...

  9. MySQL在一台db服务器上面如何启动多个实例

    安装过程省略过,源码安装请参考http://write.blog.csdn.net/postlist/1609043/all 整理自己的文档,发现以前做的例子,share下,欢迎大家提出改进意见. 一 ...

  10. I2C分析三

    1 引言 IIC (Inter-Integrated Circuit1总线是一种由Philips公司开发的2线式串行总线,用于连接微控制器及其外围设备.它是同步通信的一种特殊形式,具有接口线少.控制方 ...