题意:

给你9*9的矩阵。对于每一个数字。能减16代表上面有墙,能减32代表以下有墙。

。。

最后剩下的数字是0代表这个位置数要求,不是0代表这个数已知了。

然后通过墙会被数字分成9块。

然后做数独,这里的数独不是分成9个3*3的小块而是通过墙分成的。

思路:

首先通过数字作出墙。

然后bfs求连通块。dfs也能够。目的是分块。

然后就是dlx数独模板题了。

这里要注意的是假设找到答案2次就说明有多组解了。就应该停止返回了。不然会TLE。

代码:

#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"cmath"
#include"queue"
#include"map"
#include"vector"
#include"string"
using namespace std;
#define RN 9*9*9+5
#define CN 4*9*9+5
#define N 9*9*9*4+5
int wall[12][12][12][12];
int mp[12][12],used[12][12];
int dis[4][2]= {{0,1},{0,-1},{-1,0},{1,0}};
int kx,ff;
template<class T>inline void getd(T &x)
{
int ch = getchar();
bool minus = false;
while(!isdigit(ch) && ch != '-')ch = getchar();
if(ch == '-')minus = true, ch = getchar();
x = ch - '0';
while(isdigit(ch = getchar()))x = x * 10 - '0' + ch;
if(minus)x = -x;
} struct node
{
int x,y;
};
void bfs(int x,int y)
{
node cur,next;
cur.x=x;
cur.y=y;
queue<node>q;
q.push(cur);
used[cur.x][cur.y]=kx;
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=0; i<4; i++)
{
next.x=cur.x+dis[i][0];
next.y=cur.y+dis[i][1];
if(used[next.x][next.y]!=0 || wall[cur.x][cur.y][next.x][next.y]==1 ) continue;
used[next.x][next.y]=kx;
q.push(next);
}
}
kx++;
return ;
} struct DLX
{
int n,m,C;
int U[N],D[N],L[N],R[N],Row[N],Col[N];
int H[RN],S[CN],cnt,ans[RN];
void init(int _n,int _m)
{
n=_n;
m=_m;
for(int i=0; i<=m; i++)
{
U[i]=D[i]=i;
L[i]=(i==0? m:i-1);
R[i]=(i==m?0:i+1);
S[i]=0;
}
C=m;
for(int i=1; i<=n; i++) H[i]=-1;
}
void link(int x,int y)
{
C++;
Row[C]=x;
Col[C]=y;
S[y]++;
U[C]=U[y];
D[C]=y;
D[U[y]]=C;
U[y]=C;
if(H[x]==-1) H[x]=L[C]=R[C]=C;
else
{
L[C]=L[H[x]];
R[C]=H[x];
R[L[H[x]]]=C;
L[H[x]]=C;
}
}
void del(int x)
{
R[L[x]]=R[x];
L[R[x]]=L[x];
for(int i=D[x]; i!=x; 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 rec(int x)
{
for(int i=U[x]; i!=x; i=U[i])
{
for(int j=L[i]; j!=i; j=L[j])
{
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
R[L[x]]=x;
L[R[x]]=x;
}
void dance(int x)
{
if(R[0]==0)
{
ff++;
if(ff>=2) return ;
cnt=x;
for(int i=0; i<cnt; i++)
{
int tep=ans[i]-1;
int a=tep/81,b=(tep%81)/9;
mp[a+1][b+1]=tep%9+1;
}
return ;
}
int now=R[0];
for(int i=R[0]; i!=0; i=R[i])
{
if(S[i]<S[now]) now=i;
}
del(now);
for(int i=D[now]; i!=now; i=D[i])
{
ans[x]=Row[i];
for(int j=R[i]; j!=i; j=R[j]) del(Col[j]);
dance(x+1);
if(ff>=2) return ;
for(int j=L[i]; j!=i; j=L[j]) rec(Col[j]);
}
rec(now);
return ;
}
} dlx;
void getplace(int i,int j,int k,int &x,int &a,int &b,int &c)
{
x=(i-1)*81+(j-1)*9+k;
a=81+(i-1)*9+k;
b=81*2+(j-1)*9+k;
c=81*3+(used[i][j]-1)*9+k;
}
int main()
{
int t,cas=1;
cin>>t;
while(t--)
{
memset(wall,0,sizeof(wall));
for(int i=1; i<=9; i++)
{
for(int j=1; j<=9; j++)
{
int x;
getd(x);
if(x-128>=0)
{
x-=128;
wall[i][j][i][j-1]=1;
}
if(x-64>=0)
{
x-=64;
wall[i][j][i+1][j]=1;
}
if(x-32>=0)
{
x-=32;
wall[i][j][i][j+1]=1;
}
if(x-16>=0)
{
x-=16;
wall[i][j][i-1][j]=1;
}
mp[i][j]=x;
}
}
kx=1;
memset(used,0,sizeof(used));
for(int i=1; i<=9; i++) for(int j=1; j<=9; j++) if(used[i][j]==0) bfs(i,j); dlx.init(9*9*9,4*9*9);
for(int i=1; i<=9; i++)
{
for(int j=1; j<=9; j++)
{
int tep=(i-1)*9+j;
int x,a,b,c;
if(mp[i][j]==0)
{
for(int k=1; k<=9; k++)
{
getplace(i,j,k,x,a,b,c);
dlx.link(x,tep);
dlx.link(x,a);
dlx.link(x,b);
dlx.link(x,c);
}
}
else
{
getplace(i,j,mp[i][j],x,a,b,c);
dlx.link(x,tep);
dlx.link(x,a);
dlx.link(x,b);
dlx.link(x,c);
}
}
}
ff=0;
dlx.dance(0);
printf("Case %d:\n",cas++);
if(ff==0) puts("No solution");
else if(ff==1)
{
for(int i=1; i<=9; i++) for(int j=1; j<=9; j++) printf(j==9?"%d\n":"%d",mp[i][j]);
}
else puts("Multiple Solutions");
}
return 0;
}

[DLX+bfs] hdu 4069 Squiggly Sudoku的更多相关文章

  1. 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 ...

  2. (中等) 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 ...

  3. [DLX]HDOJ4069 Squiggly Sudoku

    题意:有9*9的格子 每个格子 由五部分组成:上(16).右(32).下(64).左(128).和该格的数值(0~9) 若上下左右有分割格子的线 就加上相应的数, 该格的数值若为0,则是未知  1~9 ...

  4. hdu 4069 福州赛区网络赛I DLC ***

    再遇到一个DLC就刷个专题 #include <stdio.h> #include <string.h> #include <iostream> #include ...

  5. 搜索(DLX): POJ 3074 3076 Sudoku

    POJ 3074 : Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller ...

  6. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  7. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

  8. (hdu)5547 Sudoku (4*4方格的 数独 深搜)

    Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game ...

  9. hdu 4069 垃圾数独

    首先dfs给每个格子分一个大的区块 其次套板子就a 我一开始直接在选取行的时候填数独,发现超时 我这一行也就4个元素,找到 x <= 81 的列计算元素位置,81 < x <= 16 ...

随机推荐

  1. mysql 查看存储过程 并导出

    查询数据库中的存储过程 select * from mysql.proc where db = dbName and `type` = 'PROCEDURE' show procedure statu ...

  2. 使用webpack+vue.js构建前端工程化

    参考文章:https://blog.csdn.net/qq_40208605/article/details/80661572 使用webpack+vue.js构建前端工程化本篇主要介绍三块知识点: ...

  3. 第2节 mapreduce深入学习:13、mapreduce的整个运行过程(多看几遍)

    两个问题: 1.mapTaks的个数怎么确认:与block块相关2.reducetask的个数怎么确认:没法确认,需要反复的设置尝试,找到最优值.  手动进行设置 job.setNumReduceTa ...

  4. Bitmap.createBitmap函数有6个重载方法

    位图剪切参考重载方法4和6,重载方法6比较简单 public static Bitmap createBitmap (Bitmap src)从原位图src复制出一个新的位图,和原始位图相同 publi ...

  5. ORACLE in与exists语句的区别(一)

    select * from Awhere id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B ...

  6. 转载:Django之Form组件

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 小试牛刀 1.创建Form类 +? 1 2 3 ...

  7. LeetCode(70) Climbing Stairs

    题目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli ...

  8. em的理解

    em 版本:CSS1 说明: 自己的理解: 注意地方: 浏览器默认大小为16px. 谷歌浏览器最小字体为12px. font-size;有继承性. 判断步骤: []看该元素本身有没有设置字体大小: 有 ...

  9. 树剖 lca

    GeneralLiu  橙边为轻边 红边为重边 绿数为每个点的 top 橙数为每个点的编号 步骤 1 先预处理 每个点的 deep深度  size子树大小  dad父节点 2 再预处理 每个点的 to ...

  10. [BZOJ1264][AHOI2006]基因匹配Match(DP + 树状数组)

    传送门 有点类似LCS,可以把 a[i] 在 b 串中的位置用一个链式前向星串起来,由于链式前向星是从后往前遍历,所以可以直接搞. 状态转移方程 f[i] = max(f[j]) + 1 ( 1 &l ...