zoj 2297

[DP+位运算状态压缩]

#include <iostream>
#include <cstring>
using namespace std;

int dp[(1<<20)+10];

int main(){
    int people[20][2];
    int k;
    int n,boss;
    
    while (cin>>n) {// the number of opponents
        for (int i=0; i<n-1; i++) {
            cin>>people[i][0]>>people[i][1];// consume and recover
        }
        cin>>boss;
        
        int m=(1<<(n-1))-1;//2^(n-1)
        
        memset(dp, 0, sizeof(dp));
        
        dp[0]=100;//The initial HP of your character is 100.
        
        for (int i=0; i<=m; i++) {//every status
            for (int j=0; j<n-1; j++) {//n-1 roles
                k=1<<j;
                if (!(k&i)&&dp[i]>=people[j][0]) {//由状态i可以转化为i&k状态
                    int s=dp[i]+people[j][1]-people[j][0];
                    if (s>dp[i+k]) {
                        dp[i+k]=s;//原来可能已经有值,则选择较大的
                    }
                    if (dp[i+k]>100) {
                        dp[i+k]=100;
                    }
                }
            }
        }
        
        if (dp[m]>=boss) {
            cout<<"clear!!!"<<endl;
        }else{
            cout<<"try again"<<endl;
        }
    }
    
    return 0;
}



你可能感兴趣的:(zoj 2297)