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 ...
随机推荐
- caldera
Caldera International星期一宣布将公司名称变更为SCO Group,交易代码则改为SCOX,希望SCO可以在客户群当中建立更好的品牌认同. Caldera除了有自己的Linux版本 ...
- Java多线程编程模式实战指南:Active Object模式(下)
Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...
- Apache Spark Tachyon的简介
Tachyon是一个分布式内存文件系统,可以理解为内存中的HDFS. 为了提供更高的性能,将数据存储剥离Java Heap. 用户可以基于Tachyon实现RDD或者文件的跨应用共享,并提供高容错机制 ...
- ResolverService跨子网的广播问题
ResolverService在广播请求时,需要借助McastTransport,而McastTransport利用java.net.MulticastSocket进行收发,java.net.Mult ...
- BestCoder Round #67 (div.2) N bulbs(hdu 5600)
N bulbs Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 2063 过山车(二分匹配入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.ne ...
- HDU 4052 Adding New Machine (线段树+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4052 初始给你w*h的矩阵,给你n个矩形(互不相交),按这些矩形尺寸把初始的矩形扣掉,形成一个新的'矩 ...
- Base64.java 工具类
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- PHP实现基于Swoole简单的HTTP服务器
引用Swoole官方定义: PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTa ...
- weak nonatomic strong等介绍(ios)
@property的属性weak nonatomic strong等介绍(ios) 2014-12-02 18:06 676人阅读 评论(0) 收藏 举报 学习ios也已经快半个月了,也尝试做简单的应 ...