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 ...
随机推荐
- deep learning学习记录二
接着我的微博继续八卦吧 微博里问了几个人,关于deep learning和cnn的区别,有不少热心网友给了回答,非常感谢.结合我听课和看文章的理解,我大胆大概总结一下: 在上世纪90年代,neural ...
- python 实现远程上传文件夹
python2 upload.py "ip" "root" "password" "22" "Only Pro ...
- SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot18-scheduling/ 本文出自方志朋的博客 ...
- Struts2 第三讲 -- Struts2的处理流程
4.Struts2的处理流程 以下是struts-defautl.xml中的拦截器 建议通过这个struts-default的副本查看,更形象 它实现了很多的功能,其中包括国际化,文件上传,类型转换, ...
- Webpack学习笔记九 webpack优化总结
webpack 优化笔记 webpack4 自带的优化包括 swingTree(摇摆树)和作用域提升 swingTree 比如入口文件 index.js引入通用方法 util, 里面有 10个方法, ...
- Linux mongodb安装、启动、运行
1.下载 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.4.tgz 2.安装 tar -zxvf ...
- #leetcode刷题之路20-有效的括号
#include <iostream> #include <string> #include <stack> using namespace std; bool i ...
- git 错误 RPC
remote: Enumerating objects: 3772, done. error: RPC failed; curl 18 transfer closed with outstanding ...
- 分别使用原生js和jQuery添加/删除元素的class属性
一.原生js添加/删除元素的class属性: <!-- span元素原有class = "test" --> <span class="test&quo ...
- QQ群排名霸屏技术居然是这样简单
最近做了一些收费的QQ群,收多少钱,一块钱的入门费,也就是说进入我的QQ群必须要1块钱的会费. 我的QQ群主要是干嘛呢,放些电影,比如说市面上电影院,正在播放的,最新最热门的,火爆的一些电影. 先前呢 ...