ZOJ 3122 Sudoku

Sudoku

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

Submit Status Practice ZOJ 3122

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了,实在想不明白这个数字是怎么来的,已经发私信问了,问到之后更新。
  1 #include <iostream>
  2 #include <cmath>
  3 #include <cstdio>
  4 using    namespace    std;
  5
  6 const    int    N = 16;
  7 const    int    COL = N*N + N*N + N*N + N*N;
  8 const    int    ROW = N*N*N;
  9 const    int    SIZE = 20000;
 10 const    int    HEAD = 0;
 11 short    U[SIZE],D[SIZE],L[SIZE],R[SIZE],S[COL + 10],C[SIZE],POS_C[SIZE],POS_R[SIZE];
 12 int    COUNT;
 13 bool    VIS_R[N + 5][N + 5],VIS_C[N + 5][N + 5],VIS_M[N + 5][N + 5];
 14 char    CH[SIZE];
 15 char    ANS[N * N + 10][N * N + 10];
 16 struct    Node
 17 {
 18     short    r,c;
 19     char    ch;
 20 }TEMP[N * N + 10];
 21
 22 void    ini(void);
 23 void    link(int,int,int,int,char,int,int);
 24 bool    dancing(int);
 25 void    remove(int);
 26 void    resume(int);
 27 void    debug(int);
 28 int    main(void)
 29 {
 30     char    s[N + 10][N + 10];
 31     int    c_1,c_2,c_3,c_4;
 32     int    count = 0;
 33
 34     while(scanf(" %s",s[1] + 1) != EOF)
 35     {
 36         count ++;
 37         if(count != 1)
 38             puts("");
 39         for(int i = 2;i <= N;i ++)
 40             scanf(" %s",s[i] + 1);
 41
 42         ini();
 43         for(int i = 1;i <= N;i ++)
 44             for(int j = 1;j <= N;j ++)
 45             {
 46                 int    k = s[i][j];
 47                 if(k >= ‘A‘ && k <= ‘Z‘)
 48                 {
 49                     int    num =  (int)sqrt(N);
 50                     VIS_R[i][k - ‘A‘ + 1] = VIS_C[j][k - ‘A‘ + 1] = true;
 51                     VIS_M[(i - 1) / num * num + (j - 1) / num + 1][k - ‘A‘ + 1] = true;
 52                     c_1 = N * N * 0 + (i - 1) * N + k - ‘A‘ + 1;
 53                     c_2 = N * N * 1 + (j - 1) * N + k - ‘A‘ + 1;
 54                     c_3 = N * N * 2 + ((i - 1) / num * num + (j - 1) / num) * N + k - ‘A‘ + 1;
 55                     c_4 = N * N * 3 + (i - 1) * N + j;
 56                     link(c_1,c_2,c_3,c_4,k,i,j);
 57                 }
 58             }
 59         for(int i = 1;i <= N;i ++)
 60             for(int j = 1;j <= N;j ++)
 61             {
 62                 if(s[i][j] >= ‘A‘ && s[i][j] <= ‘Z‘)
 63                     continue;
 64                 for(int k = 1;k <= N;k ++)
 65                 {
 66                     int    num =  (int)sqrt(N);
 67                     if(VIS_R[i][k] || VIS_C[j][k] ||
 68                        VIS_M[(i - 1) / num * num + (j - 1) / num + 1][k])
 69                         continue;
 70                     c_1 = N * N * 0 + (i - 1) * N + k;
 71                     c_2 = N * N * 1 + (j - 1) * N + k;
 72                     c_3 = N * N * 2 + ((i - 1) / num * num + (j - 1) / num) * N + k;
 73                     c_4 = N * N * 3 + (i - 1) * N + j;
 74                     link(c_1,c_2,c_3,c_4,k - 1 + ‘A‘,i,j);
 75                 }
 76             }
 77         dancing(0);
 78     }
 79
 80     return    0;
 81 }
 82
 83 void    ini(void)
 84 {
 85     R[HEAD] = 1;
 86     L[HEAD] = COL;
 87     for(int i = 1;i <= COL;i ++)
 88     {
 89         L[i] = i - 1;
 90         R[i] = i + 1;
 91         U[i] = D[i] = C[i] = i;
 92         S[i] = 0;
 93     }
 94     R[COL] = HEAD;
 95
 96     COUNT = COL + 1;
 97     fill(&VIS_R[0][0],&VIS_R[N + 4][N + 4],false);
 98     fill(&VIS_C[0][0],&VIS_C[N + 4][N + 4],false);
 99     fill(&VIS_M[0][0],&VIS_M[N + 4][N + 4],false);
100 }
101
102 void    link(int c_1,int c_2,int c_3,int c_4,char ch,int p_i,int p_j)
103 {
104     int    first = COUNT;
105     int    col;
106     for(int i = 0;i < 4;i ++)
107     {
108         switch(i)
109         {
110             case    0:col = c_1;break;
111             case    1:col = c_2;break;
112             case    2:col = c_3;break;
113             case    3:col = c_4;break;
114         }
115         L[COUNT] = COUNT - 1;
116         R[COUNT] = COUNT + 1;
117         U[COUNT] = U[col];
118         D[COUNT] = col;
119
120         D[U[col]] = COUNT;
121         U[col] = COUNT;
122         C[COUNT] = col;
123         CH[COUNT] = ch;
124         POS_R[COUNT] = p_i;
125         POS_C[COUNT] = p_j;
126         S[col] ++;
127         COUNT ++;
128     }
129     L[first] = COUNT - 1;
130     R[COUNT - 1] = first;
131 }
132
133 bool    dancing(int k)
134 {
135     if(R[HEAD] == HEAD)
136     {
137         for(int i = 0;i < k;i ++)
138             ANS[TEMP[i].r][TEMP[i].c] = TEMP[i].ch;
139         for(int i = 1;i <= N;i ++)
140         {
141             for(int j = 1;j <= N;j ++)
142                 putchar(ANS[i][j]);
143             puts("");
144         }
145         return    true;
146     }
147
148     int    c = R[HEAD];
149     for(int i = R[HEAD];i != HEAD;i = R[i])
150         if(S[i] < S[c])
151             c = i;
152
153     remove(c);
154     for(int i = D[c];i != c;i = D[i])
155     {
156         TEMP[k].r = POS_R[i];
157         TEMP[k].c = POS_C[i];
158         TEMP[k].ch = CH[i];
159         for(int j = R[i];j != i;j = R[j])
160             remove(C[j]);
161         if(dancing(k + 1))
162             return    true;
163         for(int j = L[i];j != i;j = L[j])
164             resume(C[j]);
165     }
166     resume(c);
167
168     return    false;
169 }
170
171 void    remove(int c)
172 {
173     L[R[c]] = L[c];
174     R[L[c]] = R[c];
175     for(int i = D[c];i != c;i = D[i])
176         for(int j = R[i];j != i;j = R[j])
177         {
178             U[D[j]] = U[j];
179             D[U[j]] = D[j];
180             S[C[j]] --;
181         }
182 }
183
184 void    resume(int c)
185 {
186     L[R[c]] = c;
187     R[L[c]] = c;
188     for(int i = D[c];i != c;i = D[i])
189         for(int j = L[i];j != i;j = L[j])
190         {
191             U[D[j]] = j;
192             D[U[j]] = j;
193             S[C[j]] ++;
194         }
195
196 }
时间: 2024-11-10 01:37:36

ZOJ 3122 Sudoku的相关文章

LA 2659 &amp;&amp; poj 3076 &amp;&amp; zoj 3122 Sudoku(精确覆盖 + DLX)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=660 TimeLimit: 3.000 seconds A Sudoku grid is a 16 x 16 grid of cells grouped in sixteen 4 x 4 squares, where some cells are filled wit

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

学习资料: http://www.cnblogs.com/grenet/p/3145800.html http://blog.csdn.net/mu399/article/details/7627862 2份模版 第一份精确覆盖 from POJ 3074 const int N = 9; const int MAXN = N * N * N + 10; const int MAXM = N * N * 4 + 10; const int MAXNODE = MAXN * 4 + MAXM +

CSP2019突击训练(搜索专题)

专题一 简单搜索POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find The MultiplePOJ 3126 Prime PathPOJ 3087 Shuffle'm UpPOJ 3414 PotsFZU 2150 Fire GameUVA 11624 Fire!POJ 3984 迷宫问题HDU 1241 Oil DepositsHDU 1495 非常可乐HDU 261

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

LeetCode37 Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red.  (Hard)

*Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. public clas

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partially

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <