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.
 
  还是数独问题,其实和前面的没有什么两样,然后对于每个奇形怪状的9格子,我是用BFS来构造的。。。。。。
 
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue> using namespace std; const int MaxN=;
const int MaxM=;
const int MaxNode=MaxN*MaxM; int map1[][]; struct DLX
{
int U[MaxNode],D[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode],row[MaxNode];
int size,n,m;
int H[MaxN],S[MaxM];
int ans[],ans1[],anst[];
int ansnum,depth; void init(int _n,int _m)
{
ansnum=; n=_n;
m=_m; for(int i=;i<=m;++i)
{
U[i]=D[i]=i;
L[i]=i-;
R[i]=i+;
row[i]=; S[i]=;
}
L[]=m;
R[m]=; size=m; for(int i=;i<=n;++i)
H[i]=-;
} void Link(int r,int c)
{
col[++size]=c;
row[size]=r;
++S[c]; U[size]=U[c];
D[size]=c;
D[U[c]]=size;
U[c]=size; if(H[r]==-)
H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[size]=H[r];
R[L[H[r]]]=size;
L[H[r]]=size;
}
} void remove(int c)
{
L[R[c]]=L[c];
R[L[c]]=R[c]; for(int i=D[c];i!=c;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 resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
{
U[D[j]]=j;
D[U[j]]=j;
++S[col[j]];
} L[R[c]]=R[L[c]]=c;
} void showans()
{
for(int i=;i<depth;++i)
ans1[(anst[i]-)/+]=(anst[i]-)%+; for(int i=;i<=;++i)
{
cout<<ans1[i]; if(i%==)
cout<<endl;
}
} void copyans()
{
for(int i=;i<;++i)
anst[i]=ans[i];
} bool Dance(int d)
{
if(R[]==)
{
depth=d; if(ansnum)
{
++ansnum;
return ;
} ++ansnum; copyans(); return ;
} int c=R[]; for(int i=R[];i!=;i=R[i])
if(S[i]<S[c])
c=i; remove(c); for(int i=D[c];i!=c;i=D[i])
{
ans[d]=row[i]; for(int j=R[i];j!=i;j=R[j])
remove(col[j]); if(Dance(d+))
return ; for(int j=L[i];j!=i;j=L[j])
resume(col[j]);
} resume(c); return ;
} void display()
{
for(int i=R[];i!=;i=R[i])
{
cout<<i<<' ';
for(int j=D[i];j!=i;j=D[j])
cout<<'('<<j<<','<<(row[j]-)%+<<')'<<' '; cout<<endl;
}
}
}; DLX dlx;
int s[]; void getchange(int &r,int &c1,int &c2,int &c3,int &c4,int i,int j,int k)
{
r=(i*+j)*+k;
c1=i*+j+;
c2=i*+k+;
c3=j*+k+;
c4=map1[i][j]*+k+;
} void slove()
{
int r,c1,c2,c3,c4; dlx.init(,); for(int i=;i<;++i)
for(int j=;j<;++j)
for(int k=;k<=;++k)
if(s[i*+j]== || s[i*+j]==k)
{
getchange(r,c1,c2,c3,c4,i,j,k); dlx.Link(r,c1);
dlx.Link(r,c2);
dlx.Link(r,c3);
dlx.Link(r,c4);
} /* for(int i=1;i<=81;++i)
for(int j=1;j<=9;++j)
dlx.Link(j+(i-1)*9,i); for(int i=1;i<=81;++i)
for(int j=1;j<=9;++j)
dlx.Link(9*(j-1)+(i-1)%9+1+81*((i-1)/9),i+81); for(int i=1;i<=81;++i)
for(int j=1;j<=9;++j)
dlx.Link((j-1)*81+i,i+162); for(int i=1;i<=3;++i)
for(int j=1;j<=3;++j)
for(int k=1;k<=9;++k)
for(int l=1;l<=3;++l)
for(int m=1;m<=3;++m)
dlx.Link((i-1)*243+(j-1)*27+k+(l-1)*81+(m-1)*9,(i-1)*27+(j-1)*9+k+243); for(int i=0;i<81;++i)
if(s[i]!='.')
{
dlx.ans1[i+1]=s[i]-'0'; dlx.remove(i+1); for(int j=dlx.D[i+1];j!=i+1;j=dlx.D[j])
{
if((dlx.row[j]-1)%9+1==s[i]-'0')
{
for(int k=dlx.R[j];k!=j;k=dlx.R[k])
dlx.remove(dlx.col[k]); break;
}
}
}
*/ dlx.Dance(); int temp=dlx.ansnum; if(temp==)
cout<<"No solution"<<endl;
else if(temp==)
cout<<"Multiple Solutions"<<endl;
else
dlx.showans();
} const int step[][]={{-,},{,},{,},{,-}};
int kcou;
int lu[][][]; void bfs(int num,int x,int y)
{
queue <int> que;
int temp,t1,t2; que.push(x*+y);
map1[x][y]=num; while(!que.empty())
{
temp=que.front();
que.pop(); t1=temp/;
t2=temp%; for(int k=;k<;++k)
if(lu[t1][t2][k] && map1[t1+step[k][]][t2+step[k][]]==-)
{
que.push((t1+step[k][])*+t2+step[k][]);
map1[t1+step[k][]][t2+step[k][]]=num;
}
}
} void getmap()
{
int cou=; for(int i=;i<;++i)
for(int j=;j<;++j)
if(map1[i][j]==-)
bfs(cou++,i,j);
} int main()
{
ios::sync_with_stdio(false); int T,t;
int fang[];
cin>>T; for(int cas=;cas<=T;++cas)
{
memset(map1,-,sizeof(map1));
memset(s,,sizeof(s));
memset(lu,,sizeof(lu));
kcou=; for(int i=;i<;++i)
for(int j=;j<;++j)
{
cin>>t; for(int k=;k<;++k)
if(((t>>(+k))&)==)
lu[i][j][k]=; s[i*+j]=t&;
} getmap(); cout<<"Case "<<cas<<':'<<endl; slove();
} return ;
}

(中等) HDU 4069 Squiggly Sudoku , DLX+精确覆盖。的更多相关文章

  1. POJ 3076 Sudoku DLX精确覆盖

    DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 4416   Accepte ...

  2. (简单) POJ 3074 Sudoku, DLX+精确覆盖。

    Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...

  3. POJ 3074 Sudoku DLX精确覆盖

    DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: ...

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

  5. [DLX+bfs] hdu 4069 Squiggly Sudoku

    题意: 给你9*9的矩阵.对于每一个数字.能减16代表上面有墙,能减32代表以下有墙. .. 最后剩下的数字是0代表这个位置数要求,不是0代表这个数已知了. 然后通过墙会被数字分成9块. 然后做数独, ...

  6. (简单) POJ 3076 Sudoku , DLX+精确覆盖。

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  7. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

  8. 【转】DLX 精确覆盖 重复覆盖

    问题描述: 给定一个n*m的矩阵,有些位置为1,有些位置为0.如果G[i][j]==1则说明i行可以覆盖j列. Problem: 1)选定最少的行,使得每列有且仅有一个1. 2)选定最少的行,使得每列 ...

  9. (简单) HUST 1017 Exact cover , DLX+精确覆盖。

    Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...

随机推荐

  1. 转 Oracle 12c 使用scott等普通用户的方法

    一.前言 最近电脑上安装了oracle 12c数据库,想体验下新特性.安装完后,便像11g一样在dos窗口进行下面的操作: SQL*Plus: Release 12.1.0.2.0 Productio ...

  2. struts2中的值栈对象ValueStack

    ValueStack, 即值栈对象. 值栈对象: 是整个struts数据存储的核心,或者叫中转站. 用户每次访问struts的action,都会创建一个Action对象.值栈对象.ActionCont ...

  3. HDU 4027 <线段树,区间√>

    题目连接 题意 给出一个区间,每次把[l,r]内的值√,维护区间和. 坑: £:l会比r大,swap. £: 当f[i].sum=f[i].r-f[i].l+1;,不修改.因为保证每个数都大于等于1, ...

  4. sql trim()函数去掉两头空格

    1.sql trim()函数去掉两头空格 sql语法中没有直接去除两头空格的函数,但有ltrim()去除左空格rtrim()去除右空格. 合起来用就是sql的trim()函数,即select ltri ...

  5. UVALive 7291 Kinfolk(最近公共祖先)

    题目中的描述就很最近公共祖先,再说其实这个题并不难,就是麻烦点(代码其实可以化简的),我写的判定比较多. 方法:求出两者的最近公共祖先lca,在求出两者到lca的距离 分析:给出a和b,如果LCA(a ...

  6. ubuntu 解压 windows 生成的 zip 文件乱码问题

    在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 有两种方式解决问题:(建议采用方法 ...

  7. ab测试 uwsgi遇到的问题

    1 请求并发数目较大时,接收到的数目小于发送的数目 1.1 描述:uwsgi正常返回302跳转 ab -n 5000 -c 250 -g test.log "192.168.50.20:90 ...

  8. 第11章 类的高级特性--final

    1.final变量 (1)final关键字可用于变量声明,一旦该变量被设定,就不可以再改变该变量的值.通常,由final定义的变量为常量.例如:final double PI=3.14; final关 ...

  9. 高性能web开发:如何加载js,,js的存放位置

    外部JS的阻塞下载 所有浏览器在下载JS的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等.至到JS下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容. 有人会问:为什么JS不能像 ...

  10. Linux批量替换文本,文件夹内所有文本内容

    1.替换文件夹内所有文件匹配的字符串 sed -i "s/旧内容/新内容/g" `grep 旧内容 -rl 文件夹路径` 例如将/var/www/test文件夹下的所有文件内容中的 ...