False Mirrors URAL - 1152 [状压dp + dfs记忆搜索]

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
Problem Author: Eugene Bryzgalov 
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round 

思路:状压dp;每个阳台只有两种状态,n<20;

状态 : dp[sta] 表示sta状态下发起攻击后取得的最小伤害值;

状态转移方程 dp[sta] = dp[temp] + damage;

代码:

#include<bits/stdc++.h>
#define ll long long;
using namespace std;
const int maxn = 27;
const int INF = 0x3f3f3f3f;
int a[maxn], dp[1<<21], n;
//dp[sta] :表示sta状态下发起攻击后取得的最小伤害值;
//状态转移方程 dp[sta] = dp[temp] + damage;

int dfs(int sta)
{
    if(dp[sta] != INF) return dp[sta];
    for(int i = 1; i <= n; i++)
    {
        if(sta&(1<<(i - 1)))
        {
            int l, r, temp = sta;
            if(i == 1) l = n; else l = i - 1;
            if(i == n) r = 1; else r = i + 1;
            temp -= 1<<(i - 1);
            if(temp & (1<<(l - 1))) temp -= 1<<(l - 1);
            if(temp & (1<<(r - 1))) temp -= 1<<(r - 1);
            int damage = 0;
            for(int j = 1; j <= n; j++) if(temp&(1<<(j - 1))) damage += a[j];
            dp[sta] = min(dp[sta], dfs(temp) + damage);
        }
    }
    return dp[sta];
}




int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n))
    {
        memset(dp, INF, sizeof(dp));
        dp[0] = 0;
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        printf("%d\n", dfs((1<<n) - 1));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值