题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5641

题目类型:水题

题目思路:将点x到点y所需要跨过的点存入mark[x][y]中(无需跨过其它点存0),然后按照题目给的路径逐一判断是否可走,得出结果。

需要判断的:

1.给出的点是否在1~9之间

2.点的数量n是否在4~9之间

3.有没有重复的点

4.两点之间(x-->y)是否需要跨过第三点(z),该点(z)是否已经走过

测试样例(基本考虑了所有情况):

10
4 1 3 6 2
4 6 2 1 3
4 8 1 6 7
6 2 1 2 3 5 7
6 1 5 8 2 6 7
5 4 2 8 5 6
5 12 3 4 5 6
7 0 1 2 3 4 5 6
3 1 5 9
9 1 5 9 4 2 6 3 7 8 1.invalid
2.valid
3.valid
4.invalid
5.valid
6.invalid
7.invalid
8.invalid
9.invalid
10.valid

实现代码:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h> //若从i->j 必须经过其它点,存入mark[i][j]中,否则mark[i][j]为0
int mark[][] = {
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , },
{ , , , , , , , , , }
}; //a[]存走的路线
int a[]; //num[]统计各个点出现的次数
int num[]; //flag[]记录各个点是否被走过,0表示未走,1表示已走
int flag[]; int main()
{
int T;
int n;
int i, ans;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
ans = ;
memset(a, , sizeof(a));
memset(num, , sizeof(num));
memset(flag, , sizeof(flag));
for (i = ; i <= n; i++)
{
scanf("%d", &a[i]);
if(a[i]< || a[i]>) //点是否在范围内
ans = ;
}
if(!ans)
{
printf("invalid\n");
continue;
}
for(i=; i<=n; i++)
num[a[i]]++;
for (i = ; i <= ; i++) {
if (num[i] > )
break;
}
if (n < || n > || i<) //点的数量是否在范围内,各点是不是最多只有一个
{
printf("invalid\n");
continue;
}
int x, y;
for (i = ; i < n; i++) //n个点会有n-1条线
{
x = a[i];
y = a[i + ];
if ( (mark[x][y] != ) && (flag[mark[x][y]] == ) )
{
ans = ; //如果x->y需要跨过点mark[x][y],而mark[x][y]未走过,则不可行
break;
}
flag[x] = ;
}
if (ans)
printf("valid\n");
else
printf("invalid\n");
}
return ;
}

hdu 5641 King's Phone的更多相关文章

  1. HDU 5641 King's Phone 模拟

    King's Phone 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5641 Description In a military parade, ...

  2. hdu 5641 King's Phone(暴力模拟题)

    Problem Description In a military parade, the King sees lots of new things, including an Andriod Pho ...

  3. HDU 5641 King's Phone【模拟】

    题意: 给定一串密码, 判断是否合法. 长度不小于4 不能重复经过任何点 不能跳过中间点,除非中间点已经经过一次. 分析: 3*3直接记录出可能出现在两点之间的点,直接模拟就好. 注意审题,别漏了判断 ...

  4. hdu 5641 BestCoder Round #75

    King's Phone  Accepts: 310  Submissions: 2980  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  5. HDU 5642 King's Order dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order  Accepts: 381  Submissions: 1361   ...

  6. HDU 5644 King's Pilots 费用流

    King's Pilots 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 Description The military parade w ...

  7. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  8. HDU 5642 King's Order 动态规划

    King's Order 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 Description After the king's speec ...

  9. HDU 5640 King's Cake GCD

    King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...

随机推荐

  1. Windows 10预览版14316开启Bash命令支持

    00x0 前言 4月7日凌晨,微软推送了最新的Windows 10一周年更新预览版14316,其中重要的是原生支持Linux Bash命令行支持. 00x1 问题 如何开启Linux Bash命令行? ...

  2. ASP.NET MVC HtmlHelper之Html.ActionLink

    前言 ActionLink用于生成超链接,方法用于指向Controller的Action. 扩展方法与参数说明 ActionLink扩展方法如下: public static MvcHtmlStrin ...

  3. 移动端API架构 统一Proxy还是各自为政?

    今天首先回答上一篇的问题: 为什么APP通过运营商接入网络,连通率会那么差? 1. 域名缓存问题 运营商的localdns会缓存域名的解析结果,不向权威DNS递归查询解析 为什么要这么干呢? 1)运营 ...

  4. Android按返回键退出程序但不销毁,程序后台保留

    重写onKeyDown事件即可 @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyE ...

  5. C++_系列自学课程_第_7_课_数组_《C++ Primer 第四版》

    说到数组,大家应该都很熟悉,在C.Pascal.Java等语言中,都有数组的概念.在C++中也提供了对数组的支持.数组简单来说就是一堆相同 数据类型对象的集合. 这里要把握住两个要点: 相同的数据类型 ...

  6. UVALive 6908---Electric Bike(DP或记录型深搜)

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. 从零开始学 Java - Windows 下安装 JDK

    关于未来 "我要死在火星.在我死去的时候能够想着人类能有一个美好的未来--有可持续的能源,同时能够殖民其他的星球来避免人类灭绝的最坏可能." 官网下载 直接打开官网:http:// ...

  8. BuilderParttern(建造者模式)

    /** * 建造者模式 * 主要用于构造复杂的对象 * 在优朋播放器就是采用建造者构建的,可以说比较有心得吧 * @author TMAC-J * */ public class BuilderPat ...

  9. 基于WCF MSMQ 的企业应用解决方案

    最近研究了一下基于MSMQ的WCF应用,从书上.网上查了很多资料,但始终没能彻底理解WCF-MSMQ的工作原理,也没能得到一个合理的应用解决方案.索性还是自己做个实验,探索一下吧.经过反复试验,颇有收 ...

  10. 四步让你的网站秒开,wordpress框架为例子,其他框架道理类似

    我这里以wordpress框架制作的网站为例子,效果可以看看我的网站,香港的垃圾主机199一年2M带宽,速度也能秒开,不信试试效果33小游戏 我的是wordpress制作的网站,大家都知道WP各种臃肿 ...