• SGU101


    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 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 #include <algorithm>
      5 #include <vector>
      6 
      7 using namespace std;
      8 
      9 #define maxn 105
     10 
     11 int n,sp,len = 0;
     12 int fa[10],deg[10],first[10],v1[2 * maxn],next[2 * maxn],ans1[maxn];
     13 bool done[10],vis[2 * maxn];
     14 bool flag = 1;
     15 char ans2[maxn];
     16 
     17 int _find(int x) {
     18         return x == fa[x] ? x : fa[x] = _find(fa[x]);
     19 }
     20 
     21 void _union(int x,int y) {
     22     int u = _find(x),v = _find(y);
     23 
     24     if(u != v) fa[u] = v;
     25 }
     26 void judge() {
     27     int pos = 0;
     28     while(pos <= 6 && !done[pos]) pos++;
     29 
     30     sp = pos;
     31 
     32     int now = fa[pos];
     33     int odd = 0;
     34     for(int i = 0; i <= 6; i++) {
     35             if(!done[i]) continue;
     36             if(deg[i] % 2) {
     37                     odd++;
     38                     sp = i;
     39             }
     40             int u = _find(i);
     41             if(u != now) flag = 0;
     42     }
     43 
     44     if(odd > 2) flag = 0;
     45 
     46 }
     47 
     48 
     49 void output(int u) {
     50     //printf("u = %d
    ",u);
     51     for(int e = first[u]; e != -1; e = next[e]) {
     52 
     53             if(!vis[e]) {
     54                     vis[e] = 1;
     55                     int t = (e % 2) ? -1 : 1;
     56                     vis[e + t] = 1;
     57                     output(v1[e]);
     58 
     59                     char c = t == -1 ? '-' : '+';
     60                     //printf("u = %d v = %d
    ",u,v1[e]);
     61                     ans1[len] =e / 2 + 1;
     62                     ans2[len++] = c;
     63 
     64             }
     65     }
     66 
     67 }
     68 
     69 void addedge(int a,int b,int id) {
     70         int e = first[a];
     71         next[id] = e;
     72         first[a] = id;
     73         v1[id] = b;
     74 
     75 }
     76 void solve() {
     77 
     78 
     79     judge();
     80 
     81     if(flag) {
     82             output(sp);
     83             for(int i = len - 1; i >= 0; i--)
     84                     printf("%d %c
    ",ans1[i],ans2[i]);
     85     }
     86     else printf("No solution
    ");
     87 
     88 }
     89 int main()
     90 {
     91 
     92    // freopen("sw.in","r",stdin);
     93     scanf("%d",&n);
     94 
     95     //printf("n = %d
    ",n);
     96 
     97     for(int i = 0; i <= 6; i++) {
     98             fa[i] = i;
     99             first[i] = -1;
    100     }
    101 
    102     for(int i = 0; i < 2 * n; i += 2) {
    103             int a,b;
    104             scanf("%d%d",&a,&b);
    105             done[a] = 1;
    106             done[b] = 1;
    107             _union(a,b);
    108             deg[a]++;
    109             deg[b]++;
    110             addedge(a,b,i);
    111             addedge(b,a,i + 1);
    112     }
    113 
    114     solve();
    115 
    116     //cout << "Hello world!" << endl;
    117     return 0;
    118 }
    View Code
  • 相关阅读:
    如何选择数据科学最好的Python IDE?
    Python代码详解:入门时间序列分类
    2月编程语言排行榜:Python 稳坐前三,Java依旧第一
    写 Python 时的 5 个坏习惯
    Python的多线程threading和多进程multiprocessing
    Python看春运,万条拼车数据背后的春节迁徙地图
    python数据分析案例实战——融360客户贷款风险预测(信用卡)
    情人节攻略:用Python撒狗粮的正确姿势
    Python函数式编程
    python基础
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3581266.html
Copyright © 2020-2023  润新知