LianLianKan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3681    Accepted Submission(s): 1101

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

http://acm.hdu.edu.cn/showproblem.php?pid=4272

题意:

题意:长度为n(n<=1000)的栈,栈顶元素可以与下面1~5个数中相同的元素消去,问最后能都完全消去。

题解:

状态压缩 dp  如何判断一个 物品 是否可以 被删除 ,首先 最坏的 情况是  2 0 0 0 0  1 1 1 1 2   假如我们要消除 栈顶的 2  ,0表示已经被删除了。

辅助解释:假设2前面有一个3,那么3能够消除的最远位置为最后一个0处;

我们要 知道 包括 本身在内的  10 个 数位

定义dp[h][i]表示高度为h,状态为i时能否全部消除; 二进制位1表示未消除,0表示当前位已消除;

/**
定义dp[h][i]表示高度为h,状态为i时能否全部消除; 二进制位1表示未消除,0表示当前位已消除;
 
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<iomanip>
using namespace std;
const int maxn = 1200;
int dp[maxn][maxn];
int a[maxn];
int dfs(int h,int c)
{
    if(h < 0){
        if(c) return 0;// 
        else return 1;// 结果为0,表明可以完全消除;
    }
    if(dp[h][c]!=-1) return dp[h][c];
    int &res = dp[h][c];
    res = 0;
    int t = c&(1<<9);//获得定点的位,以判断是否已经被消除;
    if(t){//未被消除;
        c = c^(1<<9);//把顶去掉;
        int cnt = 0;
        for(int i = 1,j = 8;i <= 9&&j>=0; i++,j--){
            if(c&(1<<j)){//如果当前位置未去掉;
                cnt++;
                if(cnt>5) break;//控制距离小于6;
                if(a[h] == a[h-i]){//如果和顶相等;去掉当前位;
                    int t = c^(1<<j);
                    t <<= 1;
                    if(h-10>=0) t += 1;
                    res = dfs(h-1,t);
              //      else res = dfs(h-1,t);
                    if(res) break;
                }
            }
        }
    }else
    {//已经被消除时,脱栈顶;
        c <<= 1;
        if(h-10>=0) res = dfs(h-1,c+1);//h>=10表明可以继续下移;
        else res = dfs(h-1,c);
    }
    return res;
}
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        for(int i = 0; i < n; i++){
            scanf("%d",&a[i]);
        }
        if(n%2){//数量为奇数个,肯定不可以一一配对完全消除;
            printf("0\n"); continue;
        }
        memset(dp,-1,sizeof dp);
        int c = 0, i, j;
        for(i = n-1, j = 0; i >= 0&&j<=9; i--,j++){//初始化二进制111111111;
            c = (c<<1)+1;
        }
        while(j<=9){//n<9时,补0;只是为了方便同一处理;
            c <<= 1;
            j++;
        }
        printf("%d\n",dfs(n-1,c));
    }
    return 0;
}

hdu 4272 LianLianKan 状态压缩的更多相关文章

  1. LianLianKan - HDU 4272(状态压缩)

    题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处 ...

  2. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  3. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  5. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

  6. hdu 2489 最小生成树状态压缩枚举

    思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  7. HDU 3001(状态压缩dp)

    状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...

  8. hdu 4856 Tunnels 状态压缩dp

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  9. HDU 1074 (DP + 状态压缩)

    题意: 给你N个课程, 每个课程有结束的时间 , 和完成这门课程需要的时间 超过课程结束ed时间,每一天就要花费 1点绩点: 然后要求你安排如何做课程使得花费的绩点最少 (看了博客后才发现状态压缩很⑥ ...

随机推荐

  1. 个人python学习路线记录

    一.入门视频 零基础入门学习Python --小甲鱼 二.博客园 python快速教程 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778. ...

  2. freedom isn't free

    财务自由(除去房和车) 第一阶段: 个人存款达到50万以上 第二阶段 个人存款100~200万 第三阶段 个人存款400万以上 第三阶段以上才能算实现了相对较好的财务自由!come on , boys ...

  3. Oracle数据库冷备份与恢复(救命稻草)

    说明,只要是同样系统,同样数据库版本,是可以做冷备恢复.冷备份数据必须是数据库不在open状态下.以oracle11gR2为例. 一.冷备份与冷恢复 具体步骤如下. 1. 复制旧的数据库文件 (1) ...

  4. css的checkbox样式变化

    1.CSS body{font-family:'微软简行楷'} ul li{list-style:none; margin:10px;color:#4985d7;} .myCheck { displa ...

  5. 大数据的开始:安装hadoop

    为实现全栈,从今天开始研究Hadoop,个人体会是成为某方面的专家需要从三个方面着手 系统化的知识(需要看书或者比较系统的培训) 碎片化的知识(需要根据关注点具体的深入的了解) 经验的积累(需要遇到问 ...

  6. http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SVYGqy364vDfv6AY4ERPa

    http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SV ...

  7. http://blog.csdn.net/a942980741/article/details/39990699

    http://blog.csdn.net/a942980741/article/details/39990699

  8. solr6.6 配置自带中文分词

    1.配置solrconfig.xml solr的自带中文分词包在solr-6.6.0\contrib\analysis-extras\lucene-libs下 修改solrconfig.xml增加 & ...

  9. uva 10934 装满水的气球

    题意和思路见: http://blog.csdn.net/shuangde800/article/details/11273123 我的想法: 首先问题转化一下 将问题转化成:定义f[i][j] 表示 ...

  10. 解决ie下载apk后更改后缀名为.zip的问题

    转自:http://www.cnblogs.com/niuniu/archive/2012/03/06/2381811.html 解决: 服务器添加MIME类型: application/vnd.an ...