题目:

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.

题目大意翻译:

有一些秘密的门包含着非常有趣的单词迷题, 考古学家队伍必须解决它们才能够打开大门。 因为没有其他方法能偶打开这些门, 所以解决那些迷题对我们非常重要。

在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母

一样。例如, motorola的后面可以接上acm。

你的任务是写一个程序, 读入一系列单词,然后计算确定它们是否有可能被排成这样的队列。

样例输入:

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

样例输出:

The door cannot be opened.
Ordering is possible.
The door cannot be opened.
开始用dfs写的,结果因为数据太大,时间超限,然后再网上搜了下才知道是用欧拉回路加dfs做的
这是我的dfs代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char mapn[][];
int flag,n,vis[];
void dfs(char c,int a)
{
if( a == n)
{
flag = ;
return;
}
for(int i=;i<n;i++)
{
if(mapn[i][] == c && !vis[i])
{
vis[i] = ;
dfs(mapn[i][strlen(mapn[i])-],++a);
vis[i] = ;
}
}
return;
}
int main()
{
int t;
cin >> t;
while(t--)
{
flag = ;
memset(vis,,sizeof(vis));
memset(mapn,,sizeof(mapn));
cin >> n;
for(int i=;i<n;i++)
cin >> mapn[i];
for(int i=;i<n;i++)
{
vis[i] = ;
dfs(mapn[i][strlen(mapn[i])-],);
vis[i] = ;
}
if(flag)
cout << "Ordering is possible." << endl;
else cout << "The door cannot be opened." << endl;
}
return ;
}

下面这个是用欧拉回路加dfs做的

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#define MAXN 100
using namespace std;
int vis[MAXN],G[MAXN][MAXN],N, T, in[MAXN],out[MAXN];
void dfs(int u)//只用dfs会时间超限
{
vis[u] = true;
for(int i=; i<MAXN; ++i)
{
if(G[u][i] && !vis[i])
dfs(i);
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(G, , sizeof(G));
memset(in, , sizeof(in));
memset(out, , sizeof(out));
char str[];
scanf("%d",&N);
for(int i=; i<N; ++i)
{
scanf("%s", str);
++G[str[]-'a'][str[strlen(str)-]-'a'];//存下每个单词的开头结尾好搜索
++out[str[]-'a'];//出度
++in[str[strlen(str)-]-'a'];//入度
}
bool flag=true;
int num1=, num2=;
for(int i=; i<MAXN; ++i)
{
if(!flag)
break;
/*对于有向图, 则必须其中一个点的出度恰好比入度大1,
另一个的入度比出度大。
如果奇点数不存在的话,
则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。*/
if(in[i]!=out[i])
{
if(in[i]==out[i]+)
{
++num1;
}
else if(out[i]==in[i]+)
{
++num2;
}
else
{
flag=false;
break;
}
}
}
if(num1 && num2 && num1+num2>)
flag=false;
if(flag)
{
memset(vis, , sizeof(vis));
for(int i=; i<MAXN; ++i)
if(out[i])
{
dfs(i);
break;
}
//搜索判断是否构成回路
bool flag2=true;
for(int i=; i<MAXN; ++i)
{
if(in[i] && !vis[i])
{
flag2=false;
break;
}
if(out[i] && !vis[i])
{
flag2=false;
break;
}
}
if(flag2) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
else
{
printf("The door cannot be opened.\n");
}
}
return ;
}

最新文章

  1. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
  2. iOS开发小技巧--适当的清空模型中的某个数据,达到自己的需求,记得最后将数据还原(百思项目评论页面处理最热评论)
  3. 【UOJ #150】【NOIP 2015】运输计划
  4. 解决客户端访问https报错
  5. Java Web 设置默认首页
  6. 转: 透过CAT,来看分布式实时监控系统的设计与实现
  7. flash builder Error #2032
  8. Xcode Provisioning 路径
  9. Kakfa揭秘 Day7 Producer源码解密
  10. 【dfs or 最短路】【HDU1224】【Free DIY Tour】
  11. ubuntu下php不能显示中文的问题的解决过程。
  12. [tornado]使用webscoket的使用总是403错误
  13. 正则表达式的一些探索(偏JavaScript)
  14. #2 Python面向对象(一)
  15. Spring Boot Log4j2 日志学习
  16. Ubuntu 守护进程
  17. Ubuntu:Android编译环境设置和编译
  18. 【数组】Missing Number
  19. 随机生成气泡碰撞(原生js)
  20. python 编程测试练习答案

热门文章

  1. 用scrapy爬取搜狗Lofter图片
  2. 分布式ID系列之为什么需要分布式ID以及生成分布式ID的业务需求
  3. TensorFlow学习笔记——深层神经网络的整理
  4. alluxio源码解析-层次化存储(4)
  5. jQuery学习和知识点总结归纳
  6. 基于RBAC的权限框架
  7. HlpViewer.exe 单独打开
  8. centos7通过yum安装docker
  9. Django+zTree构建组织架构树
  10. Docker:镜像的迁移