**链接 : ** Here!

**思路 : ** 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数组来辅助记录. $visR[i][num] = 1, 代表第 i 行num值已经出现过, visC[i][num] = 1, 代表第 i 列num值已经出现过, visB[i][num] = 1, 代表第 i 小块 num值已经出现过$. 计算小块的标号只需要 $x / 3 * 3 + y / 3 $ 即可.

类比 : ** 与之相似的题目是 hihocoder 1321, 但是这样做会 T, 所以 hihocoder 需要用舞蹈链**

**注意 : ** 此题读入十分恶心, 最好在写完读入操作后检查一下. 还需要注意, 如果是已经给出的值 (即非'?'), 需要先标记一下.


/*************************************************************************
> File Name: 1426-Sudoku-Killer.cpp
> Author:
> Mail:
> Created Time: 2017年11月29日 星期三 17时16分39秒
************************************************************************/ #include <bits/stdc++.h>
using namespace std; #define MAX_N 20
struct Point {
int x, y;
} pt[110];
int node_cnt = 0;
int G[MAX_N][MAX_N];
int visR[MAX_N][MAX_N] = {0};
int visC[MAX_N][MAX_N] = {0};
int visB[MAX_N][MAX_N] = {0};
int ok = 0; void clear() {
node_cnt = 0;
ok = 0;
memset(G, 0, sizeof(G));
memset(visR, 0, sizeof(visR));
memset(visC, 0, sizeof(visC));
memset(visB, 0, sizeof(visB));
}
int cal_block(int x, int y) {
return (x / 3 * 3 + y / 3);
}
bool check(int x, int y, int block, int num) {
return !(visR[x][num] || visC[y][num] || visB[block][num]);
}
void set_pt(int x, int y, int block, int num) {
visR[x][num] = visC[y][num] = visB[block][num] = 1;
}
void move_pt(int x, int y, int block, int num) {
visR[x][num] = visC[y][num] = visB[block][num] = 0;
}
void dfs(int step) {
if (ok) return;
if (step == node_cnt) {
for (int i = 0 ; i < 9 ; ++i) {
for (int j = 0 ; j < 8 ; ++j) {
printf("%d ", G[i][j]);
}
printf("%d\n", G[i][8]);
}
ok = 1;
return;
}
for (int i = 1 ; i <= 9 ; ++i) {
int x = pt[step].x;
int y = pt[step].y;
int block = cal_block(x, y);
if (!check(x, y, block, i)) continue;
set_pt(x, y, block, i);
int t_val = G[x][y];
G[x][y] = i;
dfs(step + 1);
G[x][y] = t_val;
move_pt(x, y, block, i);
}
return;
}
int main() {
int kase = 0;
char ch;
while (scanf("%c", &ch) != EOF) {
if (ch == '\n') continue;
if (kase) printf("\n");
++kase; G[0][0] = (ch == '?' ? 0 : (ch - '0'));
if (G[0][0] == 0) {
pt[node_cnt].x = 0;
pt[node_cnt].y = 0;
++node_cnt;
} else {
set_pt(0, 0, cal_block(0, 0), G[0][0]);
}
for (int i = 1 ; i < 9 ; ++i) {
scanf("%c", &ch);
if (ch == ' ') {
--i; continue;
}
G[0][i] = (ch == '?' ? 0 : (ch - '0'));
if (G[0][i] == 0) {
pt[node_cnt].x = 0;
pt[node_cnt].y = i;
++node_cnt;
} else {
set_pt(0, i, cal_block(0, i), G[0][i]);
}
}
for (int i = 1 ; i < 9 ; ++i) {
getchar();
for (int j = 0 ; j < 9 ; ++j) {
scanf("%c", &ch);
if (ch == ' ') {
--j; continue;
}
G[i][j] = (ch == '?' ? 0 : (ch - '0'));
if (G[i][j] == 0) {
pt[node_cnt].x = i;
pt[node_cnt].y = j;
++node_cnt;
} else {
set_pt(i, j, cal_block(i, j), G[i][j]);
}
}
}
dfs(0);
clear();
}
return 0;
}

HUD 1426 Sudoku Killer (DFS)的更多相关文章

  1. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. HDU 1426 Sudoku Killer(dfs 解数独)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...

  3. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. HDU 1426 Sudoku Killer【DFS 数独】

    自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...

  5. hdu 1426 Sudoku Killer

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 #include<stdio.h> #include<math.h> #in ...

  6. HDOJ Sudoku Killer(dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 思路分析:该问题为数独问题,明显解是唯一的,所有采用dfs搜索效果更好: 在搜索时,可以通过3个 ...

  7. HDU 1426 Sudoku Killer(搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 题意很明确,让你解一个9*9的数独. DFS即可. #include <cstdio> ...

  8. hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )

    利用 Dancing Link 来解数独 详细的能够看    lrj 的训练指南 和 < Dancing Links 在搜索中的应用 >这篇论文 Dancing Link 来求解数独 , ...

  9. HDU 1426 Sudoku Killer (回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398818 题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位 ...

随机推荐

  1. Hadoop的学习前奏(二)——Hadoop集群的配置

    前言: Hadoop集群的配置即全然分布式Hadoop配置. 笔者的环境: Linux:  CentOS 6.6(Final) x64   JDK:    java version "1.7 ...

  2. SQL经典面试题集锦

    1.问题背景 (1)学生表(学号,姓名,年龄,性别) student(S#,Sname,Sage,Ssex) (2)课程表(课程编号,课程名称,教师编号) course(C#,Cname,T#) (3 ...

  3. LeetCode 141. Linked List Cycle (链表循环)

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  4. Java中根据字节截取字符串

    一.简介 为了统一世界各国的字符集,流行开了Unicode字符集,java也支持Unicode编码,即java中char存的是代码点值,即无论是‘A’还是‘中’都占两个字节. 代码点值:与Unicod ...

  5. Java之正則表達式【使用语法】

    认为好就顶一个!! ! ! 3.正則表達式 用一些特殊的有意义的字符组成的字符串(死记) 原子:正則表達式的最基本组成单位 正則表達式特殊意义的字符:   .  *  +  ?不能单独表示它们,假设非 ...

  6. ubuntu多版本jdk安装及切换

    系统:ubuntu14.04 一.安装openjdk1.7 sudo apt-get install openjdk-7-jre openjdk-7-jdk 安装完成后找到其安装路径: dpkg -L ...

  7. 02-JZ2440裸机学习之GPIO实验【转】

    本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/54910717 版权声明:本文为博主原创文章,转载请注明http://blog.c ...

  8. 使iframe随内容(target到iframe的内容)改变而自适应高度,完美解决各种获取第一个demo高度后第二个高度不变情况

    转自:http://caiceclb.iteye.com/blog/281102 很高兴,终于使用jquery实现了点击外部链接,更改iframe内容时,iframe的高度自适应问题. 失败的测试就不 ...

  9. 【模板】 倍增lca

    虽然很基础,但是还是复习了一下,毕竟比树剖好写... 代码: #include<iostream> #include<cstdio> #include<cmath> ...

  10. Kernel trick----PRML读书笔记

    Many linear parametric models can be re-cast into an equivalent 'dual representstion' in which the p ...