• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

SGU 101 Domino

101. Domino

time limit per test: 0.5 sec. memory limit per test: 4096 KB

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.

 

Input

The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

 

Output

Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if  you rotate it).

 

Sample Input

5
1 2
2 4
2 4
6 4
2 1

Sample Output

2 -
5 +
1 +
3 +
4 -
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <map>
 3 #include <queue>
 4 #include <vector>
 5 #include <string>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <cstdlib>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 #define maxn 105
14 #define ll long long
15 #define INF 0x7fffffff
16 #define eps 1e-8
17 struct line{
18     int l, r;
19 };
20 line a[maxn];
21 int cnt[maxn];
22 int pre[maxn][maxn];
23 int vis[maxn][maxn];
24 int use[maxn];
25 int ans[maxn];
26 vector<int>g[maxn];
27 int n, m,c;
28 void print(int k,int u){
29     if (pre[k][u] != -2)print(k-1,pre[k][u]);
30     ans[c++] = u;
31 }
32 void dfs(int u,int k){
33     if (m == 0)return;
34     if (k == n+1){print(n, u); m = 0; return; }
35     for (int i = 0; i < g[u].size(); i++){
36         int v = g[u][i];
37         if (vis[u][v]&&vis[v][u]){
38             vis[u][v]--;
39             vis[v][u]--;
40             pre[k][v] = u;
41             dfs(v,k+1);
42             vis[u][v]++;
43             vis[v][u]++;
44         }
45     }
46 }
47 int main(){
48     int cas = 1;
49     int t;
50     while (~scanf("%d",&n)){
51         memset(vis, 0, sizeof vis);
52         for(int i=0;i<7;i++)g[i].clear();
53         memset(a, 0, sizeof a);
54         memset(pre, -1, sizeof pre);
55         memset(use, 0, sizeof use);
56         memset(ans, 0, sizeof ans);
57         for (int i = 0; i < n; i++){
58             scanf("%d%d", &a[i].l,&a[i].r);
59             cnt[a[i].l]++; cnt[a[i].r]++;
60             g[a[i].l].push_back(a[i].r);
61             g[a[i].r].push_back(a[i].l);
62             vis[a[i].l][a[i].r]++;
63             vis[a[i].r][a[i].l]++;
64         }
65         t = 0; m = 1; c = 0;
66         int t1 = a[0].l;
67         for (int i = 0; i < 7; i++)if (cnt[i] & 1){ t++; t1 = i; }
68         if (t != 0 && t!=2){ printf("No solution\n"); continue; }
69         pre[0][t1] = -2;
70         dfs(t1,1);
71         if (c <= n){ printf("No solution\n"); continue; }
72         for (int j = 0; j < n; j++){
73             for (int i = 0; i < n; i++){
74                 if(use[i]==0)if (a[i].l == ans[j] && a[i].r==ans[j+1]){ use[i] = 1; printf("%d +\n", i + 1); break; }
75                 if (use[i] == 0)if (a[i].r == ans[j] && a[i].l==ans[j+1]){ use[i] = 1; printf("%d -\n", i + 1); break; }
76             }
77         }
78     }
79     return 0;
80 }
View Code

 

posted @ 2013-10-30 14:10  HaibaraAi  阅读(95)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3