HDU4272LianLianKan(dfs)
Now we have a number stack, and we should link and pop the same element pairs from top to bottom. Each time, you can just link the top element with one same-value element. After pop them from stack, all left elements will fall down. Although the game seems to be interesting, it's really naive indeed.

To prove I am a wisdom among my friend, I add an additional rule to the game: for each top element, it can just link with the same-value element whose distance is less than 6 with it.
Before the game, I want to check whether I have a solution to pop all elements in the stack.
The first line is an integer N indicating the number of elements in the stack initially. (1 <= N <= 1000)
The next line contains N integer ai indicating the elements from bottom to top. (0 <= ai <= 2,000,000,000)
注意给的顺序是从底部到顶部。
开始想直接模拟,不可行,因为在下面5个里可能有一个或多个相同的元素,每个选择都可能对后面造成影响,因此要dfs。
其中还巧妙的用了map,当然改用set来存然后看里面到底有没有奇数个的元素也是可以的,这点的优化很重要。
#include<stdio.h>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<map> using namespace std; int vis[],a[]; int dfs(int n)
{
int i=,j=n-;
while(n>=&&vis[n]) --n;
if(n==-) return ;
if(n==&&!vis[]) return ;
while(i<=)
{
if(j<) return ;
if(vis[j]) --j;
if(a[n]==a[j])
{
vis[j]=;
if(dfs(n-)) return ;
vis[j]=;
}
++i;
--j;
}
return ;
} int main()
{
int k,m,q,t,p,n;
int T;
map<int,int> mp;
map<int,int>::iterator it; while(cin>>n)
{
t=;
for(int i=;i<n;++i)
{
scanf("%d",&a[i]);
vis[i]=;
mp[a[i]]++;
} for(it=mp.begin();it!=mp.end();++it)
{
if((*it).second%)
{
t=;
break;
}
} if(t)
{
cout<<<<endl;
}else
{
cout<<dfs(n-)<<endl;
} }
return ;
}
HDU4272LianLianKan(dfs)的更多相关文章
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 搜索——深度优先搜索(DFS)
设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...
随机推荐
- 【转】获取手机的ipv4地址
http://blog.csdn.net/yueqinglkong/article/details/17391051 直接贴代码: public class GetLocalIpAddress ext ...
- Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心
A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...
- 使用PageHeap.EXE或GFlags.EXE检查内存越界错误 (转)
2011-05-27 20:19 290人阅读 评论(0) 收藏 举报 microsoftdebuggingstructureoutputimagefile 必先利其器之一:使用PageHeap.EX ...
- python中的reduce(转)
python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是一个二元操作函数)先对集合中的第1 ...
- 20 Free Open Source Web Media Player Apps
free Media Players (Free MP3, Video, and Music Player ...) are cool because they let web developers ...
- 【优先队列】HDU 1873——看病找医生
来源:点击打开链接 看路径记录的BFS之前,再看一遍优先队列的用法. 优先队列的排序规则可以用运算符重载的方式完成,通常意义下,应该用friend bool operator <进行重载. #i ...
- 深入理解Redis中的主键失效及其实现机制
参考:http://blog.sina.com.cn/s/articlelist_1221155353_0_1.html 作为一种定期清理无效数据的重要机制,主键失效存在于大多数缓存系统中,Reids ...
- 关于androidManifest.xml的概叙以及intent-filter的详细分析
AndroidManifest.xml配置文件对于Android应用开发来说是比较细但又很重要的基础知识,本文旨在总结该配置文件中常用到的几个属性,以便日后查阅,至于那些比较细的属性,主要是平时开发比 ...
- JS Math算数
Math.ceil()ceil() 方法可对一个数进行上舍入. ceil英译 天花板 参数必须是一个数值.返回值大于等于 x,并且与它最接近的整数. Math.floor()floor() 方法可对一 ...