ZOJ 3122 Sudoku

Sudoku
Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

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



16*16的数独,和前面那个没什么区别,不过这题略奇葩。。首先给出的输入样例格式是错的,然后题目说输入数据每组之间被空行隔开,结果那个空行居然要自己输出。。。我PE了好几发。
有个不明白的地方就是,按照我的理解最大节点数应该是最大行数*最大列数才对,但是这题这样开的话会MLE,后来在网上看到了一个很奇怪的数字,改成它就A了,实在想不明白这个数字是怎么来的,已经发私信问了,问到之后更新。
  1 #include <iostream>
  2 #include <cmath>
  3 #include <cstdio>
  4 using    namespace    std;
  5 
  6 const    int    N = 16;
  7 const    int    COL = N*N + N*N + N*N + N*N;
  8 const    int    ROW = N*N*N;
  9 const    int    SIZE = 20000;
 10 const    int    HEAD = 0;
 11 short    U[SIZE],D[SIZE],L[SIZE],R[SIZE],S[COL + 10],C[SIZE],POS_C[SIZE],POS_R[SIZE];
 12 int    COUNT;
 13 bool    VIS_R[N + 5][N + 5],VIS_C[N + 5][N + 5],VIS_M[N + 5][N + 5];
 14 char    CH[SIZE];
 15 char    ANS[N * N + 10][N * N + 10];
 16 struct    Node
 17 {
 18     short    r,c;
 19     char    ch;
 20 }TEMP[N * N + 10];
 21 
 22 void    ini(void);
 23 void    link(int,int,int,int,char,int,int);
 24 bool    dancing(int);
 25 void    remove(int);
 26 void    resume(int);
 27 void    debug(int);
 28 int    main(void)
 29 {
 30     char    s[N + 10][N + 10];
 31     int    c_1,c_2,c_3,c_4;
 32     int    count = 0;
 33     
 34     while(scanf(" %s",s[1] + 1) != EOF)
 35     {
 36         count ++;
 37         if(count != 1)
 38             puts("");
 39         for(int i = 2;i <= N;i ++)
 40             scanf(" %s",s[i] + 1);
 41     
 42         ini();
 43         for(int i = 1;i <= N;i ++)
 44             for(int j = 1;j <= N;j ++)
 45             {
 46                 int    k = s[i][j];
 47                 if(k >= 'A' && k <= 'Z')
 48                 {
 49                     int    num =  (int)sqrt(N);
 50                     VIS_R[i][k - 'A' + 1] = VIS_C[j][k - 'A' + 1] = true;
 51                     VIS_M[(i - 1) / num * num + (j - 1) / num + 1][k - 'A' + 1] = true;
 52                     c_1 = N * N * 0 + (i - 1) * N + k - 'A' + 1;
 53                     c_2 = N * N * 1 + (j - 1) * N + k - 'A' + 1;
 54                     c_3 = N * N * 2 + ((i - 1) / num * num + (j - 1) / num) * N + k - 'A' + 1;
 55                     c_4 = N * N * 3 + (i - 1) * N + j;
 56                     link(c_1,c_2,c_3,c_4,k,i,j);
 57                 }
 58             }
 59         for(int i = 1;i <= N;i ++)
 60             for(int j = 1;j <= N;j ++)
 61             {
 62                 if(s[i][j] >= 'A' && s[i][j] <= 'Z')
 63                     continue;
 64                 for(int k = 1;k <= N;k ++)
 65                 {
 66                     int    num =  (int)sqrt(N);
 67                     if(VIS_R[i][k] || VIS_C[j][k] ||
 68                        VIS_M[(i - 1) / num * num + (j - 1) / num + 1][k])
 69                         continue;
 70                     c_1 = N * N * 0 + (i - 1) * N + k;
 71                     c_2 = N * N * 1 + (j - 1) * N + k;
 72                     c_3 = N * N * 2 + ((i - 1) / num * num + (j - 1) / num) * N + k;
 73                     c_4 = N * N * 3 + (i - 1) * N + j;
 74                     link(c_1,c_2,c_3,c_4,k - 1 + 'A',i,j);
 75                 }
 76             }
 77         dancing(0);
 78     }
 79 
 80     return    0;
 81 }
 82 
 83 void    ini(void)
 84 {
 85     R[HEAD] = 1;
 86     L[HEAD] = COL;
 87     for(int i = 1;i <= COL;i ++)
 88     {
 89         L[i] = i - 1;
 90         R[i] = i + 1;
 91         U[i] = D[i] = C[i] = i;
 92         S[i] = 0;
 93     }
 94     R[COL] = HEAD;
 95 
 96     COUNT = COL + 1;
 97     fill(&VIS_R[0][0],&VIS_R[N + 4][N + 4],false);
 98     fill(&VIS_C[0][0],&VIS_C[N + 4][N + 4],false);
 99     fill(&VIS_M[0][0],&VIS_M[N + 4][N + 4],false);
100 }
101 
102 void    link(int c_1,int c_2,int c_3,int c_4,char ch,int p_i,int p_j)
103 {
104     int    first = COUNT;
105     int    col;
106     for(int i = 0;i < 4;i ++)
107     {
108         switch(i)
109         {
110             case    0:col = c_1;break;
111             case    1:col = c_2;break;
112             case    2:col = c_3;break;
113             case    3:col = c_4;break;
114         }
115         L[COUNT] = COUNT - 1;
116         R[COUNT] = COUNT + 1;
117         U[COUNT] = U[col];
118         D[COUNT] = col;
119 
120         D[U[col]] = COUNT;
121         U[col] = COUNT;
122         C[COUNT] = col;
123         CH[COUNT] = ch;
124         POS_R[COUNT] = p_i;
125         POS_C[COUNT] = p_j;
126         S[col] ++;
127         COUNT ++;
128     }
129     L[first] = COUNT - 1;
130     R[COUNT - 1] = first;
131 }
132 
133 bool    dancing(int k)
134 {
135     if(R[HEAD] == HEAD)
136     {
137         for(int i = 0;i < k;i ++)
138             ANS[TEMP[i].r][TEMP[i].c] = TEMP[i].ch;
139         for(int i = 1;i <= N;i ++)
140         {
141             for(int j = 1;j <= N;j ++)
142                 putchar(ANS[i][j]);
143             puts("");
144         }
145         return    true;
146     }
147 
148     int    c = R[HEAD];
149     for(int i = R[HEAD];i != HEAD;i = R[i])
150         if(S[i] < S[c])
151             c = i;
152 
153     remove(c);
154     for(int i = D[c];i != c;i = D[i])
155     {
156         TEMP[k].r = POS_R[i];
157         TEMP[k].c = POS_C[i];
158         TEMP[k].ch = CH[i];
159         for(int j = R[i];j != i;j = R[j])
160             remove(C[j]);
161         if(dancing(k + 1))
162             return    true;
163         for(int j = L[i];j != i;j = L[j])
164             resume(C[j]);
165     }
166     resume(c);
167 
168     return    false;
169 }
170 
171 void    remove(int c)
172 {
173     L[R[c]] = L[c];
174     R[L[c]] = R[c];
175     for(int i = D[c];i != c;i = D[i])
176         for(int j = R[i];j != i;j = R[j])
177         {
178             U[D[j]] = U[j];
179             D[U[j]] = D[j];
180             S[C[j]] --;
181         }
182 }
183 
184 void    resume(int c)
185 {
186     L[R[c]] = c;
187     R[L[c]] = c;
188     for(int i = D[c];i != c;i = D[i])
189         for(int j = L[i];j != i;j = L[j])
190         {
191             U[D[j]] = j;
192             D[U[j]] = j;
193             S[C[j]] ++;
194         }
195 
196 }
原文地址:https://www.cnblogs.com/xz816111/p/4451193.html