Problem Description
I like playing game with my friend, although sometimes looks pretty naive. Today I invent a new game called LianLianKan. The game is about playing on a number stack.
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.
 
Input
There are multiple test cases.
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)
 
Output
For each test case, output “1” if I can pop all elements; otherwise output “0”.
 
Sample Input
2
1 1
3
1 1 1
2
1000000 1
 
Sample Output
1
0
0
 
 
Source

注意给的顺序是从底部到顶部。

开始想直接模拟,不可行,因为在下面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)的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  3. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 搜索——深度优先搜索(DFS)

    设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...

随机推荐

  1. sc7731 Android 5.1 LCD驱动简明笔记之二

    此篇笔记基于sc7731 - android 5.1,对lcd的framebuffer做一个简明笔记. 一共分为两大部分:第一部分,关于LCD的硬件方面的:第二部分,关于lcd核心处理(framebu ...

  2. USB DATA Toggle

    For bulk and interrupt transfers, the data toggle resets <0> only on Set Configuration, Set In ...

  3. UIImage imageNamed 与 imageWithContentsOfFile的差别

    [UIImage imageNamed:]仅仅适合与UI界面中的贴图的读取,较大的资源文件应该尽量避免使用 用UIImage载入本地图像最经常使用的是以下三种: 1.用imageNamed方法 [UI ...

  4. HTML 转义符

    特殊字符 字符 十进制 转义字符 “ " " & & & < < < > > > 不断开空格(non-breaking ...

  5. 2013 French Open Semifinal Press

    http://v.youku.com/v_show/id_XNTY4MTgzOTEy.html?firsttime=0 Novak, can you take any confirt   for qu ...

  6. [MFC]MFC中OnDraw与OnPaint的区别

    问题 问题:我在视图画的图象或者文字,当窗口改变后为什么不见了?OnDraw()和OnPaint()两个都是解决上面的问题,有什么不同? OnDraw()和OnPaint()好象兄弟俩,因为它们的工作 ...

  7. UVA 1069 - Always an integer(数论)

    1069 - Always an integer 题意:给定一个多项式,推断是否总是整数 思路:LRJ大白上的例题,上面给出了证明,仅仅要1到k + 1(k为最高次数)带入方程都是整数,那么整个方程就 ...

  8. Cocos2d-html5 笔记4: 粒子

    今天看了cocos2d-html5里面的粒子系统相关的代码,首先看了代码中引用的两篇文章, 这两篇文章google上都可以搜到pdf的. The Ocean Spray in Your Face [j ...

  9. h5 -1

    <header></header> 页眉 主要用于页面的头部的信息介绍,也可用于板块头部 <hgroup></hgroup> 页面上的一个标题组合 一个 ...

  10. shell导出和导入redis

    1.导出redis #!/bin/bash REDIS_HOST=localhost REDIS_PORT=6379 REDIS_DB=1 KEYNAME=redis:hash:* KEYFILE=k ...