HDU 1152. False Mirrors(DFS啊 )

题目链接:?space=1&num=1152

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 integerN, аmount of balconies, on which monsters have taken a circular defense. 3 ≤N≤ 20. The second line containsNintegers, 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

73 4 2 2 1 4 19

题意:

一共有n个阳台,首尾相接形成一个环;

阳台里有怪物,伤害为a[i],每次可以打掉三个阳台(打掉中间的一个左右相邻的也被打掉了),那么同时,没被打掉的那些怪物会对你造成响应a[i]的伤害。经过几次战斗,打掉所有阳台的怪物,问受到的伤害最小是多少。

代码如下:

#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double e = exp(1.0);#define INF 0x3f3f3f3fint n,sum;int a[47];int vis[47];int ans;void DFS(int num, int tt)//个数,伤害{if(tt >= ans){return ;//伤害大于总的怪兽数}if(num == 0)//怪兽已经没有了{ans = min(tt, ans);return ;}for(int i = 1; i <= n; i++)//围城一个圈{if(vis[i]){continue;}int t1 = i-1, t2 = i+1;if(t1 == 0){t1 = n;}if(t2 == n+1){t2 = 1;}int flag1 = 0, flag2 = 0;if(vis[t1]){flag1 = 1;}if(vis[t2]){flag2 = 1;}int num1 = 0, num2 = 0;if(!flag1){num1 = a[t1];vis[t1] = 1;//死掉}if(!flag2){num2 = a[t2];vis[t2] = 1;//死掉}int tt_num = a[i]+num1+num2;num-=tt_num;vis[i] = 1;tt+=num;DFS(num, tt);tt-=num;num+=tt_num;vis[i] = 0;if(!flag1){vis[t1] = 0;}if(!flag2){vis[t2] = 0;}}}int main(){while(~scanf("%d",&n)){memset(vis, 0,sizeof(vis));memset(a, 0,sizeof(a));ans = INF;int tol = 0;a[0] = 0;for(int i = 1; i <= n; i++){scanf("%d",&a[i]);tol += a[i];}DFS(tol, 0);printf("%d\n",ans);}return 0;}

,如果说对云南有进一步的了解的话就是鲜花。

HDU 1152. False Mirrors(DFS啊 )

相关文章:

你感兴趣的文章:

标签云: