A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to P such that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.
Write a Sudoku playing program that reads data sets from a text file. Each data set encodes a grid and contains 16 strings on 16 consecutive lines as shown in figure 2. The i th string stands for the i th line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,...,P,-}, where - (minus) designates empty grid cells. The data sets are separated by single empty lines and terminate with an end of file. The program prints the solution of the input encoded grids in the same format and order as used for input.
Sample Input
--A----C-----O-I -J--A-B-P-CGF-H- --D--F-I-E----P- -G-EL-H----M-J-- ----E----C--G--- -I--K-GA-B---E-J D-GP--J-F----A-- -E---C-B--DP--OE-- F-M--D--L-K-A -C--------O-I-LH- P-C--F-A--B--- ---G-OD---J----H K---J----H-A-P-L --B--P--E--K--A- -H--B--K--FI-C-- --F---C--D--H-N-
Sample Output
FPAHMJECNLBDKOGI OJMIANBDPKCGFLHE LNDKGFOIJEAHMBPC BGCELKHPOFIMAJDN MFHBELPOACKJGNID CILNKDGAHBMOPEFJ DOGPIHJMFNLECAKB JEKAFCNBGIDPLHOM EBOFPMIJDGHLNKCA NCJDHBAEKMOFIGLP HMPLCGKFIAENBDJO AKIGNODLBPJCEFMH KDEMJIFNCHGAOPBL GLBCDPMHEONKJIAF PHNOBALKMJFIDCEG IAFJOECGLDPBHMNK
翻译:
Sudoku网格是由16x16个单元格组成的网格,由16个4x4方格组成。单元格中填满了A到P(英文字母表前16个大写字母)的字母,如图1a所示。游戏是用A到P的字母填充所有空的网格单元格,这样来自网格的每个字母只在它所占用的行、列和4x4网格中出现一次。网格的初始内容满足上述约束条件,并保证了唯一的解决方案。
编写一个数独播放程序,从文本文件中读取数据集。每个数据集编码一个网格,并包含16条连续行上的16个字符串,如图2所示。第i个字符串代表网格的第i行,长度为16个字符,从行的第一个位置开始。字符串字符来自集合{A,B,.,P,-},其中-(减号)指定空网格单元格。数据集由单个空行分隔,并以文件结尾结束。该程序以与输入相同的格式和顺序打印输入编码网格的解决方案。
思路:类似与9*9的数独,不过例题输入有点坑。9*9数独链接:https://www.cnblogs.com/mzchuan/p/11434887.html
代码:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 1000000007 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 const int maxn=18000; 22 int ans[maxn]; 23 struct DLX 24 { 25 int n,id; 26 int L[maxn],R[maxn],U[maxn],D[maxn]; 27 int C[maxn],S[1030],loc[maxn][3];//C代表列,S代表每列有的数字数量,loc代表这个数在数独中的位置和数值 28 int H[maxn]; 29 void init(int nn) 30 { 31 n=nn; 32 for(int i=0;i<=n;i++) 33 { 34 U[i]=D[i]=i; 35 L[i]=i-1; 36 R[i]=i+1; 37 } 38 L[0]=n; R[n]=0; 39 id=n; 40 memset(S,0,sizeof(S)); 41 memset(H,-1,sizeof(H)); 42 } 43 void Link(int x,int y,int px,int py,int k) 44 { 45 ++id; 46 D[id]=y; U[id]=U[y]; 47 D[U[y]]=id; U[y]=id; 48 loc[id][0]=px,loc[id][1]=py,loc[id][2]=k;//存放数的位置和数 49 C[id]=y; 50 S[y]++;//此列1的数量加一 51 if(H[x]==-1) H[x]=L[id]=R[id]=id; 52 else 53 { 54 int a=H[x]; 55 int b=R[a]; 56 L[id]=a; R[a]=id; 57 R[id]=b; L[b]=id; 58 H[x]=id; 59 } 60 } 61 void Remove(int c) 62 { 63 L[R[c]]=L[c]; 64 R[L[c]]=R[c]; 65 for(int i=D[c];i!=c;i=D[i]) 66 for(int j=R[i];j!=i;j=R[j]) 67 { 68 U[D[j]]=U[j]; 69 D[U[j]]=D[j]; 70 S[C[j]]--; 71 } 72 } 73 void Resume(int c) 74 { 75 for(int i=U[c];i!=c;i=U[i]) 76 for(int j=R[i];j!=i;j=R[j]) 77 { 78 S[C[j]]++; 79 U[D[j]]=j; 80 D[U[j]]=j; 81 } 82 L[R[c]]=c; 83 R[L[c]]=c; 84 } 85 bool dfs(int step) 86 { 87 if(step==256) return true; 88 if(R[0]==0) return false; 89 int c=R[0]; 90 for(int i=R[0];i;i=R[i])//优先循环1的数量少的一列 91 { 92 if(S[i]<S[c]) 93 { 94 c=i; 95 } 96 } 97 Remove(c); 98 for(int i=D[c];i!=c;i=D[i]) 99 { 100 ans[step]=i; 101 for(int j=R[i];j!=i;j=R[j]) Remove(C[j]); 102 if(dfs(step+1)) return true; 103 for(int j=L[i];j!=i;j=L[j]) Resume(C[j]); 104 } 105 Resume(c); 106 return false; 107 } 108 }dlx; 109 int main() 110 { 111 char str[260]; 112 int kase=0; 113 while(cin>>str) 114 { 115 if(kase) 116 { 117 cout<<endl; 118 } 119 kase++; 120 for(int i=1;i<=15;i++) 121 { 122 cin>>str+i*16; 123 } 124 dlx.init(256*4); 125 int r=0,js=0;//r代表行 126 for(int x=0;x<16;x++) 127 { for(int y=0;y<16;y++) 128 { 129 char ch=str[js]; 130 js++; 131 int s=(x/4)*4+y/4;//宫 132 int a,b,c,d;//a表示约束一,b表示约束二,c表示约束三,d表示约束四 133 if(ch==‘-‘) 134 { 135 for(int i=1;i<=16;i++) 136 { 137 a=x*16+y+1; 138 b=x*16+i+256; 139 c=y*16+i+256+256; 140 d=s*16+i+256+256+256; 141 ++r; 142 dlx.Link(r,a,x,y,i); 143 dlx.Link(r,b,x,y,i); 144 dlx.Link(r,c,x,y,i); 145 dlx.Link(r,d,x,y,i); 146 } 147 } 148 else 149 { 150 int i=ch-64; 151 a=x*16+y+1; 152 b=x*16+i+256; 153 c=y*16+i+256+256; 154 d=s*16+i+256+256+256; 155 ++r; 156 dlx.Link(r,a,x,y,i); 157 dlx.Link(r,b,x,y,i); 158 dlx.Link(r,c,x,y,i); 159 dlx.Link(r,d,x,y,i); 160 } 161 } 162 } 163 dlx.dfs(0); 164 char res[16][16]; 165 for(int i=0;i<256;i++)//将答案存放到一个数独数组中 166 { 167 int a=ans[i]; 168 int x=dlx.loc[a][0],y=dlx.loc[a][1],k=dlx.loc[a][2]-1; 169 res[x][y]=k+‘A‘; 170 } 171 for(int i=0;i<16;i++) 172 { 173 for(int j=0;j<16;j++) 174 { 175 printf("%c",res[i][j]); 176 } 177 printf("\n"); 178 } 179 } 180 181 }
原文:https://www.cnblogs.com/mzchuan/p/11438173.html