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

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. IE缓存的BUG 和其他浏览器的缓存问题

    原文:IE缓存的BUG 和其他浏览器的缓存问题 1 .IE浏览器在异步请求的时候 为了偷懒  第二次请求的时候不会给后台请求相应,这样我们就无法从后台拿值,  这样的话 ,我们可以画个妆,蒙骗IE我们 ...

  2. Spring4 MVC 多文件上传(图片并展示)

    开始需要在pom.xml加入几个jar,分别是 <dependency> <groupId>commons-fileupload</groupId> <art ...

  3. 第四题(迅雷笔试题):编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

    #include <iostream> #include <stdlib.h> #include <pthread.h> using namespace std; ...

  4. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

  5. DOM - Element 对象

    http://www.runoob.com/dom/dom-element.html Element 对象 Element 对象代表 XML 文档中的一个元素.元素可以包含属性.其他元素或文本.如果一 ...

  6. 文件下载:"Content-disposition","attachment; filename=中文名>>>解决方案

    文件下载时常会出现如下问题: response.setHeader("Content-disposition","attachment; filename="+ ...

  7. &lt;xliff:g&gt;标签

    摘要: 这是Android4.3Mms源代码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sm ...

  8. 项目实践中--Git服务器的搭建与使用指南(转)

    一.前言 Git是一款免费.开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.在平时的项目开发中,我们会使用到Git来进行版本控制. Git的功能特性: 从一般开发者的角度来 ...

  9. birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典

    birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典 birdnest

  10. SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge (并不在同一个版本库中)

    在使用svn merge命令报错 英文版本:SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge 中文版本报错:并不 ...