SGU-101 Domino

来源:互联网 发布:什么软件容易画电路图 编辑:程序博客网 时间:2024/04/30 19:24

101. Domino

time limit per test: 0.25 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

51 22 42 46 42 1

Sample Output

2 -5 +1 +3 +4 -

————————————————————挺萌的分割线————————————————————

前言:改了好几天郁闷了好几天,终于挺萌的把这道题给过了。SGU说给你250ms就给你250ms,一点不虚啊。说给你4M就给你4M啊。猛啊。

思路:花了几十个小时在上面,错误居然就是把虚拟出来的栈底元素权值写错了!!!下次不要赋值为-1什么的,既然是虚拟,那就无穷大!

骨牌是无向的,但是此题要求被翻转的骨牌输出方向,并且存在大量的重边,因此当成有向图来做。尽管是有向图,但是数度数的时候依然可以按照无向图来数。想一想为什么。

边的编号当做边的权值,正向为正值,反向为负值(注意编号从0开始就不能这样做了)

代码如下:

/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <iostream>#define INF 0x3f3f3f3f#define LL long longusing namespace std;/****************************************/const int N = 7, M = 210;int m, tot, head[N], deg[N], st, path[M], pi;bool vis[N], use[M];struct Node {int v, next;int w;}edge[M];struct STA {int u, w;}sta[M];void add(int u, int v, int w){edge[tot].v = v;edge[tot].w = w;edge[tot].next = head[u];head[u] = tot++;}bool judge(){st = -1;int one = 0;for(int i = 0; i <= 6; i++) if(vis[i]) {if(st == -1) st = i;if(deg[i]&1) {one++;if(one > 2) return false;st = i;}}if(one == 1) return false;return true;}void Fleury(){int top = -1;sta[0].u = st; sta[0].w = -INF;top++;while(top != -1) {int u = sta[top].u, w = sta[top].w;int i;for(i = head[u]; i != -1; i = edge[i].next) {int id = abs(edge[i].w);if(!use[id]) {use[id] = true;top++;sta[top].u = edge[i].v;sta[top].w = edge[i].w;break;}}if(i == -1) {if(w != -INF) path[pi++] = w;top--;}}}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifscanf("%d", &m);int u, v;tot = 0;memset(head, -1, sizeof(head));memset(vis, 0, sizeof(vis));memset(deg, 0, sizeof(deg));memset(use, 0, sizeof(use));for(int i = 1; i <= m; i++) {scanf("%d%d", &u, &v);vis[u] = vis[v] = true;add(u, v, i); add(v, u, -i);deg[u]++; deg[v]++;}bool flag = judge();pi = 0;if(flag) Fleury();if(pi != m) puts("No solution");else {for(int i = pi - 1; i >= 0; i--) {if(path[i] > 0) printf("%d +\n", path[i]);else printf("%d -\n", -path[i]);}}return 0;}


0 0