HDU4272LianLianKan(dfs)

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

2012 ACM/ICPC Asia Regional Changchun Online



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

开始想直接模拟,不可行,因为在下面5个里可能有一个或多个相同的元素,每个选择都可能对后面造成影响,因此要dfs。

其中还巧妙的用了map,当然改用set来存然后看里面到底有没有奇数个的元素也是可以的,这点的优化很重要。

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<set>
 8 #include<map>
 9
10 using namespace std;
11
12 int vis[1001],a[1001];
13
14 int dfs(int n)
15 {
16     int i=0,j=n-1;
17     while(n>=0&&vis[n]) --n;
18     if(n==-1) return 1;
19     if(n==0&&!vis[0]) return 0;
20     while(i<=5)
21     {
22         if(j<0) return 0;
23         if(vis[j]) --j;
24         if(a[n]==a[j])
25         {
26             vis[j]=1;
27             if(dfs(n-1)) return 1;
28             vis[j]=0;
29         }
30         ++i;
31         --j;
32     }
33     return 0;
34 }
35
36 int main()
37 {
38     int k,m,q,t,p,n;
39     int T;
40     map<int,int> mp;
41     map<int,int>::iterator it;
42
43     while(cin>>n)
44     {
45         t=0;
46         for(int i=0;i<n;++i)
47         {
48             scanf("%d",&a[i]);
49             vis[i]=0;
50             mp[a[i]]++;
51         }
52
53         for(it=mp.begin();it!=mp.end();++it)
54         {
55             if((*it).second%2)
56             {
57                 t=1;
58                 break;
59             }
60         }
61
62         if(t)
63         {
64             cout<<0<<endl;
65         }else
66         {
67             cout<<dfs(n-1)<<endl;
68         }
69
70     }
71     return 0;
72 }
时间: 2024-10-13 11:59:12

HDU4272LianLianKan(dfs)的相关文章

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

11218 - KTV(dfs)

问题 C: Repeat Number 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 7 [提交][状态][论坛] 题目描述 Definition: a+b = c, if all the digits of c are same ( c is more than ten),then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y]. 输入 There

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

poj3187Backward Digit Sums(DFS)

题目链接: huangjing 思路: 这个题目想到dfs很容易,但是纠结在这么像杨辉三角一样计算那些值,这个我看的队友的,简直厉害,用递归计算出杨辉三角顶端的值....具体实现详见代码... 题目: Language: Default Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4285   Accepted: 2474 Description FJ and his cows

A Knight&#39;s Journey (DFS)

Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. Th