也是一个简单剪枝的dfs。记录所有为0的位置,依次填写,当发现某个空格可选的填写数字已经没有时,说明该支路无效,剪掉。

不算是一个难题吧,但是还是花了不少时间,问题主要出在细节上,行列坐标反了、3乘3小格的位置判断等。写程序一定要细心。

#include <iostream>
using namespace std; const int MAX_R = ;
int map[MAX_R + ][MAX_R + ];
int zero_pos[MAX_R * MAX_R][];
int possible_digits[MAX_R * MAX_R][];
int zero_cnt; int setPossibleDigits(int x, int y, int s)
{
bool possible[];
memset(possible, , sizeof(possible));
for (int i = ; i <= MAX_R; i++){
possible[map[i][x]] = true;
possible[map[y][i]] = true;
}
int subX = (x - ) / ;
int subY = (y - ) / ;
for (int i = subX * + ; i <= subX * + ; i++){
for (int j = subY * + ; j <= subY * + ; j++){
possible[map[j][i]] = true;
}
}
int cnt = ;
for (int i = ; i <= ; i++){
if (!possible[i])
possible_digits[s][cnt++] = i;
}
return cnt;
} bool dfs(int step)
{
if (step == zero_cnt){
return true;
}
int curX = zero_pos[step][],
curY = zero_pos[step][];
int possible_cnt = setPossibleDigits(curX, curY, step);
if (possible_cnt == ){
return false;
}
for (int i = ; i < possible_cnt; i++){
map[curY][curX] = possible_digits[step][i];
if (dfs(step + ))
return true;
else
map[curY][curX] = ;
}
return false;
} int main()
{
int t;
cin >> t;
while (t--){
memset(map, , sizeof(map));
memset(zero_pos, , sizeof(zero_pos));
memset(possible_digits, , sizeof(possible_digits));
zero_cnt = ;
for (int i = ; i <= MAX_R; i++){
for (int j = ; j <= MAX_R; j++){
char ch;
cin >> ch;
map[i][j] = ch - '';
if (map[i][j] == ){
zero_pos[zero_cnt][] = i;
zero_pos[zero_cnt++][] = j;
}
}
}
dfs();
for (int i = ; i <= MAX_R; i++){
for (int j = ; j <= MAX_R; j++){
cout << map[i][j];
}
cout << endl;
}
}
return ;
}

poj2676(数独)的更多相关文章

  1. POJ2676 (数独问题 + DLX + 状态优化顺序)

    (1)最简单的最是去暴力DFS搜索答案 , 很容易想到 , 每行每列的方式去搜索 , 不过效率是真的不行;但这个还是给出代码 ,毕竟打了也不容易呀! #include<cstdio> #i ...

  2. POJ2676,HDU4069解决数独的两种实现:DFS、DLX

    搜索实现:解决数独有两种思考策略,一种是枚举当前格能填的数字的种数,这里有一优化策略就是先搜索能填入种数小的格子:另一种是考虑处理某一行(列.宫)时,对于某一个没用过的数字,若该行(列.宫)只有一个可 ...

  3. POJ2676 Sudoku [数独]

    好题,也非常有用,犯了几个错误 1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举 2.Generic Programin ...

  4. POJ2676 – Sudoku(数独)—DFS

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24081   Accepted: 11242   Specia ...

  5. 【POJ - 2676】Sudoku(数独 dfs+回溯)

    -->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...

  6. LintCode389.判断数独是否合法

    LintCode简单题:判断数独是否合法 问题描述: 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项: 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...

  7. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. [LeetCode] Valid Sudoku 验证数独

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

  9. 数独 JAVA实现

    数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的 然后大二学了JAVA,看了下那个一些有关于 ...

随机推荐

  1. 17、enum简介

    enum简介 在日常开发中可能有一些东西是固定的,比如一年只有4个季节,春夏秋冬.我们可以自己定义一个类里面存放这4个季节.在jdk5之后,引入了枚举(enum)的概念,可以通过enum去定义这四个季 ...

  2. Go语言的各种Print函数

    Go语言的各种Print函数 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) func Pr ...

  3. 2016.08.02 math(leetcode) 小结

    math(leetcode) 小结 在leetcode中有些知识点(套路) 判断一个数是不是能被某些数整除,可以用 n%x == 0,循环除的话,就将while(n%x == 0)的循环条件设置判断整 ...

  4. mvn打war包以及解压包的方法

    有时候我们需要查看打成war包之后的目录,如果是maven项目我们可以直接用maven打包. 1.maven打包: 第一种: mvn package 如果不行先 mvn clean一下 第二种:(掌握 ...

  5. 【杂谈】需要mark的一些东西

    https://riteme.github.io/blog/2017-10-28/oi-coding-guidelines.html https://www.luogu.org/blog/34238/ ...

  6. maven2 up to maven3的'version' contains an expression but should be a constant

    在Maven2时,为了保障版本一致,一般之前我们的做法时: Parent Pom中 <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  7. 解决eclipse Debug时提示source not found的问题

    解决办法: 选择Change Attached  Source,添加自己的project,clean项目,重启eclipse即可.

  8. php CI框架

    CodeIgniter 是一个小巧但功能强大的 PHP 框架,作为一个简单而“优雅”的工具包,它可以为 PHP 程序员建立功能完善的 Web 应用程序.如果你是一个使用共享主机,并且为客户所要求的期限 ...

  9. 03 Editor plugins and IDEs 编辑器插件和 ide

    Editor plugins and IDEs  编辑器插件和 ide Introduction  介绍 Options 选项   Introduction 介绍 This document list ...

  10. 应用服务器中对JDK的epoll空转bug的处理

    原文链接:应用服务器中对JDK的epoll空转bug的处理 前面讲到了epoll的一些机制,与select和poll等传统古老的IO多路复用机制的一些区别,这些区别实质可以总结为一句话, 就是epol ...