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 ...
随机推荐
- Gym 100507D Zhenya moves from the dormitory (模拟)
Zhenya moves from the dormitory 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/D Descrip ...
- [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画
A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮 code s ...
- opencv 彩色图像亮度、对比度调节 直方图均衡化
直接上代码: #include <Windows.h> #include <iostream>// for stand I/O #include <string> ...
- String(byte[] bytes, String charsetName)
String str = new String("时之沙"); byte bytes[] = str.getBytes("GBK"); byte byte2[] ...
- 设计模式-结合Android代码
开始学设计模式 1 单例模式 单例模式可以说是最容易理解的模式了,也是应用最广的模式之一,先看看定义吧. 定义:确保单例类只有一个实例,并且这个单例类提供一个函数接口让其他类获取到这个唯一的实例. 什 ...
- CentOS6.5安装图形界面
转载自http://www.cnblogs.com/zydev/p/5128788.html 一.使用网络安装(如果网络比较快,这个方法简单) yum groupinstall "Deskt ...
- Android学习第一课
首先看一个android项目中各个包的作用 以下看几个经常使用的控件: 1. TextView 显示文本框控件 2. EditText 输入文本框 TextView控件经常使用属性: id----控件 ...
- search result
https://github.com/search?l=java&p=86&q=Floating+window&type=Code&utf8=%E2%9C%93http ...
- 在安卓开发中使用SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- [Javascript] Manipulate the DOM with the classList API
Learn how to add, remove and test for CSS classes using the classList API. It's more powerful than u ...