[csu1605]数独(精确覆盖问题)
题意 :给定数独的某些初始值,规定每个格子的得分,求得分最大的数独的解。
思路:这是某年的noip的原题,高中时就写过,位运算也就是那个时候学会的--。这题明显是暴搜,但是需要注意两点,一是需要加一些常数优化,也就是位运算,一个是剪枝,填完某个数后发现某个格子无解了则换个数填,并且那些可填的数的种数少的格子尽量先填,因为这样尽可能让矛盾在靠近根的地方出现。今天粗略学了一下舞蹈链--DLX,这个算法(准确来说是一个结构)可以比较高效的解决一些精确覆盖问题,对于重复覆盖问题稍作修改也适用。用DLX写了一遍数独,发现效率比位运算略高一点,但不明显。
位运算:
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 3e4 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } int ans, a[][], f[ << ], row[], col[], block[], sp[ << ]; int getScore(int i, int j) {
return min(min(i, - i), min(j, - j)) + ;
} void init() {
rep_up0(i, ) {
f[ << i] = i;
}
} void dfs(int k, int score) {
if (k >= ) {
max_update(ans, score);
return ;
}
int x, y, c = ;
rep_up0(i, ) {
bool ok = false;
rep_up0(j, ) {
if (a[i][j]) continue;
int tmp = row[i] | col[j] | block[make_id(i / , j / , )];
int tot = 0x3fe ^ tmp;
int cnt = ;
if (tot == ) {
ok = true;
c = ;
break;
}
cnt = sp[tot];
if (cnt < c) {
x = i;
y = j;
c = cnt;
}
}
if (ok) break;
}
if (c == || c == ) return ;
int i = x, j = y;
int tmp = row[i] | col[j] | block[make_id(i / , j / , )];
int tot = 0x3fe ^ tmp;
while (tot) {
tmp = lowbit(tot);
row[i] ^= << f[tmp];
col[j] ^= << f[tmp];
block[make_id(i / , j / , )] ^= << f[tmp];
a[i][j] = f[tmp];
dfs(k + , score + f[tmp] * getScore(i, j));
row[i] ^= << f[tmp];
col[j] ^= << f[tmp];
block[make_id(i / , j / , )] ^= << f[tmp];
a[i][j] = ;
tot -= tmp;
}
} int main() {
//freopen("in.txt", "r", stdin);
sp[] = ;
rep_up1(i, << ) {
sp[i] = sp[i - lowbit(i)] + ;
}
int T;
init();
cin >> T;
while (T --) {
int sum = , cnt = , ok = true;
mem0(col);
mem0(row);
mem0(block);
rep_up0(i, ) {
rep_up0(j, ) {
sint(a[i][j]);
sum += a[i][j] * getScore(i, j);
if (a[i][j]) {
cnt ++;
if (col[j] & ( << a[i][j])) ok = false;
if (row[i] & ( << a[i][j])) ok = false;
if (block[make_id(i / , j / , )] & ( << a[i][j])) ok = false;
col[j] |= << a[i][j];
row[i] |= << a[i][j];
block[make_id(i / , j / , )] |= << a[i][j];
}
}
}
ans = -;
if (ok) dfs(cnt, sum);
cout << ans << endl;
}
}
DLX(模板):
#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e5 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } ///行编号从1开始,列编号1~n,结点0是表头结点,结点1~n是各列顶部的虚拟结点
int result;
int b[][]; 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;
} struct DLX
{
const static int maxn = ;
const static int maxnode = ;
int n , sz; // 行数,节点总数
int S[maxn]; // 各列节点总数
int row[maxnode],col[maxnode]; // 各节点行列编号
int L[maxnode],R[maxnode],U[maxnode],D[maxnode]; // 十字链表 int ansd,ans[maxn]; // 解 void init(int 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> c1)
{
int first = sz;
for(int i = ; i < c1.size(); i++ ){
int c = c1[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) {
L[R[c]] = L[c];
R[L[c]] = R[c];
FOR(i,D,c)
FOR(j,R,i) {U[D[j]] = U[j];D[U[j]] = D[j];--S[col[j]];}
}
void restore(int c) {
FOR(i,U,c)
FOR(j,L,i) {++S[col[j]];U[D[j]] = j;D[U[j]] = j; }
L[R[c]] = c;
R[L[c]] = c;
}
void update() {
int score = ;
rep_up0(i, ansd) {
int r, c, v;
decode(ans[i], r, c, v);
score += (v + ) * b[r][c];
}
max_update(result, score);
}
bool dfs(int d) {
if(R[] == ) {
ansd = d;
update();
return true;
}
// 找S最小的列c
int c = R[];
FOR(i,R,) if(S[i] < S[c]) c = i; remove(c);
FOR(i,D,c) {
ans[d] = row[i];
FOR(j,R,i) remove(col[j]);
//if(dfs(d + 1)) return true;
dfs(d + );
FOR(j,L,i) 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;
int a[][]; int main() {
//freopen("in.txt", "r", stdin);
rep_up0(i, ) {
rep_up0(j, ) {
b[i][j] = + min(min(i, - i), min(j, - j));
}
}
int T, x;
cin >> T;
while (T --) {
solver.init();
rep_up0(i, ) {
rep_up0(j, ) {
int x;
sint(x);
rep_up0(k, ) {
if (x == || x == k + ) {
vector<int> col;
col.push_back(encode(, i, j));
col.push_back(encode(, i, k));
col.push_back(encode(, j, k));
col.push_back(encode(, make_id(i / , j / , ), k));
solver.addRow(encode(i, j, k), col);
}
}
}
}
result = -;
solver.dfs();
cout << result << endl;
}
return ;
}
[csu1605]数独(精确覆盖问题)的更多相关文章
- hihoCoder #1321 : 搜索五•数独 (Dancing Links ,精确覆盖)
hiho一下第102周的题目. 原题地址:http://hihocoder.com/problemset/problem/1321 题意:输入一个9*9数独矩阵,0表示没填的空位,输出这个数独的答案. ...
- HDU 3111 Sudoku(精确覆盖)
数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... #include <cs ...
- DLX 舞蹈链 精确覆盖 与 重复覆盖
精确覆盖问题:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 还有重复覆盖问题 dancing links 是 一种数据结构,用来优化搜索,不算是一种算法.(双向 ...
- hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )
利用 Dancing Link 来解数独 详细的能够看 lrj 的训练指南 和 < Dancing Links 在搜索中的应用 >这篇论文 Dancing Link 来求解数独 , ...
- (简单) POJ 3074 Sudoku, DLX+精确覆盖。
Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...
- poj3074 DLX精确覆盖
题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...
- HDU 3111 Sudoku ( Dancing Links 精确覆盖模型 )
推荐两篇学DLX的博文: http://bbs.9ria.com/thread-130295-1-1.html(这篇对DLX的工作过程演示的很详细) http://yzmduncan.iteye.co ...
- 跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题
精确覆盖问题的定义:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 例如:如下的矩阵 就包含了这样一个集合(第1.4.5行) 如何利用给定的矩阵求出相应的行的集合 ...
- HDU 3957 Street Fighter(搜索、DLX、重复覆盖+精确覆盖)
很久以前就看到的一个经典题,一直没做,今天拿来练手.街霸 给n<=25个角色,每个角色有 1 or 2 个版本(可以理解为普通版以及爆发版),每个角色版本可以KO掉若干人. 问最少选多少个角色( ...
随机推荐
- sqli-labs通关教程----31~40关
第三十一关 这关一样,闭合变成(",简单测试,#号不能用 ?id=1") and ("1")=("1")--+ 第三十二关 这关会把我们的输 ...
- Flutter Weekly Issue 52
教程 一个易迁移.兼容性高的 Flutter 富文本方案 复杂业务如何保证Flutter的高性能高流畅度? 插件 flutter_color_models A wrapper for the Dart ...
- Git把本地代码推送到远程github仓库
运用Git版本控制系统进行代码的管理,以便于团队成员的协作,由于之前是使用svn来进行版本控制,所以对于Git使用还有待熟练掌握.Git与svn类似,个人认为两者之间比较直观的区别就是 Git 不需要 ...
- linux 文件的查找和压缩
1.使用 locate 命令 需要安装:yum install mlocate -y 创建或更新 slocate/locate 命令所必需的数据库文件:updatedb 作用:搜索不经常改变的文件如配 ...
- python3 xlwt,csv学习
前言 对于抓取一些站点分析然后指纹识别的时候可能用到到它.所以学习下.这里就记录一些最基本的感觉有用的. xlwt 基本创建 demo: #coding=utf- import xlwt yunyin ...
- 基于TextRank算法的文本摘要
本文介绍TextRank算法及其在多篇单领域文本数据中抽取句子组成摘要中的应用. TextRank 算法是一种用于文本的基于图的排序算法,通过把文本分割成若干组成单元(句子),构建节点连接图,用句子之 ...
- web自动化中pytest框架的使用(二)---参数化
1.pytest--参数化 在测试用例的前面加上@pytest.mark.parametrize("参数名",列表数据) 参数名:用来接收每一项数据,并作为测试用例的参数 列表数据 ...
- php的一个有意思的命令:-S
php -S localhost:8188 /web 会启动一个监控IP:PORT 的http服务,算是简易的web服务器吧.基本上,实现了PHP+MySQL就可以建立一个简易测试网站的环境.
- 2019-2020-1 20199329《Linux内核原理与分析》第七周作业
<Linux内核原理与分析>第七周作业 一.本周内容概述: 对Linux系统如何创建一个新进程进行追踪 分析Linux内核创建一个新进程的过程 二.本周学习内容: 1.学习进程的描述 操作 ...
- REDHAT7进入单用户模式
Redhat7采用的是grub2,和Redhat6.x进入单用户的方法不同. 一.init方法 1.centos7的grub2界面会有两个入口,正常系统入口和救援模式: 2.修改grub2引导 在正常 ...