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 ...
随机推荐
- Database Connection Pool Library | Libzdb
Database Connection Pool Library | Libzdb A small, easy to use Open Source Database Connection Pool ...
- COCOS2D-X之帧动画的一种实现Demo
这个Demo主要是实现帧动画,建议游戏中少用帧动画.废话少说直接上代码. 一.我们直接在COCOS2D-X自带的HelloCpp的工程中添加代码即可.我们在初始化中添加如下代码并附上图片资源. CCS ...
- MySql安装(rpm)和启动配置
MySql安装(rpm)和启动配置 安装环境: OS:Oracle Linux 5.9 安装步骤: 1.解压MySql安装包 [root@bakdbserver mysql]# tar -xf MyS ...
- UVA11090 Going in Cycle!! (二分+SPFA推断有无负权)
Problem G: Going in Cycle!! Input: standard input Output: standard output You are given a weighted d ...
- 使用visual c++ 2005远程调试64位系统上32位与64位程序
一直都挺喜欢使用visual c++远程调试代码,它能展现给我们当时代码执行最真实的情景,今天有时间记录一下,以免以后忘了. 远程调试需要在被调试端安装服务器,对于visual c++ 2005来说调 ...
- VC调试技巧
Visual C++ 的 C 运行时刻函数库标识模板0xCD 已经分配的数据(alloCated Data)0xDD 已经释放的数据(Deleted Data)0xFD 被保护的数据 ...
- robots.txt禁止搜索引擎收录
禁止搜索引擎收录的方法 一.什么是robots.txt文件? 搜索引擎通过一种程序robot(又称spider),自动访问互联网上的网页并获取网页信息. 您可以在您的网站中创建一个纯文 ...
- oracle数据库单个数据文件的大小限制
之前没有仔细想过这个问题,因为总是不会用到,也没有犯过类似错误. 顺便提一下学习方法吧. 卤肉的学习方法是:常用知识点,熟悉理论并反复做实验,深入理解:不常用的知识点,相关内容都了解大概,遇到问题时想 ...
- android app 架构设计02
二:在开放的过程中,尽量把工具类,BaseActivity 放在指定的位置. DateFormat Bitmap Notification Shared Preference Environment ...
- Object.wait()的使用方法示例(转)
本文节选自 Effective Java by Joshua Bloch 和 Concurrent Programming in Java by Doug Lea. 1.3 原子数据的同步 java语 ...