POJ2676 Sudoku [数独]
好题,也非常有用,犯了几个错误
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 [数独]的更多相关文章
- poj2676 Sudoku(DFS)
做了很久还是参考了别人的答案orz,其实也不难啊.我要开始学一下怎么写搜索了... 题目链接:poj2676 Sudoku 题解:暴力搜索,DFS每个空白格子所放数字. #include<cst ...
- POJ2676 – Sudoku(数独)—DFS
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24081 Accepted: 11242 Specia ...
- Sudoku 数独游戏
#include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...
- POJ Sudoku 数独填数 DFS
题目链接:Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18105 Accepted: 8772 Sp ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- valid sudoku(数独)
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- 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 ...
- 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 ...
随机推荐
- 一切皆为 JavaScript
JavaScript起源于Netscape公司的LiveScript语言,这是一种基于对象和事件驱动的client脚本语言.最初的设计是为了检验HTML表单输入的正确性. 早些年,JavaScript ...
- HDU 3853 期望概率DP
期望概率DP简单题 从[1,1]点走到[r,c]点,每走一步的代价为2 给出每一个点走相邻位置的概率,共3中方向,不动: [x,y]->[x][y]=p[x][y][0] , 右移:[x][y ...
- 如何配置web服务器
如何配置web服务器 (1)了解DNS主机名和IP地址.如果此WEB服务器在Internet上,需向ISP申请和注册此服务器的DNS主机名和IP地址.如果此服务器只在企业网内使用,则在内部的DNS服务 ...
- 14.4.3.4 Configuring InnoDB Buffer Pool Prefetching (Read-Ahead) 配置InnoDB Buffer pool 预读
14.4.3.4 Configuring InnoDB Buffer Pool Prefetching (Read-Ahead) 配置InnoDB Buffer pool 预读 一个预读请求 是一个I ...
- Kali Linux 装好系统后安装经常使用软件
1.配置软件源 leafpad /etc/apt/source.list or(recommand): #官方源 deb http://http.kali.org/kali kali main no ...
- iOS视图控制器之间delegate传值教程
之前在StackOverFlow上看到一篇讲传值(segue传值和delegate传值)的文章,感觉讲的非常清晰,就将delegate部分翻译了一下.有兴趣能够看看. 原文: http://stack ...
- A Game of Thrones(20) - Eddard
Eddard Stark rode through the towering bronze doors of the Red Keep sore, tired, hungry, and irritab ...
- pdftk的使用介绍
首先像下面的一页pdf,如果想把它分成两页,每一页只是一个ppt页面(为了在kindle里读比较方便), 那么可以首先用A-pdf page cut, 将pdf 切成这样12个部分 然后我们现在要的只 ...
- mmap。
linux mmap 内存映射 mmap() vs read()/write()/lseek() 通过strace统计系统调用的时候,常常能够看到mmap()与mmap2().系统调用mmap()能够 ...
- OSG+VS2010+win7环境搭建
Win7下 osg+vs2010环境搭建 一.相关准备 a) Osg源代码 当前最新版:OpenSceneGraph的3.0.0.zip 下载链接: http://www.openscenegraph ...