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. Spring事务的5种隔离级别

    概述:isolation设定事务的隔离级别,事务管理器根据它来控制另外一个事务可以看到本事务内的哪些数据. 定义的5个不同的事务隔离级别: DEFAULT:默认的隔离级别,使用数据库默认的事务隔离级别 ...

  2. Collection-Iterator-foreach

    一.Collection(java.util) 1.概述:具有相同性质的一类事物的汇聚的整体,称为集合.任何集合都包含三块内容:对外的接口/接口的实现/对集合运算的算法.         java中使 ...

  3. Golang的开发环境配置之SlickEdit篇

    Golang的开发环境通常有如下三种: 1. vi, emacs, notepad++ 2. Sublime Text 2/3 3. LiteIDE 不过,今天我想体验一下在slickedit当中使用 ...

  4. cpp 计算程序运行时间的两种方法

    1. #include <time.h> time_t begin_t = clock(); // to do time_t finish_t = clock(); cout<< ...

  5. 洛谷 P2663 越越的组队

    题目描述 班级要组织一场综合能力竞赛,全班同学(N个,N是偶数)分成两队互相竞争.老师找到了越越并给了越越一张全班同学综合能力测试的成绩,要求他从全班同学中选出一半(他自己也可能被选),并要求这些同学 ...

  6. (五)SpringMVC之使用Kaptcha实现验证码功能

    一.什么是Kaptcha Kaptcha是Google开发的用于自动生成验证码的插件. 二.怎么导入Kaptcha ① 如果没有用Maven管理工具的话就直接导入包(可以直接下载:pau8) http ...

  7. BZOJ 2725: [Violet 6]故乡的梦 最短路+线段树

    2725: [Violet 6]故乡的梦 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 678  Solved: 204[Submit][Status ...

  8. SAP CRM Survey调查问卷的存储模型

    数据库表CRM_SVY_DB_SVS,通过如下的函数CRM_SVY_DB_SVS_CREATE插入: 可以通过指定的创建者和创建时间很容易查找到特定的Survey: 调查问卷的答案明细以XML的格式存 ...

  9. SAP Cloud for Customer Price-计价简介

    SAP Cloud for Customer(本文以下简称C4C)作为SAP新一代的CRM云产品,其Price功能实现虽不如以前的SAP ERP那么复杂,但是也能满足企业运作中各种Price需求. C ...

  10. Android(java)学习笔记107:Relativelayout相对布局

    1. Relativelayout相对布局案例: 我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8" ...