• URAL 1152 Faise Mirrors 状压DP 简单题


    1152. False Mirrors

    Time limit: 2.0 second
    Memory limit: 64 MB

    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

    inputoutput
    7
    3 4 2 2 1 4 1
    
    9

    题意:

    有n个阳台围成一个环,编号为1~n,每一个阳台上有一个敌人,每一个敌人有一个伤害值。

    你有一把枪,这把枪的功能是:

    每次你射编号为j的阳台,j相邻的2个阳台也同时会被射中。

    但是,每次你开一枪后,剩下的敌人都会给你一次伤害。

    问:在你消灭所有敌人的时候,你受到的最小伤害是多少。

    由n的范围,明显是状态压缩了。

    二进制表示时,1表示这个敌人还没有被杀,0表示这个敌人被杀了。

    dp[i]表示还剩下i的二进制的敌人时,受到的最小伤害。

    则:初始化:dp[(1<<n)-1]

      目标:dp[0]

    sum[i]表示还剩下i的二进制表示的敌人时,能够给人造成的伤害。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 const int inf=0x3f3f3f3f;
     8 const int maxn=22;
     9 
    10 int dp[1<<maxn];
    11 int sum[1<<maxn];
    12 int w[maxn];
    13 
    14 int main()
    15 {
    16     int n;
    17     while(scanf("%d",&n)!=EOF)
    18     {
    19         for(int i=0;i<n;i++)
    20             scanf("%d",&w[i]);
    21         for(int i=0;i<(1<<n);i++)
    22             dp[i]=inf;
    23         dp[(1<<n)-1]=0;
    24 
    25         memset(sum,0,sizeof(sum));
    26         for(int i=0;i<=(1<<n);i++)
    27         {
    28             for(int j=0;j<n;j++)
    29                 if(i&(1<<j))
    30                     sum[i]+=w[j];
    31         }
    32 
    33         for(int i=(1<<n)-1;i>=0;--i)
    34         {
    35             for(int j=0;j<n;j++)
    36             {
    37                 if(!(i&(1<<j)))
    38                     continue;
    39                 int x=i-(1<<j);
    40                 int t1=j-1;
    41                 if(t1<0)
    42                     t1=n-1;
    43                 int t2=j+1;
    44                 if(t2>=n)
    45                     t2=0;
    46                 if(x&(1<<t1))
    47                     x-=(1<<t1);
    48                 if(x&(1<<t2))
    49                     x-=(1<<t2);
    50                 dp[x]=min(dp[x],dp[i]+sum[x]);
    51             }
    52         }
    53         printf("%d
    ",dp[0]);
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    mysql 遇到的一些问题
    Nginx 深入浅出学习
    spring十三种代理之 -代理模式
    spring 十三种模式之 -- 装饰器模式
    layui使用遇到的一些问题-- 数据表格中嵌套下拉框
    格式化时间-标准做法
    provide --- inject
    vue窗口最上边显示路由进度条
    element --- el-popover
    多个音频播放时,只能播放一个
  • 原文地址:https://www.cnblogs.com/-maybe/p/4548095.html
Copyright © 2020-2023  润新知