【POJ 3076】 Sudoku
【题目链接】
http://poj.org/problem?id=3076
【算法】
将数独问题转化为精确覆盖问题,用Dancing Links求解
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXS 100000 struct info
{
int pos,val;
} a[MAXS]; int i,j,cnt;
int mat[][];
char s[]; inline int getRow(int pos)
{
return (pos - ) / + ;
}
inline int getCol(int pos)
{
return (pos - ) % + ;
}
inline int getGrid(int pos)
{
int x = getRow(pos),y = getCol(pos);
return (x - ) / * + (y - ) / + ;
}
struct DancingLinks
{
int n,m,step,size;
int U[MAXS],D[MAXS],L[MAXS],R[MAXS],Row[MAXS],Col[MAXS];
int H[MAXS],S[MAXS];
int ans[MAXS];
inline void init(int _n,int _m)
{
int i;
n = _n;
m = _m;
for (i = ; i <= m; i++)
{
S[i] = ;
U[i] = D[i] = i;
L[i] = i - ;
R[i] = i + ;
}
L[] = m; R[m] = ;
size = m;
for (i = ; i <= n; i++) H[i] = -;
}
inline void link(int r,int c)
{
size++;
Row[size] = r;
Col[size] = c;
S[c]++;
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size;
if (H[r] < ) L[size] = R[size] = H[r] = size;
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
inline void Remove(int c)
{
int i,j;
R[L[c]] = R[c];
L[R[c]] = L[c];
for (i = D[c]; i != c; i = D[i])
{
for (j = R[i]; j != i; j = R[j])
{
D[U[j]] = D[j];
U[D[j]] = U[j];
S[Col[j]]--;
}
}
}
inline void Resume(int c)
{
int i,j;
for (i = U[c]; i != c; i = U[i])
{
for (j = L[i]; j != i; j = L[j])
{
D[U[j]] = j;
U[D[j]] = j;
S[Col[j]]++;
}
}
L[R[c]] = c;
R[L[c]] = c;
}
inline bool solve(int dep)
{
int i,j,c;
if (R[] == )
{
step = dep;
return true;
}
c = R[];
for (i = R[]; i != ; i = R[i])
{
if (S[i] < S[c])
c = i;
}
Remove(c);
for (i = D[c]; i != c; i = D[i])
{
ans[dep] = Row[i];
for (j = R[i]; j != i; j = R[j])
Remove(Col[j]);
if (solve(dep+)) return true;
for (j = L[i]; j != i; j = L[j])
Resume(Col[j]);
}
Resume(c);
return false;
}
} DLX; int main()
{ while (scanf("%s",s+) != EOF)
{
cnt = ;
memset(mat,,sizeof(mat));
for (i = ; i < ; i++) scanf("%s",s+i*+);
for (i = ; i <= ; i++)
{
if (s[i] != '-')
{
mat[][i] = ;
mat[][+(getRow(i)-)*+s[i]-'A'+] = ;
mat[][+(getCol(i)-)*+s[i]-'A'+] = ;
mat[][+(getGrid(i)-)*+s[i]-'A'+] = ;
} else
{
for (j = ; j <= ; j++)
{
cnt++;
mat[cnt][i] = ;
mat[cnt][+(getRow(i)-)*+j] = ;
mat[cnt][+(getCol(i)-)*+j] = ;
mat[cnt][+(getGrid(i)-)*+j] = ;
a[cnt] = (info){i,j};
}
}
}
DLX.init(cnt,);
for (i = ; i <= cnt; i++)
{
for (j = ; j <= ; j++)
{
if (mat[i][j])
DLX.link(i,j);
}
}
DLX.solve();
for (i = ; i < DLX.step; i++) s[a[DLX.ans[i]].pos] = 'A' + a[DLX.ans[i]].val - ;
for (i = ; i <= ; i++)
{
printf("%c",s[i]);
if (i % == ) printf("\n");
}
printf("\n");
} return ; }
【POJ 3076】 Sudoku的更多相关文章
- 【POJ 3074】 Sudoku
[题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1. ...
- 【POJ 2676】 Sudoku
[题目链接] http://poj.org/problem?id=2676 [算法] 深度优先搜索 [代码] #include <algorithm> #include <bitse ...
- 【POJ - 2676】Sudoku(数独 dfs+回溯)
-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
随机推荐
- html——细线表格
细线: 1.table表格设置背景色 2.table中设置单元格距离 3.tr标签设置另外一种背景色 <!DOCTYPE html> <html> <head lang= ...
- easyui 使用笔记
http://www.easyui.info/archives/1435.html datagrid 服务端分页 服务端分页,高效,快捷!强力推荐! easyui的datagrid服务端分页,通过设置 ...
- 易买网之smartupload实现文件上传
经过俩个星期的奋斗,易买网项目完工.在之前,实现图片的上传,走过许多弯路,原来是好多基础的知识忘记了,没把smartupload文件包添加组件jar包至WEB-INF/lib包中,在此特别重视,做下文 ...
- 打造最强NGINX HTTPS
SSL LABS 检测 完整配置如下 server { listen 443 ssl; server_name xxx.xxxke.com; ssl on; ssl_certificate /xxx/ ...
- HDU_2203_KMP入门
亲和串 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- CAD在网页中如何得到用户自定义事件的参数?
主要用到函数说明: _DMxDrawX::CustomEventParam 得到用户自定义事件的参数. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- Spring装配之——JAVA代码装配Bean
首先创建几个普通的JAVA对象,用于测试JAVA代码装配bean的功能. package soundsystemJava; //作为接口 定义了CD播放器对一盘CD所能进行的操作 public int ...
- iptables详解(2):iptables实际操作之规则查询
所属分类:IPtables Linux基础 在阅读这篇文章之前,请确保你已经阅读了如下文章,如下文章总结了iptables的相关概念,是阅读这篇文章的基础. 图文并茂理解iptables 如果你是一 ...
- String s="a"+"b"+"c"+"d";创建了几个对象?
对于如下代码: package reviewTest; /** * @ClassName: StringTest * @Description: 测试String的字符串相加优化 * @author ...
- 观察者模式之Golang实现
观察者模式的具体概念原理,参见https://baike.baidu.com/item/%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F/5881786?fr ...