POJ 3076
DLX算法,刚接触,是关于精确覆盖的,白书上有算法介绍。
代码模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std; char puzzle[20][20];
const int SLOT=0;
const int ROW=1;
const int COL=2;
const int SUB=3;
const int maxn=1300;
const int maxnode=256*16*4;
const int maxr=256*16*4; struct DLX
{
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 = 0 ; i <= n; i++ )
{
U[i] = i ;
D[i] = i ;
L[i] = i - 1;
R[i] = i + 1;
}
R[n] = 0 ;
L[0] = n;
sz = n + 1 ;
memset(S,0,sizeof(S));
}
void addRow(int r,vector<int> c1)
{
int first = sz;
for(int i = 0 ; i < c1.size(); i++ ){
int c = c1[i];
L[sz] = sz - 1 ; R[sz] = sz + 1 ; 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 - 1] = first ; L[first] = sz - 1;
}
// 顺着链表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;
}
bool dfs(int d){
if(R[0] == 0 ){
ansd = d;
return true;
}
// 找S最小的列c
int c = R[0] ;
FOR(i,R,0) 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;
FOR(j,L,i) restore(col[j]);
}
restore(c); return false;
}
bool solve(vector<int> & v){
v.clear();
if(!dfs(0)) return false;
for(int i = 0 ; i< ansd ;i ++ ) v.push_back(ans[i]);
return true;
}
};
DLX slover; int encode(int a,int b,int c){
return a*256+b*16+c+1;
} void decode(int code,int &a,int &b,int &c){
code--;
c=code%16; code/=16;
b=code%16; code/=16;
a=code;
} bool read(){
for(int i=0;i<16;i++){
if(scanf("%s",puzzle[i])!=1) return false;
}
return true;
} int main(){
int kase=0;
while(read()){
if(++kase!=1) printf("\n");
slover.init(1024);
for(int r=0;r<16;r++){
for(int c=0;c<16;c++){
for(int v=0;v<16;v++){
if(puzzle[r][c]=='-'||puzzle[r][c]=='A'+v){
vector<int> columns;
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,(r/4)*4+c/4,v));
slover.addRow(encode(r,c,v),columns);
}
}
}
}
vector<int>ans;
// cout<<"YES"<<endl;
ans.clear();
slover.solve(ans);
// cout<<"YES"<<endl;
for(int i=0;i<ans.size();i++){
int r,c,v;
decode(ans[i],r,c,v);
puzzle[r][c]='A'+v;
}
for(int i=0;i<16;i++)
printf("%s\n",puzzle[i]);
}
return 0;
}
POJ 3076的更多相关文章
- (简单) POJ 3076 Sudoku , DLX+精确覆盖。
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- 【POJ 3076】 Sudoku
[题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...
- POJ 3076 Sudoku DLX精确覆盖
DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 4416 Accepte ...
- POJ 3076 Sudoku
3076 思路: dfs + 剪枝 首先,如果这个位置只能填一种字母,那就直接填 其次,如果对于每一种字母,如果某一列或者某一行或者某一块只能填它,那就填它 然后,对于某个位置如果不能填字母了,或者某 ...
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- POJ 3076 Sudoku (dancing links)
题目大意: 16*16的数独. 思路分析: 多说无益. 想说的就是dancing links 的行是依照 第一行第一列填 1 第一行第二列填 2 -- 第一行第十五列填15 第一行第二列填 1 -- ...
- Sudoku POJ - 3076
Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 5769 Accepted: 2684 Descripti ...
- Sudoku POJ - 3076 (dfs+剪枝)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- POJ 3076 SUKODU [Dangcing Links DLX精准覆盖]
和3074仅仅有数目的不同,3074是9×9.本来想直接用3074的.然后MLE,,,就差那么20M的空间,,. 从这里学习到了解法: http://www.cnblogs.com/ylfdrib/a ...
随机推荐
- awk查找特定字段
在一行中,查找字段包含exe的: ###########awk.awk######## { for(i=1;i<NF;i++) { if($i ~ /exe/) { print $i } } } ...
- HTTP权威协议笔记-9.Web机器人
经过整个春节的放肆,终于回归了,说实话,春节真心比上班累. 9.1 爬虫及爬行方式 (1) 爬虫:Web爬虫是一种机器人,他们会递归性的对各种信息Web站点进行遍历. (2) 爬行方式:Web机器人会 ...
- Moco模拟服务器实现请求&响应 (一)
接口测试Moco工具 1.使用Moco模拟,首先需要下载Moco 的jar 包,下载链接: http://central.maven.org/maven2/com/github/dreamhead/m ...
- Docker 探索安装WordPress+Mysql8.0
拉取MYSQL,注意默认是8.0版本,连接加密方式有变化 docker pull mysql 运行MYSQL docker run --name wordpress-mysql -p 3306:330 ...
- 缓存,队列(Redis,RabbitMQ)
Redis Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- 前端面试基础-html篇之H5新特性
h5的新特性(目前个人所了解)如下 语义化标签 表单新特性 视频(video)和音频(audio) canvas画布 svg绘图 地理定位 为鼠标提供的拖放API webworker (重点)Stor ...
- Unity引擎GUI之Image
UGUI的Image等价于NGUI的Sprite组件,用于显示图片. 一.Image组件: Source Image(图像源):纹理格式为Sprite(2D and UI)的图片资源(导入图片后选择T ...
- USB 接口探测分类
USB 接口探测分类 SDP (Standand Downstream Port) 标准下行接口 标准USB都支持的接口 这种端口的D+和D-线上具有15kΩ下拉电阻.限流值如上讨论:挂起时为2.5m ...
- DeltaFish 校园物资共享平台 第六次小组会议
DeltaFish 校园物资共享平台 第六次小组会议 记录人:娄雨禛 2018.6.3 任务进度(2018.5.28-2018.6.3) 前端 李鑫:商品详情界面设计.总体配色分析 刘鼎乾:卖家页面初 ...
- js点击事件 注册下一步实现代码
点击事件: <body> <input type="button" id="btn1"/> <input type="b ...