ZOJ 3122 Sudoku

时间:2022-03-14 20:18:09
Sudoku

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

Appoint description: 
System Crawler  (2015-04-23)

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.

ZOJ 3122 Sudoku

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了,实在想不明白这个数字是怎么来的,已经发私信问了,问到之后更新。
 #include <iostream>
#include <cmath>
#include <cstdio>
using namespace std; const int N = ;
const int COL = N*N + N*N + N*N + N*N;
const int ROW = N*N*N;
const int SIZE = ;
const int HEAD = ;
short U[SIZE],D[SIZE],L[SIZE],R[SIZE],S[COL + ],C[SIZE],POS_C[SIZE],POS_R[SIZE];
int COUNT;
bool VIS_R[N + ][N + ],VIS_C[N + ][N + ],VIS_M[N + ][N + ];
char CH[SIZE];
char ANS[N * N + ][N * N + ];
struct Node
{
short r,c;
char ch;
}TEMP[N * N + ]; void ini(void);
void link(int,int,int,int,char,int,int);
bool dancing(int);
void remove(int);
void resume(int);
void debug(int);
int main(void)
{
char s[N + ][N + ];
int c_1,c_2,c_3,c_4;
int count = ; while(scanf(" %s",s[] + ) != EOF)
{
count ++;
if(count != )
puts("");
for(int i = ;i <= N;i ++)
scanf(" %s",s[i] + ); ini();
for(int i = ;i <= N;i ++)
for(int j = ;j <= N;j ++)
{
int k = s[i][j];
if(k >= 'A' && k <= 'Z')
{
int num = (int)sqrt(N);
VIS_R[i][k - 'A' + ] = VIS_C[j][k - 'A' + ] = true;
VIS_M[(i - ) / num * num + (j - ) / num + ][k - 'A' + ] = true;
c_1 = N * N * + (i - ) * N + k - 'A' + ;
c_2 = N * N * + (j - ) * N + k - 'A' + ;
c_3 = N * N * + ((i - ) / num * num + (j - ) / num) * N + k - 'A' + ;
c_4 = N * N * + (i - ) * N + j;
link(c_1,c_2,c_3,c_4,k,i,j);
}
}
for(int i = ;i <= N;i ++)
for(int j = ;j <= N;j ++)
{
if(s[i][j] >= 'A' && s[i][j] <= 'Z')
continue;
for(int k = ;k <= N;k ++)
{
int num = (int)sqrt(N);
if(VIS_R[i][k] || VIS_C[j][k] ||
VIS_M[(i - ) / num * num + (j - ) / num + ][k])
continue;
c_1 = N * N * + (i - ) * N + k;
c_2 = N * N * + (j - ) * N + k;
c_3 = N * N * + ((i - ) / num * num + (j - ) / num) * N + k;
c_4 = N * N * + (i - ) * N + j;
link(c_1,c_2,c_3,c_4,k - + 'A',i,j);
}
}
dancing();
} return ;
} void ini(void)
{
R[HEAD] = ;
L[HEAD] = COL;
for(int i = ;i <= COL;i ++)
{
L[i] = i - ;
R[i] = i + ;
U[i] = D[i] = C[i] = i;
S[i] = ;
}
R[COL] = HEAD; COUNT = COL + ;
fill(&VIS_R[][],&VIS_R[N + ][N + ],false);
fill(&VIS_C[][],&VIS_C[N + ][N + ],false);
fill(&VIS_M[][],&VIS_M[N + ][N + ],false);
} void link(int c_1,int c_2,int c_3,int c_4,char ch,int p_i,int p_j)
{
int first = COUNT;
int col;
for(int i = ;i < ;i ++)
{
switch(i)
{
case :col = c_1;break;
case :col = c_2;break;
case :col = c_3;break;
case :col = c_4;break;
}
L[COUNT] = COUNT - ;
R[COUNT] = COUNT + ;
U[COUNT] = U[col];
D[COUNT] = col; D[U[col]] = COUNT;
U[col] = COUNT;
C[COUNT] = col;
CH[COUNT] = ch;
POS_R[COUNT] = p_i;
POS_C[COUNT] = p_j;
S[col] ++;
COUNT ++;
}
L[first] = COUNT - ;
R[COUNT - ] = first;
} bool dancing(int k)
{
if(R[HEAD] == HEAD)
{
for(int i = ;i < k;i ++)
ANS[TEMP[i].r][TEMP[i].c] = TEMP[i].ch;
for(int i = ;i <= N;i ++)
{
for(int j = ;j <= N;j ++)
putchar(ANS[i][j]);
puts("");
}
return true;
} int c = R[HEAD];
for(int i = R[HEAD];i != HEAD;i = R[i])
if(S[i] < S[c])
c = i; remove(c);
for(int i = D[c];i != c;i = D[i])
{
TEMP[k].r = POS_R[i];
TEMP[k].c = POS_C[i];
TEMP[k].ch = CH[i];
for(int j = R[i];j != i;j = R[j])
remove(C[j]);
if(dancing(k + ))
return true;
for(int j = L[i];j != i;j = L[j])
resume(C[j]);
}
resume(c); return false;
} 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[C[j]] --;
}
} void resume(int c)
{
L[R[c]] = c;
R[L[c]] = c;
for(int i = D[c];i != c;i = D[i])
for(int j = L[i];j != i;j = L[j])
{
U[D[j]] = j;
D[U[j]] = j;
S[C[j]] ++;
} }