HUD 1426 Sudoku Killer (DFS)
**链接 : ** 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)的更多相关文章
- hdu 1426 Sudoku Killer (dfs)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1426 Sudoku Killer(dfs 解数独)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1426 Sudoku Killer【DFS 数独】
自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...
- hdu 1426 Sudoku Killer
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 #include<stdio.h> #include<math.h> #in ...
- HDOJ Sudoku Killer(dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 思路分析:该问题为数独问题,明显解是唯一的,所有采用dfs搜索效果更好: 在搜索时,可以通过3个 ...
- HDU 1426 Sudoku Killer(搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 题意很明确,让你解一个9*9的数独. DFS即可. #include <cstdio> ...
- hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )
利用 Dancing Link 来解数独 详细的能够看 lrj 的训练指南 和 < Dancing Links 在搜索中的应用 >这篇论文 Dancing Link 来求解数独 , ...
- HDU 1426 Sudoku Killer (回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398818 题意: 给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位 ...
随机推荐
- vmware里面的linux怎么和windows相互传文件
我们常常遇到这样的问题.高版本号的vmware遇到低版本号的linux.使用起来就比較抠脚,比方低版本号的linux安装在高版本号的vmware里. 1.不能全屏显示虚拟机 2.每次切换出来.总要按一 ...
- iOS UITextView 高度随文字自己主动添加,并尾随键盘移动(二)
上节地址:http://blog.csdn.net/lwjok2007/article/details/47401293 接着上节我们来实现 输入框自己主动调节高度 首先,我们得知道,要推断是否该换行 ...
- 进程同步与相互排斥:POSIX有名信号量
在 POSIX 标准中,信号量分两种,一种是无名信号量,一种是有名信号量. 无名信号量一般用于线程间同步或相互排斥,而有名信号量一般用于进程间同步或相互排斥. 它们的差别和管道及命名管道的差别类似.无 ...
- 【C++】双向线性链表容器的实现
// 双向线性链表容器 #include <cstring> #include <iostream> #include <stdexcept> using name ...
- 【bzoj1002】 [FJOI2007]轮状病毒DP
递推+环状特殊处理+高精度 #include<algorithm> #include<iostream> #include<cstdlib> #include& ...
- kafka02
- java错误:无法将Object转换为int类型
Object value java要想将Object转换为int类型,就必须先将Object转换为String,然后String再转换为int,如下: Integer.parseInt(String. ...
- The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
EventLog.SourceExists https://stackoverflow.com/questions/9564420/the-source-was-not-found-but-some- ...
- YTU 2705:用重载求距离
2705: 用重载求距离. 时间限制: 1 Sec 内存限制: 128 MB 提交: 208 解决: 114 题目描述 使用函数重载的方法定义两个重名函数,分别求出整型数的两点间距离和浮点型数的两 ...
- Spark 操作Hive 流程
1.ubuntu 装mysql 2.进入mysql: 3.mysql>create database hive (这个将来是存 你在Hive中建的数据库以及表的信息的(也就是元数据))mysql ...