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 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
随机推荐
- VC++为你的程序增加内存泄露检测
使用方法: C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 ...
- 转载 -- Cocoapod方式引入百川SDK -报错[!] Unable to find a specification for `xxx`
[cocopad集成百川sdk官网] iOS需要填写BundleID .BundleID要是当前应用的BundleID.勾选淘宝登录基础包下载SDK. 注意事项:将下载的SDK中的身份图片yw_122 ...
- 编程之美 set 21 24点游戏
题目 输入: n1, n2, n3, n4 (1~13) 输出: 若能得到运算结果为 24, 则输出一个对应的运算表达式 如: 输入: 11, 8, 3, 5 输出: (11-8) * (3*5) = ...
- IOS 预览word文档的集中方式
在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...
- URL地址传值型多条件搜索JS
function ResetSearchVal(objArray) { var strUrl = location.href; ; i < objArray.length; i++) { var ...
- wap开发体会<转载>
前二天因工作需要,上头要求做一个wap版的网站,到网上学习了一天,弄了个beta版出来(http://wap.luckty.com 功能很一般),整理几点经验如下: 1.wap网站用的是wml标识,非 ...
- Django学习笔记第一篇--Hello,Django
一.Django的安装: 1.python虚拟运行的环境的安装以及安装django: sudo pip install virtualenv export VIRTUALENV_DISTRINUTR= ...
- asp 中创建日志打印文件夹
string FilePath = HttpRuntime.BinDirectory.ToString(); string FileName = FilePath + "日志" + ...
- Spring Security OAuth2 源码分析
Spring Security OAuth2 主要两部分功能:1.生成token,2.验证token,最大概的流程进行了一次梳理 1.Server端生成token (post /oauth/token ...
- 在R语言中封装类读写sql操作工具类
1.mysql_helper.R # 使用RMySQL操作数据库 # 载入DBI和RMySQL包 library(DBI) library(RMySQL) mysql_con <- functi ...