[DLX+bfs] hdu 4069 Squiggly Sudoku
题意:
给你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的更多相关文章
- 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 ...
- (中等) 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 ...
- [DLX]HDOJ4069 Squiggly Sudoku
题意:有9*9的格子 每个格子 由五部分组成:上(16).右(32).下(64).左(128).和该格的数值(0~9) 若上下左右有分割格子的线 就加上相应的数, 该格的数值若为0,则是未知 1~9 ...
- hdu 4069 福州赛区网络赛I DLC ***
再遇到一个DLC就刷个专题 #include <stdio.h> #include <string.h> #include <iostream> #include ...
- 搜索(DLX): POJ 3074 3076 Sudoku
POJ 3074 : Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...
- (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 ...
- hdu 4069 垃圾数独
首先dfs给每个格子分一个大的区块 其次套板子就a 我一开始直接在选取行的时候填数独,发现超时 我这一行也就4个元素,找到 x <= 81 的列计算元素位置,81 < x <= 16 ...
随机推荐
- webpack的详细介绍和使用
// 一个常见的`webpack`配置文件 const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-we ...
- vue-cli 中使用less
(1)安装Less模块: npm install less (2)安装less和less-loader,命令如下 npm install less less-loader --sava-dev (3) ...
- 08C#事件
C#事件 1.2 事件 事件是C#语言内置的语法,可以定义和处理事件,为使用组件编程提供了良好的基础. 1.16.1 事件驱动 Windows操作系统把用户的动作都看作消息,C# ...
- JavaSE-13 内部类
学习要点 内部类的定义 内部类的应用 内部类 定义 Java的一个类中包含着另一类. A类和B类是C类的外部类.B类是C类的外部类.A类也称为顶层类. 如何使用内部类 public class MyF ...
- python circle nested
#!/usr/bin/python # -*- coding:utf- -*- # @filename: tmp2 # @author:vickey # @date: // : def circle_ ...
- MySQL-----备份(转储)
备份: **备份数据表结构+数据** mysqldump -u root 要备份的数据库表名 > 要备份的数据的备份名(这里也可以指定路径) -p **备份数据表结构** mysqldump - ...
- BNUOJ 7697 Information Disturbing
Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. ...
- Leetcode 211.添加与搜索单词
添加与搜索单词 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字 ...
- Spark 动态(统一)内存管理模型
作者编辑:王玮,胡玉林 一.回顾 在前面的一篇文章中我们介绍了spark静态内存管理模式以及相关知识https://blog.csdn.net/anitinaj/article/details/809 ...
- 总结:常用的Linux系统监控命令(2)
判断I/O瓶颈 mpstat命令 命令:mpstat -P ALL 1 1000 结果显示: 注意一下这里面的%iowait列,CPU等待I/O操作所花费的时间.这个值持续很高通常可能是I/O瓶颈所导 ...