ZOJ Problem Set - 2297 Survival 【状压dp】

题目:ZOJ Problem Set - 2297 Survival

题意:给出一些怪,有两个值,打他花费的血和可以增加的血,然后有一个boss,必须把小怪所有都打死之后才能打boss,血量小于0会死,也不能大于100.

分析:定义状态:dp【st】,表示在 st 状态下的血量。

然后转移:dp【st】 = max (dp【st】,dp【st&~(1<<i )】+p[i].first - p[i].second);

注意初始化的时候必须在开始初始化,否则容易出错。

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const long long N = 22;
const int inf = 0x3f3f3f3f;
int dp[1<<N];
pair<int,int> p[N];
int main()
{
    int n,boss;
    while(~scanf("%d",&n) && n)
    {
        n--;
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            p[i]=make_pair(x,y);
        }
        scanf("%d",&boss);
        memset(dp,0,sizeof(dp));
        int ans = 0;
        dp[0]=100;
        for(int st=1;st<(1<<n);st++)
        {
            dp[st]=-inf;  //必须在这里初始化
            for(int j=0;j<n;j++)
                if(st&(1<<j)&&dp[st&~(1<<j)]>=p[j].first)
                {
                    dp[st]=max(dp[st],dp[st&~(1<<j)]-p[j].first+p[j].second);
                    if(dp[st]>100)
                        dp[st]=100;
                }
        }
        if(dp[(1<<n)-1]>=boss)
            puts("clear!!!");
        else
            puts("try again");
    }
    return 0;
}
时间: 2024-10-19 07:41:06

ZOJ Problem Set - 2297 Survival 【状压dp】的相关文章

zoj 2297 Survival 状压dp

Description The King of Fighter 97 (KOF97) is an electronic game of wrestling type. Once it was fashionable among youths. The game is amused. However, playing by oneself is not as excited as with friends. Getting bored? Maybe you can alter to the sur

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit

ZOJ 3471 Most Powerful(状压DP)

Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way every two at

ZOJ 3802 Easy 2048 Again 状压DP

直接从前往后DP,因为一共只有500个数,所以累加起来的话单个数不会超过4096,并且因为是Flappy 2048的规则,所以只有之后数列末尾一串递减的是有效的,因此可以状压. 1700ms = =,据说用滚动数组优化一下会好很多 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector>

ZOJ 2563 Long Dominoes(状压DP)

给定一个m*n的方格子,要求用3*1的骨牌去覆盖,骨牌可以用横放或者竖放,问最终有多少种放置方式,将其铺满. 分析:由于最多30行,每行最多9列,所以可以按行来dp,设计每行的状态从而进行转移,考虑每个骨牌放置对下一行的影响,共有0,1,2,3种方式,0对应横放或者竖放时最下面那 个格子,此行对下一行没有影响,1,竖放时第1个,2竖放时第2个,这样进行转移.注意,第i行横放时要求上一行相应位置状态为0. 思路及代码都来自这里,其实不会做这题,看了才了解. 代码: 1 #include <bits

ZOJ 2563 Long Dominoes(状压DP)题解

题意:n*m的格子,用1 * 3的矩形正好填满它,矩形不能重叠,问有几种填法 思路:poj2411进阶版.我们可以知道,当连续两行的摆法确定,那么接下来的一行也确定.当第一行还有空时,这时第三行必须要用3 * 1的去填:当第一行没有空第二行有空时,第三行必须不填:当第一行有空第二行没空,这种不能存在:当前两行没空时,我最多就是填1 * 3的方块. 代码: #include<set> #include<map> #include<cmath> #include<qu

状压DP [ZOJ 3471] Most Powerful

Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a

lightoj 1119 - Pimp My Ride(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1119 题解:状压dp存一下车有没有被搞过的状态就行. #include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long ll; ll dp[1 << 15] , val[15][15]; int main() {

zoj zju 2994 Tiling a Grid With Dominoes 状压dp

Tiling a Grid With Dominoes Time Limit: 2 Seconds      Memory Limit: 65536 KB We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five differen