题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069

Problem Description
Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each column, each row, and each of the nine Connecting-sub-grids that compose the grid contains all of the digits from 1 to 9.
Left figure is the puzzle and right figure is one solution.

Now, give you the information of the puzzle, please tell me is there no solution or multiple solution or one solution.
 
Input
The first line is a number T(1<=T<=2500), represents the number of case. The next T blocks follow each indicates a case.
Each case contains nine lines, Each line contains nine integers.
Each module number tells the information of the gird and is the sum of up to five integers:
0~9: '0' means this gird is empty, '1' - '9' means the gird is already filled in.
16: wall to the up
32: wall to the right
64: wall to the down
128: wall to the left
I promise there must be nine Connecting-sub-grids, and each contains nine girds.
 
Output
For each case, if there are Multiple Solutions or no solution just output "Multiple Solutions" or "No solution". Else output the exclusive solution.(as shown in the sample output)

题目大意:给一个不规则的9阶数独,问是否有唯一解,是则输出。

思路:先DFS一下,找出每个格子对应的块号,再套DLX的模板。

代码(1203MS):

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXC = * * + ;
const int MAXR = * * + ;
const int MAXP = MAXR * + MAXC; struct DLX {
int sz;
int sum[MAXC];
int row[MAXP], col[MAXP];
int left[MAXP], right[MAXP], up[MAXP], down[MAXP];
int ansd, ans[MAXR], anscnt; void init(int n) {
for(int i = ; i <= n; ++i) {
up[i] = down[i] = i;
left[i] = i - ; right[i] = i + ;
}
left[] = n; right[n] = ;
sz = n + ;
memset(sum, , sizeof(sum));
} void add_row(int r, vector<int> &func) {
int first = sz;
for(size_t i = ; i < func.size(); ++i) {
int c = func[i];
left[sz] = sz - ; right[sz] = sz + ; up[sz] = up[c]; down[sz] = c;
down[up[c]] = sz; up[c] = sz;
row[sz] = r; col[sz] = c;
++sum[c], ++sz;
}
left[first] = sz - ; right[sz - ] = first;
} void remove(int c) {
left[right[c]] = left[c];
right[left[c]] = right[c];
for(int i = down[c]; i != c; i = down[i]) {
for(int j = right[i]; j != i; j = right[j])
up[down[j]] = up[j], down[up[j]] = down[j], --sum[col[j]];
}
} void restore(int c) {
for(int i = up[c]; i != c; i = up[i]) {
for(int j = left[i]; j != i; j = left[j])
up[down[j]] = j, down[up[j]] = j, ++sum[col[j]];
}
left[right[c]] = c;
right[left[c]] = c;
} bool dfs(int d) {
if(!right[]) {
ansd = d;
return ++anscnt == ;
}
int c = right[];
for(int i = right[]; i != ; i = right[i]) if(sum[i] < sum[c]) c = i;
remove(c);
for(int i = down[c]; i != c; i = down[i]) {
if(!anscnt) ans[d] = row[i];
for(int j = right[i]; j != i; j = right[j]) remove(col[j]);
if(dfs(d + )) return true;
for(int j = left[i]; j != i; j = left[j]) restore(col[j]);
}
restore(c);
return false;
} int solve(vector<int> &v) {
v.clear();
anscnt = ;
dfs();
if(anscnt == ) for(int i = ; i < ansd; ++i) v.push_back(ans[i]);
return anscnt;
}
} solver; const int SLOT = ;
const int ROW = ;
const int COL = ;
const int SUB = ; int fr[] = {-, , , };
int fc[] = {, , , -};
int fp[] = {, , , }; int mat[MAXN][MAXN];
int val[MAXN][MAXN], cnt;
int T, n = ; bool in_n(int x) {
return <= x && x < n;
} void dfs(int r, int c, int p) {
val[r][c] = p;
for(int i = ; i < ; ++i) {
int nr = r + fr[i], nc = c + fc[i];
if(in_n(nr) && in_n(nc) && ((fp[i] & mat[r][c]) == ) && !val[nr][nc])
dfs(nr, nc, p);
}
} void print(int mat[MAXN][MAXN]) {
for(int i = ; i < n; ++i) {
for(int j = ; j < n; ++j) printf("%d", mat[i][j]);
puts("");
}
} 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;
} int main() {
scanf("%d", &T);
for(int kase = ; kase <= T; ++kase) {
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j) scanf("%d", &mat[i][j]);
memset(val, , sizeof(val));
cnt = ;
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j) if(!val[i][j]) dfs(i, j, ++cnt);
printf("Case %d:\n", kase);
//print(val);
solver.init( * * );
for(int r = ; r < n; ++r)
for(int c = ; c < n; ++c)
for(int i = ; i < ; ++i) mat[r][c] &= ~fp[i];
//print(mat);
for(int r = ; r < n; ++r) for(int c = ; c < n; ++c) for(int v = ; v < n; ++v) {
if(!mat[r][c] || mat[r][c] == + v) {
vector<int> func;
func.push_back(encode(SLOT, r, c));
func.push_back(encode(ROW, r, v));
func.push_back(encode(COL, c, v));
func.push_back(encode(SUB, val[r][c] - , v));
solver.add_row(encode(r, c, v), func);
}
}
vector<int> ans;
int res = solver.solve(ans);
if(res == ) puts("No solution");
if(res == ) {
int r, c, v;
for(size_t i = ; i < ans.size(); ++i) {
decode(ans[i], r, c, v);
mat[r][c] = + v;
}
print(mat);
}
if(res == ) puts("Multiple Solutions");
}
}

HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)的更多相关文章

  1. HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...

  2. HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063 Description You are playing a flying game. In th ...

  3. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  4. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memo ...

  6. (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)

    http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others)  ...

  7. 2016 ACM/ICPC Asia Regional Qingdao Online(2016ACM青岛网络赛部分题解)

    2016 ACM/ICPC Asia Regional Qingdao Online(部分题解) 5878---I Count Two Three http://acm.hdu.edu.cn/show ...

  8. hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  9. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

随机推荐

  1. Bootstrap 进度条媒体对象和 Well 组件

    一.Well 组件 这个组件可以实现简单的嵌入效果. //嵌入效果 <div class="well"> Bootstrap </div> //有 lg 和 ...

  2. 窗口类型(Widget, Window, Dialog, Desktop, SubWindow等等)

    http://doc.qt.io/qt-5/qwidget.html#windowFlags-prop http://doc.qt.io/qt-5/qtwidgets-widgets-windowfl ...

  3. HTTP访问的两种方式(HttpClient+HttpURLConnection)整合汇总对比

    HttpClient: HttpClient是Apache Jakarta Common下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版 ...

  4. MVC分页控件之二,为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集(转)

    namespace Entity { public interface IPagedList { /// <summary> /// 记录数 /// </summary> in ...

  5. Sqlcompletefree

    Sqlcompletefree SQLSERVER中SQL代码自动提示填充,自动格式化SQL语句工具Sqlcompletefree.方便开发操作.

  6. qt QString 与 int,char的转换

    每次QString转换int或者char的时候都要查资料,记录一下,方便下次查看. 参考: http://blog.csdn.net/ei__nino/article/details/7297791 ...

  7. java中String.valueOf()和toString()方法的区别

    http://www.ztyhome.com/android-tostring-string-valueof-diff/

  8. POJ1326问题描述

    Description Mileage program of ACM (Airline of Charming Merlion) is really nice for the travelers fl ...

  9. 官方教程:教你用好微信JS-SDK接口

    微信开放JS-SDK接口,开发者和行业用户可谓是欢欣鼓舞.奔走相告,目测将激起一大波第三方开发商的创新产品!真真是H5开发者的利好!但也有用户表示,还不了解JS-SDK接口到底是啥,究竟怎么用.现在, ...

  10. oracle ORA-00001:违反唯一约束条件

    --获取约束信息 select * from information_schema.constraint_column_usage---可以获取指定数据库中的所有约束的信息以及约束与列的对应关系 go ...