题意:

给你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. 第2节 hive基本操作:10、外部分区表综合练习

    外部分区表综合练习: 需求描述:现在有一个文件score.csv文件,存放在集群的这个目录下/export/servers/scoredatas/month=201806,这个文件每天都会生成,存放到 ...

  2. secureCRT 破解

    转自:http://www.cnblogs.com/qingtingzhe/articles/5008902.html

  3. css hack解决方案

    现在大家做项目的时候估计很多都已经不怎么考虑兼容问题,大多数的公司都已经舍弃ie7.8了,都是从ie9+开始,所以说不会有那么多的兼容问题需要去解决了,但是由于本人力求完美的工作习惯,做项目的时候还是 ...

  4. 采用Qt快速绘制多条曲线(折线),跟随鼠标动态显示线上点的值(基于Qt的开源绘图控件QCustomPlot进行二次开发)

    QCustomPlot是一个开源的基于Qt的第三方绘图库,能够绘制漂亮的2D图形. QCustomPlot的官方网址:https://www.qcustomplot.com/ 从官网下载QCustom ...

  5. 笔试算法题(27):判断单向链表是否有环并找出环入口节点 & 判断两棵二元树是否相等

    出题:判断一个单向链表是否有环,如果有环则找到环入口节点: 分析: 第一个问题:使用快慢指针(fast指针一次走两步,slow指针一次走一步,并判断是否到达NULL,如果fast==slow成立,则说 ...

  6. [Python3网络爬虫开发实战] 1.5.1-PyMySQL的安装

    在Python 3中,如果想要将数据存储到MySQL中,就需要借助PyMySQL来操作,本节中我们介绍一下它的安装方式. 1. 相关链接 GitHub:https://github.com/PyMyS ...

  7. 杭电 1009 FatMouse' Trade (贪心)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  8. IDA-IDC脚本编写语法

    1.IDA脚本编写基础 IDC是IDA内置的脚本语言,其语法与C非常相似,它是一种解释性语言. 执行方法 在IDA中按SHIFT+F2键会弹出一个对话框,把语句直接写在对话框中,点击run就可被运行. ...

  9. hdu 2433 Travel (最短路树)

     One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...

  10. 洛谷 通天系列 P1760 P1757 P1759

    P1760 通天之汉诺塔 汉诺塔问题.一个高精乘单精解决 ans=2^n-1 /*by SilverN*/ #include<algorithm> #include<iostream ...