【题目链接】

http://poj.org/problem?id=3074

【算法】

将数独问题转化为精确覆盖问题,用Dancing Links求解

转化方法如下 :

我们知道,在一个数独中 :

1.每个格子填且只填一个数

2.每一行填1-9这九个数

3.每一列填1-9这九个数

4.每个格子填1-9这九个数

对于第一个约束条件,我们用81列,表示是否填入

对于第二个约束条件,我们每一行用9列,表示这一行是否有1-9

第三,四个约束条件的处理方式和第二个类似

【代码】

#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 250000 int i,j,cnt;
int mat[][];
char s[]; struct info
{
int pos,val;
} a[MAXS]; 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+) && s[] != 'e')
{
cnt = ;
memset(mat,,sizeof(mat));
for (i = ; i <= ; i++)
{
if (s[i] != '.')
{
mat[][i] = ;
mat[][+(getRow(i)-)*+s[i]-''] = ;
mat[][+(getCol(i)-)*+s[i]-''] = ;
mat[][+(getGrid(i)-)*+s[i]-''] = ;
} 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[DLX.ans[i]].val;
for (i = ; i <= ; i++) printf("%c",s[i]);
printf("\n");
} return ; }

【POJ 3074】 Sudoku的更多相关文章

  1. 【POJ 3076】 Sudoku

    [题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...

  2. 【POJ 2676】 Sudoku

    [题目链接] http://poj.org/problem?id=2676 [算法] 深度优先搜索 [代码] #include <algorithm> #include <bitse ...

  3. 【POJ - 2676】Sudoku(数独 dfs+回溯)

    -->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...

  4. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  5. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  6. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  7. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  8. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  9. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

随机推荐

  1. 提高mysql千万级大数据SQL查询优化几条经验

    凯哥java                             微信号                             kaigejava 功能介绍                    ...

  2. java 类名.this

    类名为this的限定词. 相对于内部类:有多个this: 1.内部类本身的this: 2.内部类的环境类的this: 类名.this,就是为了对这些this指针的指向做出限定. 区别于类名.class ...

  3. 郁闷的出纳员 题解(Splay)

    题面 看似是要区间修改,然而实际上只需要维护底线和工资的相对大小关系, 瞬间变水 用delta记录对工资的加减,那么添加节点时点权应-delta,输出时+delta 几种操作中减少工资较麻烦: 1.d ...

  4. js的三种对象

    JS中,可以将对象分为“内部对象”.“宿主对象”和“自定义对象”三种. 1,内部对象 js中的内部对象包括Array.Boolean.Date.Function.Global.Math.Number. ...

  5. 常见的Xshell运行命令

    最近接触到了Xshell这个软件,使用这个软件我们来进行连接Linux系统,进去之后我们可能会两眼一抹黑,小编就带大家来学些常见的shell命令. 首先我们要跟大家从最简单的聊起,我们进入Xshell ...

  6. 小程序map地图多点定位

    最近需求有一个类似共享单车查看附近单车的功能,看了看小程序map api对多点定位显示描述的不怎么清晰.显示定位数组添加多个时就不显示了.踩了几个坑写了几个方法.最终弄出来了.有问题建议欢迎留言. h ...

  7. Array.prototype.slice.call()的理解

    最近在看廖雪峰的JS课程,浏览器中的操作DOM的那一章,有这样一道题. JavaScript Swift HTML ANSI C CSS DirectX <!-- HTML结构 --> & ...

  8. H5 应用程序缓存(离线缓存)

    离线缓存这个功能的实现有以下步骤: 1,以nginx做web服务器为例,在mime.types文件中添加一行:text/cache-manifest     manifest,作用是为了让服务器识别该 ...

  9. 8 pandas模块,多层索引

      1 创建多层索引     1)隐式构造         最常见的方法是给DataFrame构造函数的index参数传递两个或更多的数组           · Series也可以创建多层索引    ...

  10. map put

    public class test { static Map<String, Map<String, Integer>> mapB = new HashMap<Strin ...