【POJ】3076 Sudoku
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的更多相关文章
- 【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, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【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$都是偶数啦~ ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- 【POJ】1222 EXTENDED LIGHTS OUT
[算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...
- 【POJ】【1637】Sightseeing tour
网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...
- 【poj】1001
[题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...
- 【POJ】3070 Fibonacci
[算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...
随机推荐
- NodeJS连接MongoDB数据库时报错
今天第一次尝试连接MongoDB数据库,具体步骤也很简单. 首先,通过NodeJS运行环境安装MongoDB包,进入要安装的目录,执行语句 npm install mongodb 安装成功后,通过如下 ...
- 关于ligerUi的ligertree的初始化默认选中指定项目的方法
LigerUi中ligerTree官方示例代码片段: var parm = function (data) { return data.text.indexOf('节点1.3') == 0; }; t ...
- c#个人记录常用方法(更新中)
1.日期毫秒转换为标准的C#日期格式 //使用时,先将秒Convert.ToInt64,返回格式2015-2-10 14:03:33 public DateTime JavaTimeToC(long ...
- Sql语句批量更新数据(多表关联)
最近在项目中遇到一个问题,原来设计的功能是不需要一个特定的字段值depid的,但是新的功能需要根据depid来展现,于是出现了这样一个问题,新增加的数据都有正确的depid,而原来的大量的数据就没有d ...
- UBUNTU下如何开启SSHD服务
“人生本来就有很多事是徒劳无功的.”就像青春,虽然徒劳,却在人的心里住得最久,挥之不去,怀念那段青涩年华,怀念它的... sudo apt-get install openssh-server Ubu ...
- normalize.css介绍
Normalize.css 只是一个很小的CSS文件,但它在默认的HTML元素样式上提供了跨浏览器的高度一致性.相比于传统的CSS reset,Normalize.css是一种现代的.为HTML5准备 ...
- shopnc 商城源码阅读笔记-缓存技术
缓存方式 : 从 shopnc 的缓存驱动目录 /framework/cache里已有的实现类来看,shopnc支持以下5种缓存方式 apc Eaccelerator file memcache xc ...
- Nginx+Keepalived主备负载均衡
实验环境及软件版本: CentOS版本: 6.6(2.6.32.-504.el6.x86_64) nginx版本: nginx-1.6.2 keepalived版本:keepalived ...
- leetcode 解题 String to Integer (atoi)(C&python)
//此题是easy题,比较简单,主要困难在考虑全输入的各种情况://1.开始的时候有空格等空白字符//2.开头有加减号//3.溢出(第一次写就是没有考虑到这个情况) //C代码int myAtoi(c ...
- 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...