Domino

题目描述

Dominoes - game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards.

The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice…

The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

ENCYCLOPÆDIA BRITANNICA

Given a set of domino pieces where each side is marked with two digits from $0$ to $6$. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

题意概述

有$N$张多米诺骨牌,每张骨牌的两个面上各有一个数。现需将骨牌排成一条直线,且要求相邻骨牌上相邻的两个数相同。所有数字在$0$到$6$之间。给出一组方案。

数据范围:$1 \le N \le 100$。

算法分析

将$0$到$6$看成点,骨牌看成边,就变成了经典的欧拉路径问题。直接 DFS 即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct IOManager {
template <typename T>
inline bool read(T &x) {
char c; bool flag = false; x = 0;
while (~ c && ! isdigit(c = getchar()) && c != '-') ;
c == '-' && (flag = true, c = getchar());
if (! ~ c) return false;
while (isdigit(c)) x = (x << 3) + (x << 1) + c - '0', c = getchar();
return (flag && (x = -x), true);
}
inline bool read(char &c) {
c = '\n';
while (~ c && ! (isprint(c = getchar()) && c != ' ')) ;
return ~ c;
}
inline int read(char s[]) {
char c; int len = 0;
while (~ c && ! (isprint(c = getchar()) && c != ' ')) ;
if (! ~ c) return 0;
while (isprint(c) && c != ' ') s[len ++] = c, c = getchar();
return (s[len] = '\0', len);
}
template <typename T>
inline IOManager operator > (T &x) {
read(x); return *this;
}
template <typename T>
inline void write(T x) {
x < 0 && (putchar('-'), x = -x);
x > 9 && (write(x / 10), true);
putchar(x % 10 + '0');
}
inline void write(char c) {
putchar(c);
}
inline void write(char s[]) {
int pos = 0;
while (s[pos] != '\0') putchar(s[pos ++]);
}
template <typename T>
inline IOManager operator < (T x) {
write(x); return *this;
}
} io;

struct Solver {
private:
static const int N = 110;
static const int C = 10;
int n, cnt, mp[C][C], in[C];
bool vis[N];
pair <int, int> line[N];
vector <pair <int, int> > ans;
void input() {
io > n;
for (int i = 1; i <= n; ++ i) {
io > line[i].first > line[i].second;
++ mp[line[i].first][line[i].second], ++ mp[line[i].second][line[i].first];
++ in[line[i].first], ++ in[line[i].second];
}
}
void dfs(int t) {
for (int i = 0; i < C; ++ i)
if (mp[t][i]) -- mp[t][i], --mp[i][t], dfs(i), ++ cnt, ans.push_back(make_pair(t, i));
}
void process() {
cnt = 0; int s = -1;
for (int i = 0; i < C; ++ i)
if (in[i] & 1) ++ cnt, s = i;
else if (in[i] && ! ~ s) s = i;
if (cnt > 2) io < (char *) "No solution\n";
else {
cnt = 0, dfs(s);
if (cnt < n) io < (char *) "No solution\n";
else {
for (int i = ans.size() - 1; ~ i; -- i)
for (int j = 1; j <= n; ++ j)
if (! vis[j]) {
if (line[j].first == ans[i].first && line[j].second == ans[i].second) {
io < j < ' ' < '+' < '\n', vis[j] = true; break;
}
if (line[j].first == ans[i].second && line[j].second == ans[i].first) {
io < j < ' ' < '-' < '\n', vis[j] = true; break;
}
}
}
}
}

public:
void solve() { input(), process(); }
} solver;

int main() {
solver.solve();
return 0;
}

Domino
https://regmsif.cf/2017/10/12/oi/domino/
作者
RegMs If
发布于
2017年10月12日
许可协议