UVa 1309 DLX Sudoku
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的更多相关文章
- 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 ...
- POJ 3074 Sudoku (DLX)
Sudoku Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 搜索(DLX): POJ 3074 3076 Sudoku
POJ 3074 : Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller ...
- POJ 3076 Sudoku DLX精确覆盖
DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 4416 Accepte ...
- (中等) 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 ...
- (简单) POJ 3076 Sudoku , DLX+精确覆盖。
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- (简单) POJ 3074 Sudoku, DLX+精确覆盖。
Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- POJ 3074 Sudoku DLX精确覆盖
DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8336 Accepted: ...
随机推荐
- CallContext的LogicalCallContext在多线程环境下面公用变量
压根名听说过这个类的看这里:如何实现对上下文(Context)数据的统一管理 原来以为CallContext就可以直接在多线程环境下面共享使用的,今天突然想到:Asp.Net环境下面,设置来设置去的, ...
- 1043 方格取数 2000年NOIP全国联赛提高组
1043 方格取数 2000年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 设有N* ...
- Android用RecyclerView实现的二维Excel效果组件
excelPanel 二维RecyclerView.不仅可以加载历史数据,而且可以加载未来的数据. 包括在您的项目中 excelPanel 二维RecyclerView.不仅可以加载历史数据,而且 ...
- 如何修改Ruby的gem源(gem sources)
Ruby环境下的gem sources地址默认是国外网络地址,所以在使用gem的过程中经常会出现找不到资源的Error.那么如何解决这种Error?方法很简单:要么就多次尝试执行gem命令,要么就修改 ...
- 洛谷 P1855 榨取kkksc03
题目描述 洛谷2的团队功能是其他任何oj和工具难以达到的.借助洛谷强大的服务器资源,任何学校都可以在洛谷上零成本的搭建oj并高效率的完成训练计划. 为什么说是搭建oj呢?为什么高效呢? 因为,你可以上 ...
- 在SAP C4C里触发SAP ERP的ATP check和Credit check
在C4C里创建一个新的Sales Quote: 添加三个行项目: 执行action "Request External Pricing"会从ERP更新pricing信息,触发ATP ...
- HDU 5452 Minimum Cut (Spaning Tree)
生成树的上的一个非根结点对应一条生成树上的边,然后这个结点的子树上连出去的边就对应去掉这条边的割, 然后就可以对树外的边求LCA,在LCA上标记,利用这个信息可以算出有多少条边在子树上,以及有多少条边 ...
- bzoj3209:3209: 花神的数论题
觉得还是数位dp的那种解题形式但是没有认真的想,一下子就看题解.其实还是设置状态转移.一定要多思考啊f[i][j]=f[i-1][j]+g[i-1][j] g[i][j]=f[i-1][j-1]+g[ ...
- Spark Job具体的物理执行
即使采用pipeline的方式,函数f对依赖的RDD中的数据集合的操作也会有两种方式: 1.f(record),f作用于集合的每一条记录,每次只作用于一条记录 2.f(records),f一次性作用于 ...
- php循环a-z字母表
ord — 返回字符的 ASCII 码值 说明 int ord ( string $string ) 返回字符串 string 第一个字符的 ASCII 码值. 该函数是 chr() 的互补函数. ...