POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description
 Write a Sudoku playing program that reads data sets from a text file.Input
Output
题目大意:填一个16×16的数独。
思路:套DLX。
代码(POJ 641MS / ZOJ 390MS)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; const int MAXC = + ;
const int MAXR = + ;
const int MAXP = MAXR * + MAXC; struct DLX {
int n, sz;//列数,结点总数
int sum[MAXC];//每列拥有的结点数
int row[MAXP], col[MAXP];//结点所在的行和列
int left[MAXP], right[MAXP], up[MAXP], down[MAXP];//十字链表
int ansd, ans[MAXR]; void init(int nn) {
n = nn;
for(int i = ; i <= n; ++i) {
up[i] = down[i] = i;
left[i] = i - ; right[i] = i + ;
}
right[n] = ; left[] = n;
sz = n + ;
memset(sum, , sizeof(sum));
} void add_row(int r, vector<int> columns) {
int first = sz;
for(int i = , len = columns.size(); i < len; ++i) {
int c = columns[i];
left[sz] = sz - ; right[sz] = sz + ; down[sz] = c; up[sz] = up[c];
down[up[c]] = sz; up[c] = sz;
row[sz] = r; col[sz] = c;
++sum[c]; ++sz;
}
right[sz - ] = first; left[first] = sz - ;
} void remove(int c) {
left[right[c]] = left[c];
right[left[c]] = right[c];
for(int i = down[c]; i != c; i = down[i])
for(int j = right[i]; j != i; j = right[j]) {
up[down[j]] = up[j]; down[up[j]] = down[j]; --sum[col[j]];
}
} void restore(int c) {
for(int i = up[c]; i != c; i = up[i])
for(int j = left[i]; j != i; j = left[j]) {
up[down[j]] = j; down[up[j]] = j; ++sum[col[j]];
}
left[right[c]] = c;
right[left[c]] = c;
} bool dfs(int d) {
if(right[] == ) {
ansd = d;
return true;
}
int c = right[];
for(int i = right[]; i != ; i = right[i]) if(sum[i] < sum[c]) c = i;
remove(c);
for(int i = down[c]; i != c; i = down[i]) {
ans[d] = row[i];
for(int j = right[i]; j != i; j = right[j]) remove(col[j]);
if(dfs(d + )) return true;
for(int j = left[i]; j != i; j = left[j]) restore(col[j]);
}
restore(c);
return false;
} bool solve(vector<int> &v) {
v.clear();
if(!dfs()) return false;
for(int i = ; i < ansd; ++i) v.push_back(ans[i]);
return true;
}
}; DLX solver; 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 + ;
} void decode(int code, int &a, int &b, int &c) {
--code;
c = code % ; code /= ;
b = code % ; code /= ;
a = code;
} char puzzle[][]; bool read() {
for(int i = ; i < ; ++i)
if(scanf("%s", puzzle[i]) == EOF) return false;
return true;
} int main() {
int kase = ;
while(read()) {
if(++kase != ) printf("\n");
solver.init();
for(int r = ; r < ; ++r)
for(int c = ; c < ; ++c)
for(int v = ; v < ; ++v)
if(puzzle[r][c] == '-' || puzzle[r][c] == 'A' + v) {
vector<int> columns;
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, (r/)*+c/, v));
solver.add_row(encode(r, c, v), columns);
}
vector<int> ans;
solver.solve(ans);
for(int i = , len = ans.size(); i < len; ++i) {
int r, c, v;
decode(ans[i], r, c, v);
puzzle[r][c] = 'A' + v;
}
for(int i = ; i < ; ++i) printf("%s\n", puzzle[i]);
}
}
POJ 3076 / ZOJ 3122 Sudoku(DLX)的更多相关文章
- 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 ...
 - 【UVA1309】Sudoku(DLX)
		
点此看题面 大致题意: 让你填完整一个\(16*16\)的数独. 解题思路 我们知道,数独问题显然可以用\(DLX\)解决. 考虑对于一个数独,它要满足的要求为:每个位置都必须有数,每一行都必须有全部 ...
 - POJ 1979 Red and Black (红与黑)
		
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
 - 舞蹈链(DLX)
		
舞蹈链(DLX) Tags:搜索 作业部落 评论地址 一.概述 特别特别感谢这位童鞋His blog 舞蹈链是一种优美的搜索,就像下面这样跳舞- 舞蹈链用于解决精确覆盖或者重复覆盖的问题 你可以想象成 ...
 - POJ 3268 Silver Cow Party (最短路径)
		
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
 - POJ.3087 Shuffle'm Up (模拟)
		
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
 - POJ.1426 Find The Multiple (BFS)
		
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
 - POJ  2676  Sudoku (DFS)
		
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11694 Accepted: 5812 Special ...
 - zoj 2358,poj 1775 Sum of Factorials(数学题)
		
题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...
 
随机推荐
- html基础用法(下)
			
设计表格: <html> <head> <title>表格</title> <meta charset="utf-8" /&g ...
 - mycat常用的分片规则
			
一.枚举法<tableRule name="sharding-by-intfile"> <rule> <columns>user ...
 - Oracle 11g行字段拼接WMSYS.WM_CONCAT问题Not A LOB
			
Oracle 11g行字段拼接WMSYS.WM_CONCAT问题Not A LOB 一.问题出现 项目中的某个查询需要将表中某个字段不重复地拼接起来,百度得到该函数WMSYS.WM_CONCAT(字段 ...
 - linux命令系列-ln(软硬链接)
			
linux命令 ln命令可以生成软链接和硬链接,也可叫做符号链接和实体链接. 有兴趣深入理解的可以查阅相关文档,一般的读者只需记住以下几点即可: .不管是软链接还是硬链接都不会额外增加磁盘空间(虽然实 ...
 - ubuntu系統如何啟動root用戶登陸?
			
之前分享過關於這個問題的文章,現在自己在分享一個關於這個問題的文章給大家.為了學習Linux,一氣之下把win10的換成了ubuntu的系統.安裝就不給大家介紹了(網上很多教程). 在我們安裝好之後, ...
 - doc命令操作数据库(下)
			
1.给数据表添加一组数据: 2.给数据表添加多组数据: 3.对数据进行删除和修改: 4.用select查询单个或多个数据信息: 5.去除重复值: 6.查询的各种用法: between的用法: 查询排序 ...
 - Python基础教程学记(1)
			
引言 Python是什么?——Python是一种面向对象的解释性高级编程语言,具有动态语义.这句话的要点在于,Python是一种知道如何不妨碍你编写程序的编程语言.它让你能够毫无困难地实现所需的功能, ...
 - django开发傻瓜教程-3-celery异步处理
			
Ref: https://www.jianshu.com/p/6f8576a37a3e https://blog.csdn.net/Demo_3/article/details/78119951 ht ...
 - Element-ui学习使用
			
这是我使用Element-ui的布局,排布的一个界面,原本我是使用WinfowsForm来做的一个摄像头注册以及查询的小工具,目前我关注前后端的开发,所以就想着能不能把这么个小工具,我用前后端的形式开 ...
 - java web相对路径和绝对路径总结
			
java web 开发过程中很多地方涉及url路径的问题,比如jsp页面.servlet之间的跳转.其实,可以将url中的/xxx看成一级目录,然后像看待目录层级之间的关系那样去看待url路径.接下来 ...