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
",dp[0]);
    return 0;
}

  

原文地址:https://www.cnblogs.com/zero-begin/p/4492227.html