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 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
随机推荐
- lumen 常用辅助函数
optional 函数接收任意参数并允许你访问对象上的属性或调用其方法.如果给定的对象为空,属性或方法调用返回 null return optional($user->address)-> ...
- 剑指 offer set 13 把数组排成最小的数
总结 1. 给定 3, 32, 321 将他们组合成最小的数, 比如 321323 2. 3 -> 333 32 -> 322 321 -> 321 然后再排序
- excel 使用技巧
计算两个日期的差值 1.计算当前日期与目标日期相差天数,下面例子中当前日期未2017/3/19 2.列增加数据条
- JUnit常用断言及注解
断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过. 断言核心方法 assertArrayEquals(expecteds, actuals) 查看两个数组 ...
- http协议------>概述和动手实践认识Http协议
http协议是用来定义客户端和web服务器通讯格式 浏览器与服务器的交互过程 是tcp/ip的应用层 版本:http/1.0(客户端和web服务器建立连接后只能访问一个web资源) http/1. ...
- 2017 Multi-University Training Contest - Team 3——HDU6063 RXD and math
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6063 题目意思:字面意思,给出n,k,算出这个式子的答案. 思路:比赛的时候是打表找规律过了,赛后仔细 ...
- jmeter常见参数 vars、prev、ctx 、props 类的api
ctx - ( JMeterContext) - gives access to the context vars - ( JMeterVariables) - gives read/write ac ...
- html中a标签的target属性
_blank -- 在新窗口中打开链接 _parent -- 在父窗体中打开链接 _self -- 在当前窗体打开链接,此为默认值 _top -- 在当前窗体打开链接,并替换当前的整个窗体(框架页) ...
- 3.2 - FTP文件上传下载
题目:开发一个支持多用户同时在线的FTP程序要求:1.用户加密认证2.允许同时多用户登录3.每个用户有自己的家目录,且只能访问自己的家目录4.对用户进行磁盘配额,每个用户的可用空间不同5.允许用户在f ...
- VC++SDK编程——模拟时钟
#include <Windows.h> #include <tchar.h> #include <math.h> typedef struct Time { in ...