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 ...
随机推荐
- MySQL :: MySQL 5.0 Reference Manual :: 14.4 The MEMORY (HEAP) Storage Engine
MySQL :: MySQL 5.0 Reference Manual :: 14.4 The MEMORY (HEAP) Storage Engine The MEMORY (HEAP) Stora ...
- Phpcms V9 所有的中文变量
$LANG['start_update_category'] = '开始更新栏目页 ...'; $LANG['start_to_end_id'] = '" 第{page} - {endpag ...
- [More Effective C++]条款22有关返回值优化的验证结果
(这里的验证结果是针对返回值优化的,其实和条款22本身所说的,考虑以操作符复合形式(op=)取代其独身形式(op),关系不大.书生注) 在[More Effective C++]条款22的最后,在返回 ...
- HealthKit开发教程Swift版:起步
原文:HealthKit Tutorial with Swift: Getting Started 作者:Ernesto García 译者:Mr_cyz ) HealthKit是iOS 8中的新的A ...
- C文件操作的语言fgets()
谈fgets(..)功能. 原型 char * fgets(char * s, int n,FILE *stream); 參数: s: 字符型指针.指向存 ...
- Delphi TStream 详细介绍
Delphi TStream 详细介绍Stream对象,又称流式对象,是TStream.THandleStream.TFileStream.TMemoryStream.TResourceStream和 ...
- BestCoder Round #3HDU 4907
1. HDU 4907:http://acm.hdu.edu.cn/showproblem.php?pid=4907 中文题我就不说题意了,直接说解题思路吧! ① 第一种思路就是我比赛时的思路,将a数 ...
- cocos2dx--两个场景切换各函数调用顺序
场景A切换到场景B,有切换特效 调用顺序例如以下:(AAABABABA) A:构造函数 A:onEnter A:onEnterTransitionDidFinish B:构造函数 A:onExitTr ...
- DelphiXE 显示GIF动画
DelphiXE可以直接显示GIF动画,不需要第三方控件的支持.只要引用GifImg单元即可. uses GIFImg; procedure TForm1.FormCreate(Sender: TOb ...
- LINUX编程学习笔记(十四) 创建进程与 父子进程内存空间
1什么是进程:进程是一个执行中的程序 执行的程序: 代码->资源->CPU 进程有很多数据维护:进程状态/进程属性 所有进程属性采用的一个树形结构体维护 ps -a//所有进程 ps - ...