UVa 211 The Domino Effect.
程序员文章站
2024-03-18 23:46:58
...
原题目请参照 https://vjudge.net/problem/UVA-211
在这里插入代#include<iostream>
#include<vector>
using namespace std;
int drow[4] = { -1, 0, 1, 0 };
int dcol[4] = { 0, 1, 0, -1 };
int inf[7] = { 1, 8, 14, 19, 23, 26, 28 };
int Data[7][8], T = 0, t = 0;
constexpr int maxn = 500;
int L[maxn], R[maxn], U[maxn], D[maxn], row[maxn], col[maxn], S[maxn];//S用来储存各个骨牌可能出现的位置.row储存结点对应的骨牌编号,col储存节点编号。
bool Vis[100];
void link();
bool inRange(int, int);
void init();
void remove(int flag);
void restore(int flag);
bool dfs();
void print_ans();
int code(int, int);
void sort(vector<int>&);
void swap(int& a, int& b);
int main() {
int cur;
while (1) {
init();
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 8; j++) {
if (cin >> cur) { Data[i][j] = cur; }
else return 0;
}
}
link();
if (T != 0)
printf("\n\n\n\n\n");
printf("Layout #%d:\n", ++T);
print_ans();
printf("\nMaps resulting from layout #%d are:\n", T);
dfs();
printf("\n\nThere are %d solution(s) for layout #%d.\n", t, T);
}
}
void link() {
int tot = 154, bone = 57;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 8; j++) {
int node = j + 1 + i * 8;
for (int k = 1; k <= 2; k++) {
int x = i + drow[k], y = j + dcol[k];
if (inRange(x, y)) {
int Bone = code(Data[i][j], Data[x][y]);
node = j + 1 + i * 8;
int copy = node;
while (D[copy] != node) { copy = D[copy]; }
D[copy] = tot; U[tot] = copy; D[tot] = node; U[node] = tot;
row[tot] = Bone; col[tot] = node; row[bone] = Bone;
copy = bone;
while (R[copy] != bone) { copy = R[copy]; }
R[copy] = tot; L[tot] = copy; R[tot] = bone; L[bone] = tot++;
copy = y + 1 + x * 8, node = copy;
while (D[copy] != node) { copy = D[copy]; }
D[copy] = tot; U[tot] = copy; D[tot] = node; U[node] = tot;
row[tot] = Bone; col[tot] = node; row[bone] = Bone;
copy = bone;
while (R[copy] != bone) { copy = R[copy]; }
R[copy] = tot; L[tot] = copy; S[Bone]++; R[tot] = bone; L[bone++] = tot++;
}
}
}
}
}
bool inRange(int r, int c) {
return r >= 0 && r <= 6 && c >= 0 && c <= 7;
}
int code(int x, int y) {
int a = min(x, y), b = x - a > 0 ? x - a : y - a;
return inf[a] + b + 56;
}
void init() {//十字链表的初始化。1~56为网络节点,57~153为所有可能摆放的位置。
t = 0;
for (int i = 0; i < maxn; i++) {
L[i] = R[i] = U[i] = D[i] = S[i] = 0;
if (1 <= i && i <= 56) { D[i] = U[i] = i; L[i] = i - 1; R[i] = i + 1; }
if (i >= 57 && i <= 153) { D[i] = i + 1; U[i] = i - 1; L[i] = R[i] = i; }
}
L[0] = 56; R[56] = 0; R[0] = 1; L[1] = 0;
U[0] = 153; D[153] = 0; D[0] = 57; U[57] = 0;
}
void remove(int flag) {//移除编号为flag的点的列。
L[R[flag]] = L[flag]; R[L[flag]] = R[flag];
for (int i = D[flag]; i != flag; i = D[i]) {
S[row[i]]--;
for (int j = R[i]; j != i; j = R[j]) {
U[D[j]] = U[j]; D[U[j]] = D[j];
}
}
}
void restore(int flag) {
L[R[flag]] = flag; R[L[flag]] = flag;
for (int i = D[flag]; i != flag; i = D[i]) {
S[row[i]]++;
for (int j = R[i]; j != i; j = R[j]) {
U[D[j]] = j; D[U[j]] = j;
}
}
}
bool dfs() {
int ret = 1;
for (int i = 57; i <= 84; i++) { if (S[i] > 0) { ret = 0; break; } if (!Vis[i]) { ret = 0; break; }}
if (ret == 1) { print_ans(); t++; return true; }
int index = D[0];
vector<int>seq;
for (int i = D[0]; i != 0; i = D[i]) { if (!Vis[row[i]]) { if (S[row[i]] <= 0) { return false; } seq.push_back(i); } }
if (!seq.empty()) {
sort(seq);
int in = seq[0];
int a = seq.size();
for (int i = 0; i < S[row[in]]; i++) {
index = seq[i];
Vis[row[index]] = true;
int pos[2], cnt = 0;
for (int j = R[index]; j != index; j = R[j]) { pos[cnt++] = col[j]; }
remove(pos[0]); remove(pos[1]);
Data[(pos[0] - 1) / 8][(pos[0] - 1) % 8] = Data[(pos[1] - 1) / 8][(pos[1] - 1) % 8] = row[index] - 56;
dfs();
restore(pos[0]); restore(pos[1]);
S[row[index]]--;
Vis[row[index]] = false;
}
}
else return false;
return false;
}
void print_ans() {
printf("\n\n");
for (int i = 0; i < 7; i++) {
cout << endl;
for (int j = 0; j < 8; j++) {
printf("%4d", Data[i][j]);
}
}
cout << endl << endl;
}
void sort(vector<int> &seq) {
int a = seq.size();
for (int i = 0; i < a - 1; i++) {//确保枚举可能位置最少的骨牌。
for (int j = 0; j < a - 1 - i; j++) {
if (S[row[seq[j]]] > S[row[seq[j + 1]]]) swap(seq[j + 1], seq[j]);
}
}
for (int i = 1; i < a; i++) {//确保枚举同一块骨牌的位置。
if (S[row[seq[0]]] != S[row[seq[i]]]) break;
if (row[seq[i]] == row[seq[0]]) continue;
for (int j = i; j < a; j++) {
if (S[row[seq[0]]] != S[row[seq[j]]]) break;
if (row[seq[j]] == row[seq[0]]) { swap(seq[i], seq[j]); break; }
}
}
}
void swap(int& a, int& b) {
int tmp = a; a = b; b = tmp;
}
//用最常规的DFS算法就能够解决,但是这里使用了dlx算法判断了后续不能放置骨牌的位置。并且对剩余的位置进行了排序,确保每一次都放置可能性最小的位置,从而进一步提高了效率。码片