poj 3074 Sudoku
来源:互联网 发布:python rawurlencode 编辑:程序博客网 时间:2024/05/20 05:24
题意:解出9*9数独
思路:和poj 3076一样,只不过编码函数有点不同,依然使用舞蹈链
题目链接:http://poj.org/problem?id=3074
#include<cstdio>#include<vector>#include<cstring>using namespace std;const int maxn=400;const int maxnode=3000;const int maxr=1000;struct DLX{ int n,sz; int s[maxn]; int row[maxnode],col[maxnode]; int r[maxnode],l[maxnode],u[maxnode],d[maxnode]; int ansd,ans[maxr]; void init(int n) { this->n=n; for(int i=0;i<=n;i++) { l[i]=i-1,r[i]=i+1,u[i]=d[i]=i; } l[0]=n,r[n]=0; sz=n+1; memset(s,0,sizeof(s)); } void addrow(int R,const vector<int> &v) { int first=sz; for(int i=0;i<(int)v.size();i++) { int c=v[i]; l[sz]=sz-1,r[sz]=sz+1,d[sz]=c,u[sz]=u[c]; d[u[c]]=sz,u[c]=sz; row[sz]=R,col[sz]=c; s[c]++;sz++; } r[sz-1]=first;l[first]=sz-1; } 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 Restore(int c) { for(int i=u[c];i!=c;i=u[i]) { for(int j=l[i];j!=i;j=l[j]) { ++s[col[j]]; u[d[j]]=j,d[u[j]]=j; } } l[r[c]]=c; r[l[c]]=c; } bool dfs(int cur) { if(r[0]==0) { ansd=cur; return true; } int c=r[0]; for(int i=r[0];i!=0;i=r[i]) if(s[i]<s[c]) c=i; Remove(c); for(int i=d[c];i!=c;i=d[i]) { ans[cur]=row[i]; for(int j=r[i];j!=i;j=r[j]) Remove(col[j]); if(dfs(cur+1)) return true; for(int j=l[i];j!=i;j=l[j]) Restore(col[j]); } Restore(c); return false; } bool solve(vector<int> &v) { v.clear(); if(!dfs(0)) return false; for(int i=0;i<ansd;i++) v.push_back(ans[i]); return true; }};DLX dlx;const int slot=0;const int row=1;const int col=2;const int sub=3;int encode(int a,int b,int c){ return a*81+b*9+c+1;}void decode(int code,int &a,int &b,int &c){ code--; c=code%9,code/=9; b=code%9,code/=9; a=code;}char s[100],a[10][10];int main(void){ vector<int> v1; while(scanf("%s",s),s[0]!='e') { dlx.init(324); int cnt=0; for(int i=0;i<9;i++) { for(int j=0;j<9;j++) a[i][j]=s[cnt++]; a[i][9]='\0'; } for(int r=0;r<9;r++) { for(int c=0;c<9;c++) { for(int v=0;v<9;v++) { if(a[r][c]=='.'||a[r][c]=='1'+v) { v1.clear(); v1.push_back(encode(slot,r,c)); v1.push_back(encode(row,r,v)); v1.push_back(encode(col,c,v)); v1.push_back(encode(sub,(r/3)*3+c/3,v)); dlx.addrow(encode(r,c,v),v1); } } } } v1.clear(); dlx.solve(v1); int r,c,v; for(int i=0;i<(int)v1.size();i++) { decode(v1[i],r,c,v); a[r][c]='1'+v; } for(int i=0;i<9;i++) printf("%s",a[i]); printf("\n"); } return 0;}
0 0
- poj 3074 Sudoku
- POJ 3074 Sudoku DLX
- POJ 3074 Sudoku
- poj 3074 Sudoku
- poj 3074 Sudoku
- POJ 3074 Sudoku
- poj 3074 Sudoku
- Poj 3074: Sudoku
- POJ 3074 Sudoku 笔记
- POJ 3074 Sudoku 笔记
- POJ 3074 Sudoku(数据结构,DLX)
- poj 3074 Sudoku(Dancing Links)
- POJ 3074 Sudoku 舞蹈链
- POJ 3074 Sudoku [DLX] [SDLX]
- poj Sudoku
- Sudoku POJ
- POJ 3074 Sudoku (Dancing Links)
- poj 3074 Sudoku (精确覆盖,DLX,搜索)
- java serializable 序列化
- JQuery中的 ajax 简单应用
- 《如何阅读一本书》读书笔记
- Struts2 数据分页
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- poj 3074 Sudoku
- kafka深入理解
- opencv内存管理器2-cvSeqPartition聚类
- Linux实用工具(一)
- POJ 3279-Fliptile(母牛翻方格-开关问题)
- 已知平面三点坐标求其中两条边之间的夹脚
- 第16周阅读程序3
- Android中MediaMuxer和MediaCodec用例 - audio+video
- Spring的核心