16×16的数独。

看白书学的DLX,有些细节还有待消化,贴个模板先。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; const int maxn = ;
const int maxnode = + ;
const int maxr = + ;
const int maxc = + ; #define FOR(i,A,s) for(int i = A[s]; i != s; i = A[i]) struct DLX
{
int n, sz;
int S[maxc]; int row[maxnode], col[maxnode];
int L[maxnode], R[maxnode], U[maxnode], D[maxnode]; int ansd, ans[maxr]; void init(int n)
{
this-> n = n; for(int i = ; i <= n; i++) {
U[i] = D[i] = i; L[i] = i - ; R[i] = i + ;
}
L[] = n; R[n] = ; sz = n + ;
memset(S, , sizeof(S));
} void AddRow(int r, vector<int> columns)
{
int first = sz;
for(int i = ; i < columns.size(); i++)
{
int c = columns[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 - ;
} 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[] == ) { ansd = d; return true; } 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 + )) return true;
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;
}
}solver; const int SLOT = ;
const int ROW = ;
const int COL = ;
const int SUB = ; 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 % ;
} char G[maxn][maxn + ]; bool read()
{
for(int i = ; i < ; i++)
if(scanf("%s", G[i]) != ) return false;
return true;
} int main()
{
int kase = ;
while(read())
{
if(kase++ > ) puts(""); solver.init();
for(int r = ; r < ; r++)
for(int c = ; c < ; c++)
for(int v = ; v < ; v++)
if(G[r][c] == '-' || G[r][c] == 'A' + v) {
vector<int> columns;
//一行完成四个任务
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/)*+c/, v));
solver.AddRow(encode(r,c,v), columns);
} vector<int> ans;
solver.solve(ans); for(int i = ; i < ans.size(); i++) {
int r, c, v;
decode(ans[i], r, c, v);
G[r][c] = 'A' + v;
} for(int i = ; i < ; i++) printf("%s\n", G[i]);
} return ;
}

代码君

UVa 1309 DLX Sudoku的更多相关文章

  1. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  2. POJ 3074 Sudoku (DLX)

    Sudoku Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  3. 搜索(DLX): POJ 3074 3076 Sudoku

    POJ 3074 : Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller ...

  4. POJ 3076 Sudoku DLX精确覆盖

    DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 4416   Accepte ...

  5. (中等) HDU 4069 Squiggly Sudoku , DLX+精确覆盖。

    Description Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that ...

  6. (简单) POJ 3076 Sudoku , DLX+精确覆盖。

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  7. (简单) POJ 3074 Sudoku, DLX+精确覆盖。

    Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...

  8. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  9. POJ 3074 Sudoku DLX精确覆盖

    DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: ...

随机推荐

  1. 网络编程之异步IO,rabbitMQ笔记

    对于网络并发编程而言,多线程与多进程算是最常见的需求场景了.毕竟网站开放就是想要更多的流量访问的. 回顾 回顾下之前学过的关于线程,进程和协程的知识点 IO密集型任务--用多线程更好计算密集型任务-- ...

  2. 整理一些最近项目中用到的一些JS方法

    验证是否为图片格式 function IsImgType(src) { var rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ ...

  3. 【翻译转载】【官方教程】Asp.Net MVC4入门指南(6):验证编辑方法和编辑视图

    本节中,您将开始修改为电影控制器所新加的操作方法和视图.然后,您将添加一个自定义的搜索页. 在浏览器地址栏里追加/Movies, 浏览到Movies页面.并进入编辑(Edit)页面. Edit(编辑) ...

  4. (译)Cg Programming/Unity(Cg编程/Unity)

    最近在学习Unity3d中的shader编程,能找到的中文资料比较少,于是,尝试翻译一下wiki Books上的资料,以方便其他跟我一样的入门学习者.由于是第一次翻译技术资料,经验不足,难免出错,请路 ...

  5. 告别CMD.windows终端神器conemu设置

    前言 一种刘姥姥进大观园的感觉,现在是见啥啥新鲜.因为之前不怎么接触到命令操作,平时偶尔用用cmd也没觉得什么不妥.直到现在经常调试脚本,使用git越发感觉不方便.看见同事使用的terminal绚丽夺 ...

  6. 【转】Popclip的JSON格式化扩展

    http://liuyunclouder.github.io/2016/09/29/JSONizer:Popclip的JSON格式化扩展 作为一个MAC党,不好好利用MAC的神兵利器,简直就是罪过.A ...

  7. tmpfs与内存盘

    如何快速的吃掉一段内存:通过tmpfs来划一片领地,再用dd去粗暴占用mount -t tmpfs -o size=512M tmpfs /mnt/demodd if=/dev/zero of=/mn ...

  8. linux必会命令-查询-tail

    先说一个tail使用的例子: tail -n 20 filename 说明:显示filename最后20行. Linux下tail命令的使用方法.linux tail命令用途是依照要求将指定的文件的最 ...

  9. 利用ss5服务搭建代理服务器

    利用ss5服务搭建代理服务器 1. 下载ss5-3.8.9-8.tar.gz ###官网下载http://ss5.sourceforge.net/ 2. 安装ss5 yum -y install gc ...

  10. JS数据结构与算法--单向链表

    链表结构:链表中每个元素由一个存储元素本身的节点和一个指向下一元素的引用组成.如下所示(手画的,比较丑,懒得用工具画了,嘻嘻) 1.append方法,向链表末尾插入一个节点 2.insert(posi ...