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 解题报告及测试数据的更多相关文章

  1. sgu 102 Coprimes 解题报告及测试数据

    102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求一个1-10000之间的数 N 的互质 ...

  2. SGU 101 Domino (输出欧拉路径)

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...

  3. SGU 101.Domino( 欧拉路径 )

    求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...

  4. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

  5. sgu 103 Traffic Lights 解题报告及测试数据

    103. Traffic Lights Time limit per test: 0.25 second(s) Memory limit: 4096 kilobytes 题解: 1.其实就是求两点间的 ...

  6. sgu 100 A+B 解题报告及测试数据

    100.A+B time limit per test: 0.25 sec. memory limit per test: 65536 KB 题解:上手题,不解释. 直接上代码: #include & ...

  7. SGU 101 Domino【欧拉路径】

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...

  8. Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据

    Array Diversity Time Limit:404MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descript ...

  9. SGU 101.Domino (欧拉路)

    时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个   方形,两边 ...

随机推荐

  1. hdu 4240在(最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240 思路:题意真的有点难理解:在城市A->B之间通过所有路径一小时之内能通过最大的车辆(Max ...

  2. 当苹果因为UIDevice、udid、uniqueIdentifier而把我们的应用拒之门外invalid binary的时候,呕心沥血解决方法啊

    本文转载至 http://blog.csdn.net/macmini/article/details/16341669 当我们辛辛苦苦把应用或者游戏做好的时候,满怀激动地把应用提交上去给苹果大大,谁知 ...

  3. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

  4. poj_2479 动态规划

    题目大意 给定一列数,从中选择两个不相交的连续子段,求这两个连续子段和的最大值. 题目分析 典型的M子段和的问题,使用动态规划的方法来解决. f[i][j] 表示将A[1...i] 划分为j个不相交连 ...

  5. 第一次广搜!HDU1548--A Strange Lift

    一上来看见题目就用了深搜(因为只会深搜)果断内存超限(据说时间也会超限)无奈只好开始用广搜 其实广搜的思路和深搜有很多类似的地方 不过实现的过程中用到了队列 因此有点难以理解(好吧我个人认为) 这题是 ...

  6. LAMP集群项目

    vi /etc/sysconfig/network 一.安装硬件环境(安装虚拟机) 1.安装VMware步骤 1.修改网卡配置 vi /etc/sysconfig/network-scripts/if ...

  7. 用httpClient的PostMethot频繁发送数据,抛出Too many open files

    在工作过程中,用httpclient去压测一个web api,发现压一小段时间就出现了Too many open files.实际上,HttpClient建立Socket时 ,post.release ...

  8. Spring Security OAuth2 源码分析

    Spring Security OAuth2 主要两部分功能:1.生成token,2.验证token,最大概的流程进行了一次梳理 1.Server端生成token (post /oauth/token ...

  9. 阿里云服务器Mysql修改密码

    核心架构基于阿里云服务器CentOS7.2操作系统,全面支持Java Web运行环境   Tomcat 8.5.8 JDK 8u111 MySQL5.7.16 详细信息,请在服务器执行  more r ...

  10. 直接IO 零拷贝 DAM 自缓存应用程序

    直接IO  零拷贝  DAM  自缓存应用程序