Sudoku
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 5769 | Accepted: 2684 |
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.
Write a Sudoku playing program that reads data sets from a text file.
Input
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.
Output
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--O- E--F-M--D--L-K-A -C--------O-I-L- H-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
Source
这个就是2676变了一下形
直接上代码了
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 5000000, INF = 0x7fffffff; int S[maxn], head[maxn], vis[maxn]; int U[maxn], D[maxn], L[maxn], R[maxn]; int C[maxn], X[maxn]; int n, m, ans, ret, ans1; void init() { for(int i = 0; i <= m; i++) D[i] = i, U[i] = i, R[i] = i + 1, L[i] = i - 1; L[0] = m, R[m] = 0; mem(S, 0), mem(head, -1); ans = m + 1; } void delc(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 resc(int c) { for(int i = U[c]; i != c; i = U[i]) for(int j = L[i]; j != i; j = L[j]) U[D[j]] = j, D[U[j]] = j, S[C[j]]++; L[R[c]] = c, R[L[c]] = c; } void add(int r, int c) { ans++, S[c]++, C[ans] = c, X[ans] = r; D[ans] = D[c]; U[ans] = c; U[D[c]] = ans; D[c] = ans; if(head[r] < 0) head[r] = L[ans] = R[ans] = ans; else L[ans] = head[r], R[ans] = R[head[r]],L[R[head[r]]] = ans, R[head[r]] = ans; } bool dfs(int sh) { if(!R[0]) { sort(vis, vis + 16 * 16); int cnt = 0; for(int i = 0; i < 16; i++) { for(int j = 0; j < 16; j++) { int num = vis[cnt++]; num=num - (i * 16 + j) * 16; printf("%c", 'A' + num - 1); // cout << 111 << endl; } printf("\n"); } printf("\n"); return true; } int c = R[0]; for(int i = R[0]; i; i = R[i]) if(S[c] > S[i]) c = i; delc(c); for(int i = D[c]; i != c; i = D[i]) { vis[sh] = X[i]; for(int j = R[i]; j != i; j = R[j]) delc(C[j]); if(dfs(sh + 1)) return true; for(int j = L[i]; j != i; j = L[j]) resc(C[j]); } resc(c); return false; } char str[20][20]; void build(int x, int y, int k) { ans1 = (x * 16 + y - 1) * 16 + k; add(ans1, x * 16 + k); add(ans1, 16 * 16 + (y - 1) * 16 + k); add(ans1, 16 * 16 * 3 + x * 16 + y); int block = (y - 1) / 4 * 4 + x / 4; add(ans1, 16 * 16 * 2 + block * 16 + k); } int main() { while(~scanf("%s", str[0])) { m = 16 * 16 * 4; init(); for(int i = 1; i < 16; i++) { rs(str[i]); } for(int i = 0; i < 16; i++) for(int j = 1; j <= 16; j++) { if(str[i][j - 1] == '-') for(int k = 1; k <= 16; k++) build(i, j, k); else build(i, j, str[i][j - 1] - ('A' - 1)); } dfs(0); } return 0; }