URAL1152——状态压缩+DP——False Mirrors

Description

Background

We wandered in the labyrinth for twenty minutes before finally entering the large hall. The walls were covered by mirrors here as well. Under the ceiling hung small balconies where monsters stood. I had never seen this kind before. They had big bulging eyes, long hands firmly holding riffles and scaly, human-like bodies. The guards fired at me from the balconies, I shot back using my BFG-9000. The shot shattered three mirrors filling the room with silvery smoke. Bullets drummed against my body-armor knocking me down to the floor. Falling down I let go a shot, and got up as fast as I fell down by rotating on my back, like I did in my youth while break dancing, all this while shooting three more times. Three mirrors, three mirrors, three mirrors…
Sergey Lukjanenko, “The Labyrinth of Reflections”

Problem

BFG-9000 destroys three adjacent balconies per one shoot. (  N-th balcony is adjacent to the first one). After the shoot the survival monsters inflict damage to Leonid (main hero of the novel) — one unit per monster. Further follows new shoot and so on until all monsters will perish. It is required to define the minimum amount of damage, which can take Leonid.

Input

The first line contains integer  N, аmount of balconies, on which monsters have taken a circular defense. 3 ≤  N ≤ 20. The second line contains  N integers, amount of monsters on each balcony (not less than 1 and no more than 100 on each).

Output

Output minimum amount of damage.

Sample Input

inputoutput
7
3 4 2 2 1 4 1
9
 大意:先输入一个n,表示有n个炮台的怪物,每次你只能破坏三个炮台,剩下的怪物会攻击中间的英雄,问你把所有怪物消灭完之前,英雄所承受的最小的伤害是多少,注意1-n是一个圈,用状态压缩枚举每一种情况
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 0x3f3f3f3f;
int dp[1<<20];
int sum[1<<20];
int a[110];
int n;
int main()
{
    scanf("%d",&n);
    for(int i = 0 ; i < n ; i++)
        scanf("%d",&a[i]);
    for(int i = 0 ; i < (1<<n);i++){
        dp[i] = maxn;
        for(int j = 0 ; j <n ; j++){
            if(i&(1<<j))
                sum[i] += a[j];
        }
    }
    dp[(1<<n)-1] = 0;
    int t1,t2,x;
    for(int i = (1<<n)-1;i >= 0;i--){
        for(int j = 0 ; j < n ;j++){
            if(i&(1<<j)){
             x= i - (1<<j);
            if(j-1<0)
               t1 = n -1;//找上一个炮台
            else t1 = j - 1;
            if(j + 1 >= n)
                t2 = 0;
            else t2 = j + 1;
            if(x&(1<<t1))
                x -= (1<<t1);
            if(x&(1<<t2))
                x -= (1<<t2);
            dp[x] = min(dp[x],dp[i]+sum[x]);
            }
        }
    }
    printf("%d\n",dp[0]);
    return 0;
}

  

转载于:https://www.cnblogs.com/zero-begin/p/4492227.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值