HDU 4069 数独
好久没做题了,建图搞了好久…… 然后,判是否有多解的时候会把原来的答案覆盖掉…… 这里没注意,弄了一下午……
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = **+;
const int MAXR = **+;
const int MAXNODE = MAXN*MAXR+; struct DLX {
// 行编号从1开始,列编号为1~n,结点0是表头结点;结点1~n是各列顶部的虚拟结点
int n, sz; // 列数,结点总数
int S[MAXN]; //各列结点数 int row[MAXNODE], col[MAXNODE]; //各结点行列编号
int L[MAXNODE], R[MAXNODE], U[MAXNODE], D[MAXNODE]; //十字链表 int ansd, ans[MAXR]; // 解 void init(int n) { //n是列数
this->n = n; //虚拟结点
for (int i = ; i <= n; i++) {
U[i] = i; D[i] = i; L[i] = i-; R[i] = i+;
}
R[n] = ; L[] = n;
sz = n+;
memset(S, , sizeof(S));
} void addRow(int r, vector<int> columns) {
//这一行的第一个结点
//行没有设头结点,每一行连成一个环形
int first = sz;
for (int i = ; i < columns.size(); i++) {
int c = columns[i];
L[sz] = sz-; R[sz] = sz+; D[sz] = c; U[sz] = U[c];
D[U[c]] = sz; U[c] = sz;
row[sz] = r; col[sz] = c;
S[c]++; sz++;
}
R[sz-] = first; L[first] = sz-;
} //顺着链表A,遍历s外的其他元素
#define FOR(i, A, s) for (int i = A[s]; i != s; i = A[i]) void remove(int c) { //删除c列
L[R[c]] = L[c]; R[L[c]] = R[c];
for (int i = D[c]; i != c; i = D[i]) // 对于每一个c列不为0的所有行
for (int j = R[i]; j != i; j = R[j]) { //删除这一整行
U[D[j]] = U[j]; D[U[j]] = D[j]; S[col[j]]--;
}
} void restore(int c) { //回连c列
for (int i = U[c]; i != c; i = U[i])
for (int j = L[i]; j != i; j = L[j]) {
U[D[j]] = j; D[U[j]] = j; S[col[j]]++;
}
L[R[c]] = c; R[L[c]] = c;
} int flag; void dfs(int d) { //d为递归深度
if (R[] == ) { //找到解
ansd = d; //记录解的长度
flag++;
return ;
} //找S最小的C列
int c = R[]; //第一个未删除的列
for (int i = R[]; i != ; i = R[i]) if (S[i]<S[c]) c = i;
remove(c); //删除第c列
for (int i = D[c]; i != c; i = D[i]) { //用结点i所在的行覆盖第c列
if (!flag) ans[d] = row[i];
for (int j = R[i]; j != i; j = R[j]) remove(col[j]); //删除节结点i所在行覆盖第c列
dfs(d+);
if (flag>) return ;
for (int j = L[i]; j != i; j = L[j]) restore(col[j]); // 恢复
}
restore(c); //恢复
} int solve(vector<int> &v) {
v.clear();
flag = ;
dfs();
for (int i = ; i < ansd; i++) v.push_back(ans[i]);
return flag;
}
}; const int dir[][] = { {-, }, {, }, {, }, {, -} }; const int SLOT = ;
const int ROW = ;
const int COL = ;
const int SUB = ; inline int encode(int a, int b, int c) { return a*+b*+c+; }
inline void decode(int code, int &a, int &b, int &c) {
code--;
c = code%; code /= ;
b = code%; code /= ;
a = code;
} DLX solver;
int Matrix[][];
int belong[][];
int cnt; vector<int> columns; void test() { int *p = ; (*p) = ; } void dfs(int r, int c, int num) {
belong[r][c] = cnt;
int t = Matrix[r][c]>>;
int nr, nc;
for (int i = ; i < ; i++) if (!(t&(<<i))) {
nr = r+dir[i][]; nc = c+dir[i][];
if (!(<=nr&&nr<)||!(<=nc&&nc<)||belong[nr][nc]!=-) continue;
dfs(nr, nc, num+);
}
} void solve() {
solver.init(**);
for (int r = ; r < ; r++) {
for (int c = ; c < ; c++) {
Matrix[r][c] %= ;
for (int v = ; v <= ; v++) {
if (Matrix[r][c]!= && Matrix[r][c]!=v) continue;
columns.clear();
columns.push_back(encode(SLOT, r, c));
columns.push_back(encode(ROW, r, v-));
columns.push_back(encode(COL, c, v-));
columns.push_back(encode(SUB, belong[r][c], v-));
solver.addRow(encode(r, c, v-), columns);
}
}
} int ans = solver.solve(columns);
if (ans == ) { puts("Multiple Solutions"); return; }
if (ans == ) { puts("No solution"); return; }
for (int i = ; i < columns.size(); i++) {
int r, c, v;
decode(columns[i], r, c, v);
Matrix[r][c] = v+;
}
for (int r = ; r < ; r++) {
for (int c = ; c < ; c++) {
printf("%d", Matrix[r][c]);
}
puts("");
}
} int main() {
#ifdef Phantom01
freopen("HDU4069.txt", "r", stdin);
// freopen("HDU4069.out", "w", stdout);
#endif //Phantom01 int T;
scanf("%d", &T);
for (int C = ; C <= T; C++) {
printf("Case %d:\n", C);
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
scanf("%d", &Matrix[i][j]); for (int r = ; r < ; r++)
for (int c = ; c < ; c++)
belong[r][c] = -; cnt = ;
for (int r = ; r < ; r++) {
for (int c = ; c < ; c++) if (belong[r][c]==-){
dfs(r, c, );
cnt++;
}
}
solve();
} return ;
}
HDU 4069 数独的更多相关文章
- hdu 4069 垃圾数独
首先dfs给每个格子分一个大的区块 其次套板子就a 我一开始直接在选取行的时候填数独,发现超时 我这一行也就4个元素,找到 x <= 81 的列计算元素位置,81 < x <= 16 ...
- HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...
- hdu 4069 福州赛区网络赛I DLC ***
再遇到一个DLC就刷个专题 #include <stdio.h> #include <string.h> #include <iostream> #include ...
- (中等) HDU 4069 Squiggly Sudoku , DLX+精确覆盖。
Description Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that ...
- HDU 1426(数独 DFS)
题意是完成数独. 记录全图,将待填位置处填 0,记录下所有的待填位置,初始化结束.在每个待填位置处尝试填入 1 - 9,若经过判断后该位置可以填入某数字,则继续向下填下一个位置, 回溯时把待填位置重新 ...
- HDU - 5547 数独(回溯法)
题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547 正所谓:骗分过样例,暴力出奇迹. 解题思想(暴力出奇迹(DFS+回溯)): 1 ...
- [DLX+bfs] hdu 4069 Squiggly Sudoku
题意: 给你9*9的矩阵.对于每一个数字.能减16代表上面有墙,能减32代表以下有墙. .. 最后剩下的数字是0代表这个位置数要求,不是0代表这个数已知了. 然后通过墙会被数字分成9块. 然后做数独, ...
- hdu 3909 数独扩展
思路:做法与9*9的一样.只不过是变量. #include<set> #include<map> #include<cmath> #include<queue ...
- Dancing Links [Kuangbin带你飞] 模版及题解
学习资料: http://www.cnblogs.com/grenet/p/3145800.html http://blog.csdn.net/mu399/article/details/762786 ...
随机推荐
- centos7安装anaconda之后报错:rpm: /home/wyl/anaconda3/lib/liblzma.so.5: version `XZ_5.1.2alpha' not found (required by /lib64/librpmio.so.3)
1.报错 参考:https://stackoverflow.com/questions/47633870/rpm-lib64-liblzma-so-5-version-xz-5-1-2alpha-no ...
- 算法21----重塑矩阵 LeetCode566
1.题目 在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重 ...
- C++递归方法实现全排列
#include<iostream> using namespace std; void perm(int list[],int k,int m);//声明 void perm(int l ...
- [环境搭建] VS-Visual Studio-IIS Express 支持局域网訪问
原创作品,转载请注明出处:http://blog.csdn.net/qiujuer/article/details/40350385 使用Visual Studio开发Web网页的时候有这种情况:想要 ...
- 用MyEclipse 打包JAR文件
用MyEclipse 将自己定义标签打成JAR包 1.新建一个javaproject 2.将标签有关的java代码拷贝到新建javaproject的一个包中,这时会报错 ...
- hello world to php( mac 配置 xmapp virtual host)
一.安装xmapp.安装完以后查看,服务是否都能启动(数据库和server) 二.配置自己的virtualhost 1.系统host文件加入server的域名(在浏览器中输入域名后会先通过 ...
- 接口測试-HAR
參考文章 雪球的 HttpApi 接口測试框架设计 HAR(HTTP Archive)规范 神器--Chrome开发人员工具(一) HAR是什么 一句话:关于HTTP所有的信息的一种文件保存格式 HA ...
- mybatis和hibernate的区别【转】
第一章 Hibernate与MyBatisHibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀的 ...
- HDU5411CRB and Puzzle(矩阵高速幂)
题目链接:传送门 题意: 一个图有n个顶点.已知邻接矩阵.问点能够反复用长度小于m的路径有多少. 分析: 首先我们知道了邻接矩阵A.那么A^k代表的就是长度为k的路径有多少个. 那么结果就是A^0+A ...
- Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)
题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...