也是一个简单剪枝的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. 微服务深入浅出(11)-- SpringBoot整合Docker

    添加Dockerfile 在目录src/main/resources目录下店家Dockerfile文件: From java MAINTAINER "Eric"<eric.l ...

  2. Kafka消息系统基础知识索引

    一些观念的修正 从 0.9 版本开始,Kafka 的标语已经从“一个高吞吐量,分布式的消息系统”改为"一个分布式流平台". Kafka不仅仅是一个队列,而且是一个存储,有超强的堆积 ...

  3. HDU 2391 Filthy Rich (dp)

    题目连接 Problem Description They say that in Phrygia, the streets are paved with gold. You're currently ...

  4. HDU 1010 Tempter of the Bone (广搜+减枝)

    题目链接 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. How ...

  5. cdn_一些常用的cdn地址

    https://code.jquery.com/jquery-3.3.1.jshttps://cdn.bootcss.com/jquery/2.1.4/jquery.min.js https://cd ...

  6. 通过BurpSuite和sqlmap配合对dvwa进行sql注入测试和用户名密码暴力破解

    0x1 工具和环境介绍 dvwa:渗透测试环境 BurpSuite:强大的WEB安全测试工具 sqlmap:强大的sql注入工具 以上工具和环境都在kali linux上安装和配置. 0x2 步骤说明 ...

  7. python(32):多进程(2) multiprocessing

    python 多线程:多线程 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心. Python提供了非常好用的多进程包multiprocessing,你只需要 ...

  8. Ubuntu下 git 服务器的搭建【转】

    转自:http://www.open-open.com/lib/view/open1391477731082.html 搭建git服务器的4个步骤 1   配置服务器前的准备工作 首先ubuntu系统 ...

  9. Interger不可变原理

    1.先看代码: package main.java.db.mq; public class TestSwap { public static void main(String[] args) { In ...

  10. Nginx - 日志格式及输出

    1. 前言 在 Nginx 服务器中,如果想对日志输出进行控制还是很容易的.Nginx 服务器提供了一个 HttpLogModule 模块,可以通过它来设置日志的输出格式. 2. HttpLogMod ...