博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sgu101-欧拉回路
阅读量:6244 次
发布时间:2019-06-22

本文共 4024 字,大约阅读时间需要 13 分钟。

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 -
 
题意是这种:给你一个数字N,告诉你有多少牌,接下来每一行分别代表一张牌,两个数字分别代表正反两面。能够交换数字。比方我是[1,2]。[3。2]那么我就能够先推倒[1,2],然后转下[3,2]变成[2。3]那么就能够推倒[2,3]
如题目sample,output第一行是2 -,那么我就先选第2个[2,4]变成[4,2],然后是5+,就是[2,1],依次下去就能够所有推倒了。当然有些组合是能够推倒有些是不能所有推倒的。
 
怎么reduce这个题呢?应该非常easy想到一笔画问题。真的是非常easy想到 = =,那么sample input 就转化成
就是一个无向欧拉通路(跟回路差点儿相同。仅仅是回路要回到起点)问题
仅仅要奇数度的点的个数为0或者2就能够达成通路,假设是0,那么起点随意选,否则起点要选奇数度点。
 
所以我们要用DFS
 
void through(int start, vector
& s){ vector
e = maps[start]; for(int i = 0;i < e.size();i++){ if(Edges[e[i]].visited) continue; else{ Edges[e[i]].visited = true; if(start == Edges[e[i]].v1){ through(Edges[e[i]].v2, s); s.push_back(e[i]); } else { through(Edges[e[i]].v1, s); s.push_back(e[i]); } } } return;}
最核心的代码在这里,在DFS里有个跟出栈差点儿相同的操作,在这个操作后,我们就把这条路径给加到结果里面去,当然结果里面是逆序的,我们要逆向输出
 
以下贴出所有代码
 
#include 
#include
#include
#include
using namespace std;int N = 0;int occ[7] = {0};int START = 0;struct Edge{ int v1,v2; bool visited; Edge(){}; Edge(int _v1, int _v2, bool _vis){ v1 = _v1;v2 = _v2;visited = _vis; }};struct Edge Edges[110];map< int , vector
>maps;/* * 返回一个节点的全部边 */vector
getEdge(int n){ vector
set; for(int i = 0;i < N;i++){ if(n == Edges[i].v1 || n == Edges[i].v2){ set.push_back(i); } } return set;}void res(int start,vector
& s){ for(int i = s.size()-1;i >= 0;i--){ int front = Edges[s[i]].v1; int back = Edges[s[i]].v2; if(start == front){ cout << s[i]+1 << " +" << endl; start = back; } else { cout << s[i]+1 << " -" << endl; start = front; } }}void through(int start, vector
& s){ vector
e = maps[start]; for(int i = 0;i < e.size();i++){ if(Edges[e[i]].visited) continue; else{ Edges[e[i]].visited = true; if(start == Edges[e[i]].v1){ through(Edges[e[i]].v2, s); s.push_back(e[i]); } else { through(Edges[e[i]].v1, s); s.push_back(e[i]); } } } return;}int main(){ int front,back; cin >> N; int index = 0; while(cin >> front && cin >> back) { Edges[index] = Edge(front,back,false); index++; occ[front]++; occ[back]++; } int odd_pot = 0, start = Edges[0].v1; for(int i = 0;i < 7;i++){ if(occ[i] % 2 == 1){ odd_pot++; start = i; } maps[i] = getEdge(i); } START = start; if(odd_pot != 0 && odd_pot != 2){ cout << "No solution"; return 0; } vector
results; through(start,results); if(results.size() < N)cout << "No solution"; else res(START,results); return 0;}

转载地址:http://yfpia.baihongyu.com/

你可能感兴趣的文章
Java线程面试题 Top 50
查看>>
java内存模型
查看>>
python继承关系及DVD案例
查看>>
木其工作室代写程序 [原]使用Filter过滤ip禁止访问系统
查看>>
2.6 The Object Model -- Bindings
查看>>
2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)...
查看>>
二叉树问题(区间DP好题)
查看>>
PHP基础
查看>>
PHP奇淫技巧
查看>>
Centos中配置环境变量
查看>>
mysql中判断记录是否存在方法比较【转】
查看>>
HBase 列族的概念
查看>>
hdu2036
查看>>
基于模板匹配的马赛克检验
查看>>
Database4.exe用来导入excel
查看>>
Unable to preventDefault inside passive event listener
查看>>
java中string和int互相转化 (转)
查看>>
[LUOGU] P1220 关路灯
查看>>
【转】在控制台、WinForm项目中的嵌入mdf文件的烦恼
查看>>
【转】C51中断函数的写法
查看>>