1152. False Mirrors
Time limit: 2.0 second
Memory limit: 64 MB
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
input | output |
---|---|
7 3 4 2 2 1 4 1 | 9 |
/**参考:http://blog.csdn.net/domacles0124/article/details/9698001 **/
/**记忆化搜索+状态DP**/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 1051001
#define inf 0x7fffffff
int n;
int a[22];
int sum[maxn];
int dp[maxn];
int read()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
memset(dp,-1,sizeof(dp));//便于区分哪个状态未进行处理
dp[0]=0;//根据题目,当没有怪物时,消耗为0
}
int DFS(int s)//根据状态进行搜索,状态用2进制表示,某一位上为1,说明对应的阳台有怪物
{
if(dp[s]!=-1)
return dp[s];
dp[s]=inf;
for(int i=0;i<n;i++)
{
if(s&(1<<i))//若i位置有怪物,就以i为射击中点,枚举i
{
int tmp=s;
tmp&=~(1<<((i-1+n)%n));//一枪射掉连续的三个阳台 , 这是去掉第一个
tmp&=~(1<<i); //这是第二个
tmp&=~(1<<((i+1)%n));//这是第三个
int res=0;
for(int j=0;j<n;j++)
if(tmp&(1<<j))//将剩余位置的怪物数量加起来,就是消耗的血量
res+=a[j];
dp[s]=min(dp[s],res+DFS(tmp));//dp取枚举之后的最小值
}
}
return dp[s];
}
void print()
{
printf("%d\n",DFS((1<<n)-1));
}
int main()
{
read();
print();
return 0;
}