**链接 : ** 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. 【转】在Oracle中查看各个表、表空间占用空间的大小

    查看当前用户每个表占用空间的大小:    select segment_name,sum(bytes)/1024/1024 from user_extents group by segment_nam ...

  2. luogu 3383【模板】线性筛素数

    我太菜了 %韩神 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib&g ...

  3. MSP430 PIN 操作寄存器

    1.P口端口寄存器: (1).PxDIR   输入/输出方向寄存器 (0:输入模式    1:输出模式) (2).PxIN    输入寄存器 输入寄存器是只读寄存器,用户不能对其写入,只能通过读取该寄 ...

  4. shell脚本-基础

    shell脚本-基础 编程基础 程序是指令+ 数据 程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心,指令服务于数据 shell 程序提供了编程能力,解释执行. 计算运行二进 ...

  5. Area of Polycubes

    http://poj.org/problem?id=3792 题意:给出n个小正方体的中心坐标,求构成的多重小立方体的表面积.要求输入的下一个小正方体必须与之前的正方体有一个面是相交的.如果不满足条件 ...

  6. [App Store Connect帮助]二、 添加、编辑和删除用户(4)更改用户的 App 访问权限

    您可以限制具有“App 管理”.“客户支持”.“开发者”.“营销”或“销售”职能的用户(均不具有“访问报告”职能)拥有哪些 App 的访问权限.如果您不更改他们的用户 App 访问权限,他们将默认拥有 ...

  7. 根据JSON创建对应的HIVE表

    本文提供一种用SCALA把JSON串转换为HIVE表的方法,由于比较简单,只贴代码,不做解释.有问题可以留言探讨 package com.gabry.hiveimport org.json4s._im ...

  8. RHEL6.5安装成功ORACLE11GR2之后,编写PROC程序出错解决方法

    1.  proc: error while loading shared libraries: libclntsh.so.11.1: cannot open shared object file: N ...

  9. SharePoint通过IP地址访问

    问题:SP站点通过计算机名称可以访问,但不能通过IP地址访问 解决方案:打开SharePoint2010管理中心>应用程序管理>配置备用访问映射>编辑公用 URL 备用访问映射集:选 ...

  10. java环境搭建(及安装问题“No repository found containing”解决) 并创立第一个java程序

    环境: java8 及 Eclipse java8 配置:http://jingyan.baidu.com/article/e2284b2b5967e7e2e7118d74.html Eclipse ...