DLX第一题,模板留念。

 /* 3076 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct DLX {
static const int maxc = ** + ;
static const int maxr = ** + ;
static const int maxnode = *** + ;
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_) {
n = n_; for (int i=; i<=n; ++i) {
L[i] = i - ;
R[i] = i + ;
U[i] = i;
D[i] = i;
}
L[] = n;
R[n] = ; sz = n + ;
memset(S, , sizeof(S));
} void addRow(int r, vi columns) {
int first = sz;
int size = SZ(columns); for (int i=; i<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 (int i=D[c]; i!=c; i=D[i]) {
for (int j=R[i]; j!=i; j=R[j]) {
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[col[j]];
}
}
} void restore(int c) {
for (int i=U[c]; i!=c; i=U[i]) {
for (int j=L[i]; j!=i; j=L[j]) {
++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 (int i=R[]; i!=; i=R[i]) {
if (S[i] < S[c])
c = i;
} remove(c);
for (int i=D[c]; i!=c; i=D[i]) {
ans[d] = row[i];
for (int j=R[i]; j!=i; j=R[j]) {
remove(col[j]);
}
if (dfs(d+)) return true;
for (int j=L[i]; j!=i; j=L[j]) {
restore(col[j]);
}
}
restore(c); return false;
} bool solve(vector<int>& v) {
v.clr(); if (!dfs()) return false; for (int i=; i<ansd; ++i)
v.pb(ans[i]); return true;
}
} DLX; DLX 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 puzzle[][]; bool read() {
rep(i, , )
if (scanf("%s", puzzle[i])!=)
return false;
return true;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int kase = ; while (read()) {
if (kase++)
putchar('\n'); solver.init();
rep(r, , ) {
rep(c, , ) {
rep(v, , ) {
if (puzzle[r][c]=='-' || puzzle[r][c]=='A'+v) {
vi columns;
columns.pb(encode(SLOT, r, c));
columns.pb(encode(ROW, r, v));
columns.pb(encode(COL, c, v));
columns.pb(encode(SUB, r/*+c/, v));
solver.addRow(encode(r, c, v), columns);
}
}
}
} vi ans;
bool flag = solver.solve(ans);
#ifndef ONLINE_JUDGE
puts(flag ? "Yes":"No");
#endif
int sz = SZ(ans); rep(i, , sz) {
int r, c, v;
decode(ans[i], r, c, v);
puzzle[r][c] = v + 'A';
} rep(i, , )
puts(puzzle[i]);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

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

  1. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  2. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  3. 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉

    DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $  当然这里的$i$和$k$都是偶数啦~ ...

  4. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  5. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  6. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

  7. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  8. 【poj】1001

    [题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...

  9. 【POJ】3070 Fibonacci

    [算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...

随机推荐

  1. 使用struts+spring+hibernate组装web应用

    这篇文章将讨论怎样组合几个着名的框架去做到松耦合的目的,怎样建立你的构架,怎样让你的各个应用层保持一致.富于挑战的是:组合这些框架使得每一层都以一种松耦合的方式彼此沟通,而与底层的技术无关.这篇文章将 ...

  2. 使用info.plist(或工程名-info.plist)向程序中添加软件Build ID或者版本号信息

    在实际应用程序开发过程中,经常需要向程序中添加软件版本号或者类似的信息,以保证之后发现问题时知道bug所在的版本,我们可以通过在工程名-info.plist文件中设置相关的key/value对(键/值 ...

  3. 轮子来袭 vJine.Core 之 AppConfig<T>

    1.引用vJine.Core; 2.定义配置类; using System; using System.Collections.Generic; using System.Text; using Sy ...

  4. OC6_字符串练习

    // // QFString.h // OC6_字符串练习 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhang ...

  5. Boot Petalinux Project Using a remote system

    通过jtag实现在远程服务器端下载petalinux image到连接在本地PC的开发板上的方法. 具体连接方式为 比如Host的系统为Windows,Remote system为运载在远程服务器上的 ...

  6. PHP - 5.4 Array dereferencing 数组值

    在5.4之前我们直接获取数组的值得方法如下 <?php $str = 'a;b;c;d'; list($value) = explode(';',$str); echo $value; 结果为: ...

  7. 干货:Web应用上线之前程序员应该了解的技术细节

    [伯乐在线注]:<Web 应用上线前,程序员应考虑哪些技术细节呢?>这是 StackExchange 上面的一个经典问题贴. 最赞回复有 2200+ 顶,虽然大多数人可能都听过其中大部分内 ...

  8. 005.ClearStoredGroups方法

    Delphi procedure ClearStoredGroups; 类型:procedure 可见性:protected 所在单元:System.RegularExpressionsCore 父类 ...

  9. Project not selected to build for this solution configuration.

    Project not selected to build for this solution configuration.   When you upgrade your older solutio ...

  10. C语言的数据类型

    复习之余,做点笔记<C语言之数据类型> 一.整数数据类型 (1)整数数据类型 整数类型 char 字符型变量   1字节(8Bit) short 短整型      2字节(16Bit) i ...