ZOJ 3122 Sudoku
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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.
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]] ++;
} }
ZOJ 3122 Sudoku的更多相关文章
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
- Dancing Links [Kuangbin带你飞] 模版及题解
学习资料: http://www.cnblogs.com/grenet/p/3145800.html http://blog.csdn.net/mu399/article/details/762786 ...
- [kuangbin带你飞]专题1-23题目清单总结
[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...
- ACM--[kuangbin带你飞]--专题1-23
专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...
- kuangbin带我飞QAQ DLX之一脸懵逼
1. hust 1017 DLX精确覆盖 模板题 勉强写了注释,但还是一脸懵逼,感觉插入方式明显有问题但又不知道哪里不对而且好像能得出正确结果真是奇了怪了 #include <iostream& ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
随机推荐
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- CodeForces 489D Unbearable Controversy of Being (搜索)
Unbearable Controversy of Being 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/B Descrip ...
- 159. Longest Substring with At Most Two Distinct Characters
最后更新 二刷 08-Jan-17 回头看了下一刷的,用的map,应该是int[256]的意思,后面没仔细看cuz whatever I was doing at that time.. wasnt ...
- Random的nextInt用法
因为想当然的认为Random类中nextInt()(注:不带参数),会产生伪随机的正整数,采用如下的方式生成0~99之间的随机数: Random random = new Random(); rand ...
- HTTP Header 简介
HTTP Header 简介 HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服 ...
- 使用Windbg和SoS扩展调试分析.NET程序
在博客堂的不是我舍不得 - High CPU in GC(都是+=惹的祸,为啥不用StringBuilder呢?). 不是我舍不得 - .NET里面的Out Of Memory 看到很多人在问如何分析 ...
- HTML第一天学习笔记
- android SoundPool播放音效
MediaPlayer的缺点: 资源占用量高,延时时间较长 不支持多个音效同一时候播放 SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音 ...
- 2015 NOIP day2 t2 信息传递 tarjan
信息传递 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.luogu.org/problem/show?pid=2661 Descrip ...
- Codeforces Round #277 (Div. 2) D. Valid Sets 暴力
D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...