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.

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的更多相关文章

  1. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  2. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  3. Dancing Links [Kuangbin带你飞] 模版及题解

    学习资料: http://www.cnblogs.com/grenet/p/3145800.html http://blog.csdn.net/mu399/article/details/762786 ...

  4. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  5. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

  6. kuangbin带我飞QAQ DLX之一脸懵逼

    1. hust 1017 DLX精确覆盖 模板题 勉强写了注释,但还是一脸懵逼,感觉插入方式明显有问题但又不知道哪里不对而且好像能得出正确结果真是奇了怪了 #include <iostream& ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

随机推荐

  1. Apache Spark Tachyon的简介

    Tachyon是一个分布式内存文件系统,可以理解为内存中的HDFS. 为了提供更高的性能,将数据存储剥离Java Heap. 用户可以基于Tachyon实现RDD或者文件的跨应用共享,并提供高容错机制 ...

  2. HDU 4793 Collision (2013长沙现场赛,简单计算几何)

    Collision Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. Spring Autowiring by Type

    In Spring, "Autowiring by Type" means, if data type of a bean is compatible with the data ...

  4. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...

  5. linux中vi编辑器

    vi编辑器,通常称之为vi,是一种广泛存在于各种UNIX和Linux系 统中的文本编辑程序.它的功能十分强大,但是命令繁多,不容易掌握,它可以执行输出.删除.查找.替换.块操作等众多文本操作,而且用户 ...

  6. JQuery学习使用笔记 -- JQuery插件开发

    内容转载自 http://www.css88.com/archives/4821 扩展jQuery插件和方法的作用是非常强大的,它可以节省大量开发时间.这篇文章将概述jQuery插件开发的基本知识,最 ...

  7. 内存映射文件详解-----C++实现

    先不说内存映射文件是什么.贴个代码先,. #include <iostream> #include <fcntl.h> #include <io.h> #inclu ...

  8. Java NIO类库Selector机制解析(上)

    一.  前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但引入了全新的高效的I/O机制,同时,也引入了多路复用的异步模式.NIO的包中主要包含了这样几种抽象数据类型: ...

  9. freemaker遍历嵌套list的map

    <#if disMap?exists> <#list disMap?keys as key> <#if ((disMap[key]))??&&((disM ...

  10. 在自定义的dwt文件中调用page_header.lbi和page_footer.lbi

    昨天下午接到需求说要增加一个新的页面,作为优惠活动规则的介绍之用,之前对ecshop各种修改,但是这次自己做页面还是第一次,文件太多,函数也太多,一个一个的读过来时间很头疼的事情,于是就参照goods ...