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: ...
随机推荐
- Enable-Migrations 迁移错误,提示找不到连接字符串
把迁移项目设为启动项目即可,若是MVC Web项目可能就没有这个问题.
- 数据库(数据库、表及表数据、SQL语句)
数据库MYSQL 今日内容介绍 u MySQL数据库 u SQL语句 第1章 数据库 1.1 数据库概述 l 什么是数据库 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储 ...
- 洛谷P2606 [ZJOI2010]排列计数(组合数 dp)
题意 题目链接 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案 ...
- event——事件对象详解
PS:转自https://www.cnblogs.com/songyaqi/p/5204143.html 1. 事件对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标 ...
- web标准、可用性、可访问性
前言:大家不难发现,只要是招聘UED相关的岗位,如前端开发工程师.交互设计师.用户研究员甚至视觉设计师,一般都对web标准.可用性和可访问性的理解有要求.那么到底什么是web标准.可用性.可访问性呢? ...
- C++ 中函数后面跟const是什么意思
问题:c++:void display( ) const 中的const是什么意思?简答:意思是除了表明了mutable的成员变量以外该类的其他的成员变量在这个函数内一律不能修改. 详细:加const ...
- win7双网卡走哪个网卡路由设置
有没有软件能做这个我还真不知道.说说我的做法吧: 单位里无线是可以访问Internet的,有线是用来访问公司内部系统的. 默认的54M无线网络和100M的有线网络,系统在选择默认路由的时候肯定是选择有 ...
- git的常用操作指令
git学习网址: http://www.backlogtool.com/git-guide/cn/intro/intro2_3.html 廖雪峰的git教程 git的工作区和暂存区(描述git的工作流 ...
- java面试题(杨晓峰)---第五讲String、StringBuffer、StringBuilder有什么区别?
线程 字符 操作频繁度 1 String (1)String的创建机制 由于String在java世界中使用过于频繁,java为了避免在一个系统中产生大量重复的String对象,引入了字符串常量池,其 ...
- 已知一棵完全二叉树,求其节点的个数 要求:时间复杂度低于O(N),N为这棵树的节点个数
package my_basic.class_4; public class Code_08_CBTNode { // 完全二叉树的节点个数 复杂度低于O(N) public static class ...