本文主要是介绍HDU 4069 Squiggly Sudoku DLX,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这是昨天周赛的题,我竟然不会怎么判断多解,后来一google,卧槽,我想复杂了。。。。。。直接看能搜出来几次就行了。
这题就是个变形,先floodfill一下,然后就是模板了
然后发现比大华的快了好几倍,然后加了输入挂后,瞬间成Best solution中的rank1了
/*
ID: sdj22251
PROG: inflate
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 9*9*9*9
#define INF 1000000000
#define N 9
#define M 9*9*9+5
using namespace std;
int L[MAXN], R[MAXN], C[MAXN], S[M], U[MAXN], D[MAXN], H[M], O[M], X[MAXN]; //X用来存储行,C用来存储列,H用来存储每行中的第一个结点,O用来存储结果
int cnt, head;
char mp[N + 5][N + 5], ans[N * N + 5];
bool vis[N * N * 4 + 5];
int ditu[N + 5][N + 5], pos[N * 5][N * 5];
int num, ct;
void link(int r, int c)
{S[c]++;C[cnt] = c;X[cnt] = r;U[cnt] = c;D[cnt] = D[c];U[D[c]] = cnt;D[c] = cnt;if(H[r] == -1){H[r] = cnt;L[cnt] = R[cnt] = cnt;}else{L[cnt] = H[r];R[cnt] = R[H[r]];L[R[H[r]]] = cnt;R[H[r]] = cnt;}cnt++;
}
void init()
{cnt = 0;head = 0;num = 0;ct = 0;for(int i = 0; i <= N * N * 4; i++){S[i] = 0;vis[i] = 0;D[i] = U[i] = i;R[i] = (i + 1) % (N * N * 4 + 1);L[i] = (i + N * N * 4) % (N * N * 4 + 1);cnt++;}memset(H, -1, sizeof(H));
}
void cal(int &r, int &cx, int &cy, int &ck, int &cg, int i, int j, int k)
{r = (i * N + j) * N + k - 1; //代表所属的行cg = i * N + j + 1; //代表的是数独中i,j位置所属的列cx = N * N + i * N + k; //代表数独中同一行所属的列cy = N * N * 2 + j * N + k; //代表数独中同一列所属的列ck = N * N * 3 + (pos[i][j] - 1) * N + k;//代表数独中同一宫所属的列
}
void search(int x, int y)
{pos[x][y] = ct;int ta = ditu[x][y];if(ta & 16) ta ^= 16;if(ta & 32) ta ^= 32;if(ta & 64) ta ^= 64;if(ta & 128) ta ^= 128;mp[x][y] = ta + '0';ta = ditu[x][y];if((ta & 16) == 0 && x >= 1 && pos[x - 1][y] == 0) search(x - 1, y);if((ta & 32) == 0 && y < N - 1 && pos[x][y + 1] == 0) search(x, y + 1);if((ta & 64) == 0 && x < N - 1 && pos[x + 1][y] == 0) search(x + 1, y);if((ta & 128) == 0 && y >= 1 && pos[x][y - 1] == 0) search(x, y - 1);
}
void readdata()
{int r, cx, cy, ck, cg;memset(pos, 0, sizeof(pos));for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)scanf("%d", &ditu[i][j]);for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)if(pos[i][j] == 0){++ct;search(i, j);}for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)if(mp[i][j] != '0'){cal(r, cx, cy, ck, cg, i, j, mp[i][j] - '0');link(r, cx);link(r, cy);link(r, ck);link(r, cg);vis[cx] = vis[cy] = vis[ck] = vis[cg] = 1;}for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)for(int k = 1; k <= N; k++){cal(r, cx, cy, ck, cg, i, j, k);if(vis[cx] || vis[cy] || vis[ck] || vis[cg]) continue;link(r, cx);link(r, cy);link(r, ck);link(r, cg);}
}
void removes(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 resumes(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;
}
bool dfs(int k)
{if(R[head] == head){num++; //直接用num记录有多少个解for(int i = 0; i < k; i++)ans[X[O[i]] / N] = X[O[i]] % 9 + '1';if(num >= 2) return true;else return false;}int s = INF, c;for(int i = R[head]; i != head; i = R[i])if(s > S[i]){s = S[i];c = i;}removes(c);for(int i = U[c]; i != c; i = U[i]){O[k] = i;for(int j = R[i]; j != i; j = R[j])removes(C[j]);if(dfs(k + 1)) return true;for(int j = L[i]; j != i; j = L[j])resumes(C[j]);}resumes(c);return false;
}
int main()
{int T, cas = 0;scanf("%d", &T);while(T--){init();readdata();dfs(0);printf("Case %d:\n", ++cas);if(num == 0) printf("No solution\n");else if(num == 1){for(int i = 0; i < N * N; i++){printf("%c", ans[i]);if((i + 1) % N == 0) printf("\n");}}else printf("Multiple Solutions\n");}return 0;
}
这篇关于HDU 4069 Squiggly Sudoku DLX的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!