[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掉若干人. 问最少选多少个角色( ...
随机推荐
- 全平台阅读器 StartReader
前段时间在网上闲逛, 发现了一款全平台阅读器 StartReader, 用了一阵子感觉还不错,网址是: https://www.startreader.com/ 感觉这款阅读器是程序员的福音,it人员 ...
- 文本文件的合并操作方法 - Python
我们有时候,看到几k的日志文件,一大堆,一个一个打开又很麻烦,少看几个,又担心遗漏,这个时候,如果有一个可以合并所有文本文件的工具就好了. 下面这个代码就可以实现,它不局限于.txt格式,基本上字符型 ...
- [PHP][thinkphp5] 学习二:路由、配置调用、常量定义调用
路由: 其实TP5就是一个集多家框架所长而成的,感觉失去了自己的特色!路由这块呢类似于laravel框架!废话不说直接上码! 路由配置,类似laravel,就在route.php文件里配置路由(文件所 ...
- cli命令速查
在文件的指定行(n)插入指定内容: sed -i "niecho "haha"" a 执行后,在a文件的第n行插入echo "haha" 多 ...
- Python 开发工具链全解
可能刚开始学习Python时,有人跟你说可以将源文件所在的文件夹添加到 PYTHONPATH环境变量中,然后可以从其他位置导入此代码.在大多数情况下,这个人常常忘记补充这是一个非常糟糕的主意.有些人在 ...
- 【MyBatis深入剖析】应用分析与最佳实践(下)
MyBatis编程式开发 MyBatis编程式开发步骤 MyBatis和MySQL Jar包依赖 全局配置文件mybatis-config.xml 映射器Mapper.xml Mapper接口 编程式 ...
- 米特运输——(dfs)
米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储 存一直是一个大问题.D星上有N个城市,我们将其顺序编号为1到N,1号城市为首都.这N个城市由N- ...
- 如何给 Inno Setup 生成的安装包添加版本信息
使用 Inno 已有的函数 GetFileVersion 获取 EXE 文件的版本 #define ApplicationName 'Application Name' #define Applica ...
- RabbitMQ Hello world(二)
简介: Rabbitmq 是消息代理中间件,它接收或者发送消息.你可以把它想想宬一个邮局:当你把邮件放到邮箱时,你可以确定某一位邮递员可以准确的把邮件送到收件人手中,在这个比喻中,rabbitmq是一 ...
- 谷歌提高Google Assistant中Voice Match的准确性
谷歌正在提高 Google Assistant 中 Voice Match 的准确性,使其变得更加完善.谷歌表示一旦用户在 Google Assistant 中启用 Voice Match 功能,那么 ...