**链接 : ** 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. 已知目标qps跟并发用户数20,压测平均响应时间实例

    Jmeter性能测试案例(一) 转:https://blog.csdn.net/lovesoo/article/details/78579547 测试需求:测试20个用户访问网站在负载达到30QPS时 ...

  2. MFC:Win32-Dll及MFC-Dll编写调用

    一.win32-dll 1.编写 代码例如以下: Math.h #ifdef MATH_EXPORTS #define MATH_API __declspec(dllexport) #else #de ...

  3. Easyui 页面訪问慢解决方式,GZIP站点压缩加速优化

    1. 静态资源压缩GZIP是站点压缩加速的一种技术,对于开启后能够加快我们站点的打开速度.原理是经过server压缩,client浏览器高速解压的原理,能够大大降低了站点的流量. 详细代码能够參加je ...

  4. MFC基础学习

    RECT rect = { }; //获取窗口的内部客户区矩形 GetClientRect(&rect); 模态和费模态对话框! 模态对话框只需要包含对话框头文件,定义对话框类,调用DoMod ...

  5. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  6. MySQL之自定义函数实例讲解

    转自:https://www.2cto.com/database/201804/740205.html MySQL中已经有很多函数,如时间函数等,但是有时这些函数不能满足自己的设计需求,此时需要自定义 ...

  7. 36.面板Ext.Panel使用

    转自:https://www.cnblogs.com/linjiqin/archive/2011/06/22/2086620.html 面板Ext.Panel使用 概要 1.Ext.Panel概述 2 ...

  8. vue动态绑定class的最常用几种方式

    vue动态绑定class的最常用几种方式:  第一种:(最简单的绑定) 1.绑定单个class html部分: <div :class="{'active':isActive}&quo ...

  9. 云栖社区> > 正文 永久免费SSL安全证书Letsencrypt安装使用方法

    ./letsencrypt-auto certonly --standalone --email admin@thing.com -d thing.com -d www.thing.com

  10. PCB genesis大孔加小孔(即卸力孔)实现方法

    一.为什么 大孔中要加小孔(即卸力孔) 这其实跟钻刀的排屑有关了,当钻刀越大孔,排屑量也越大(当然这也得跟转速,下刀速的参数有关系),通常当钻刀越大,转速越慢,下刀速也越慢(因为要保证它的排屑通畅). ...