sgu 101 Domino 解题报告及测试数据
101. Domino
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
题解:
求多米诺骨牌按照一定方式放置能否使相邻的位置数字相同。其实就是求无向图的欧拉通路,dfs即可。
但需要注意以下几点:
1、注意是否是连通图。
2、注意自环。
3、注意有两个奇度顶点时深搜起点。
以下是代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int arr[7][7];
int visited[105];
int path[105][2];
int st,end;
int n,tag;
struct node{
int x,y;
}Node[105]; int judge(){ //判断是否是欧拉图、半欧拉图
int a[7]={ 0 };
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
a[i]+=arr[i][j];
int cnt=0;
for(int i=0;i<7;i++)
if(a[i]%2){
st=i;
cnt++;//如果是半欧拉图 将深搜起点改为奇度顶点
}
if(cnt==0 || cnt==2)return st; //如果是返回深搜起点
return -1;
}
void dfs(int v,int k,int ii){//深度优先搜索
if(tag==1)return;
visited[v]=1;
char ch = k==1? '+' : '-'; //k为1代表不翻 k为2代表翻转
int t = k==1?Node[v].y : Node[v].x;
path[ii][0]=v+1;
path[ii][1]=ch;
if(ii==n-1){ //如果是连通图 输出路径
for(int i=0;i<n;i++)
printf("%d %c\n",path[i][0],path[i][1]);
tag=1;
}
for(int i=0;i<n;i++)
if(visited[i]==0){
if(Node[i].x==t)dfs(i,1,ii+1);
else if(Node[i].y == t)dfs(i,2,ii+1);
visited[i]=0;
}
}
int main(){
//freopen("1.in","r",stdin);
while(cin >> n){
for(int i=0;i<n;i++){
scanf("%d%d",&st,&end);
arr[st][end]++;
arr[end][st]++;
Node[i].x = st;
Node[i].y = end;
}
if(judge()==-1)printf("No solution\n");//不是欧拉图 半欧拉图
else{
for(int i=0;i<n;i++)
if(Node[i].x == st){
dfs(i,1,0);
break;
}
else if(Node[i].y == st){
dfs(i,2,0);
break;
}
if(tag==0)printf("No solution\n");//不是连通图
}
}
}
以下是测试数据:
sample input
17
0 3
1 2
5 1
1 4
2 1
0 2
3 4
3 5
4 3
5 0
3 4
4 0
0 4
4 4
1 4
0 4
3 1
3
5 2
0 5
3 5
4
5 2
5 2
5 4
2 5
9
2 5
2 1
2 1
5 4
5 0
4 1
2 1
3 5
3 4
14
2 5
4 1
5 4
0 0
3 0
5 4
3 0
3 2
2 4
1 2
5 3
3 1
4 1
2 4
sample output
3 +
2 +
5 +
4 +
7 -
1 -
10 -
8 -
9 -
11 -
17 +
15 +
12 +
13 +
14 +
16 -
6 +
No solution
3 -
1 +
2 -
4 -
4 -
1 -
2 +
3 -
7 +
6 -
9 -
8 +
5 +
5 +
4 +
7 -
8 +
1 +
3 +
2 +
10 +
9 +
6 -
11 +
12 +
13 -
14 -
sgu 101 Domino 解题报告及测试数据的更多相关文章
- sgu 102 Coprimes 解题报告及测试数据
102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求一个1-10000之间的数 N 的互质 ...
- SGU 101 Domino (输出欧拉路径)
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- SGU 101.Domino( 欧拉路径 )
求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- sgu 103 Traffic Lights 解题报告及测试数据
103. Traffic Lights Time limit per test: 0.25 second(s) Memory limit: 4096 kilobytes 题解: 1.其实就是求两点间的 ...
- sgu 100 A+B 解题报告及测试数据
100.A+B time limit per test: 0.25 sec. memory limit per test: 65536 KB 题解:上手题,不解释. 直接上代码: #include & ...
- SGU 101 Domino【欧拉路径】
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...
- Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据
Array Diversity Time Limit:404MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- SGU 101.Domino (欧拉路)
时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
随机推荐
- hdu 1813(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1813 思路:首先bfs预处理出‘0’到边界点最短距离,然后构造 h() 为所’0‘点逃离迷宫的最少步数 ...
- Angular2 Observable 可观察对象
可观察对象支持在应用中的发布者和订阅者之间传递消息.在需要进行事件处理,异步编程和处理多值的时候,可观察对象相对其他技术有显著的优点. 可观察对象是声明式的 —— 也就是说,虽然你定义了一个用于发布值 ...
- IOC和AOP的一些基本概念
IOC和AOP的一些基本概念介绍 IOC 介绍 IOC 一.什么是IOC IoC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不 ...
- $(document).scrollTop()与$(window).scrollTop()
$(document).scrollTop() 获取垂直滚动的距离 即当前滚动的地方的窗口顶端到整个页面顶端的距离 要获取顶端 只需要获取到scrollTop()==0的时候 就是顶端了 要获取底端 ...
- axios post传参后台无法接收问题
起因是在angular项目中使用axios发送post请求,向后台传参后台一直无法接收,网上查了有说是请求头设置不对,需要把Content-Type:application/x-www-form-ur ...
- 160229-02、Sublime Text 3 快捷键总结
选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...
- 使用Servlet3.0新特性asyncSupported=true时抛异常java.lang.IllegalStateException: Not supported
最近在运用Servlet3.0新特性:异步处理功能的时候出现以下了2个问题: 运行时会抛出以下两种异常: 一月 19, 2014 3:07:07 下午 org.apache.catalina.core ...
- iOS中navigationItem修改标题的颜色
UIColor * color = [UIColor redColor];//这里我们设置的是颜色,NSDictionary * dict = [NSDictionary dictionaryWith ...
- /proc/iomem和/proc/ioports对应的fops
/proc/iomem和/proc/ioports对应的fops static int __init ioresources_init(void) { struct proc_dir_entr ...
- The Personal Touch Client Identification 个性化接触 客户识别
w服务器要知道和谁在交谈. HTTP The Definitive Guide Web servers may talk to thousands of different clients simul ...