Lizards and Basements 2 解题报告

Description

This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced.

Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each.

As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball.

The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies?

Polycarp can throw his fire ball into an archer if the latter is already killed.

Input
The first line of the input contains three integers n, a, b (3 ≤ n ≤ 101 ≤ b < a ≤ 10). The second line contains a sequence of nintegers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has.

Output

In the first line print t — the required minimum amount of fire balls.

In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order.

Sample Input
Input
3 2 1
2 2 2
Output
3
2 2 2
Input
4 3 1
1 4 1 1
Output
4
2 2 3 3

大意是n个Archer排成一排让法师的火球攻击,每个Archer都有自己的hp( h[i] ),hp低于0时会死亡。火球对目标i有一个主伤害a,对i+1和i-1有一个溅射伤害b。法师不能直接攻击到1号和n号。问至少要多少火球才能杀死所有Archer,并输出每个火球的攻击目标。
 
话说一般的游戏里hp为零时就会战斗不能,为了方便接下来处理,把所有的h[i]都加上1,h[i]+=1;

 首先分析火球的攻击顺序,将其从小到大排序后发现规律:对第i个敌人攻击时,i-1之前的敌人必定死亡,对第i个敌人攻击完毕后,第i-1个敌人必定死亡。

方法一:
从2到n进行搜索,攻击次数从至少杀死第i-1个敌人枚举到杀死i-1,i,i+1三个敌人,dfs(ep,num) ep为目前攻击对象,num为总的攻击次数,再加一个最优性剪枝 if (num>ans) return false;可解。
缺点:不够优雅。
图片

方法二:
DP f[i][hp1][hp2][hp3]
i表示第i个敌人,hp1表示第i-1个敌人的hp, hp2表示第i个敌人的hp,hp3表示第i+1个敌人的hp,保证i-1之前的敌人全部死亡。
f[i][hp1][hp2][hp3]表示达到此状态最小所需的火球数。
数组f初值为无穷大OO。
f[2][h[1]][h[2]][h[3]]初值为0。
方程:
d1=max(0,hp1-b);
d2=max(0,hp2-a);
d3=max(0,hp3-b);

if ( f[i][hp1][hp2][hp3]+1<
f[i][d1][d2][d3] ) f[i][d1][d2][d3]=f[i][hp1][hp2][hp3]+1;
//若从hp1,hp2,hp3到达d1,d2,d3状态所需的火球更少则 
d1,d2,d3由hp1,hp2,hp3状态到达。
 

if ( d1==0 && f[i][hp1][hp2][hp3]+1<f[i+1][d2][d3][h[i+2]] )  
f[i+1][d2][d3][h[i+2]]=f[i][hp1][hp2][hp3]+1;
//若攻击后,第i-1个敌人已经死亡则可以开始攻击第i+1个敌人。 

if ( hp1==0 && f[i][hp1][hp2][hp3]<f[i+1][d2][d3][h[i+2]] ) 
f[i+1][d2][d3][h[i+2]]=f[i][hp1][hp2][hp3];
//若还没攻击,第i-1个敌人就已经死了,则可以直接攻击第i+1个敌人。(我还没用力,你就倒下了
⊙﹏⊙b)

用数组p[][][][]记录路径。

然后...就没有然后了,此题可解。
 
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#define OO 999999
using namespace std;

int n,a,b,tmp,tp,kill;
int h[30];
int f[30][30][30][30];
int p[30][30][30][30];
int d1,d2,d3;
int th1,th2,th3;
int ph1,ph2,ph3;
int main()
{
    //input
    maxh=0;
    scanf("%d%d%d",&n,&a,&b);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&h[i]);
        h[i]+=1;
    }
    h[n+1]=0;
    //初始化数组f为OO
    for (int i=0;i<30;i++)
    {
        for (int j=0;j<30;j++)
        {
            for (int k=0;k<30;k++)
            {
                for (int l=0;l<30;l++)
                {
                    f[i][j][k][l]=OO;
                }
            }
        }
    }
    //起始状态
    f[2][h[1]][h[2]][h[3]]=0;
    //DP
    for (int i= 2;i<n;i++)
        for (int hp1=h[i-1];hp1>=0;hp1--)
            for (int hp2=h[i];hp2>=0;hp2--)
                for ( int hp3=h[i+1];hp3>=0;hp3--)
                    if (f[i][hp1][hp2][hp3]!=OO)
                    {
                        d1=max(0,hp1-b);
                        d2=max(0,hp2-a);
                        d3=max(0,hp3-b);
                        if (f[i][hp1][hp2][hp3]+1<f[i][d1][d2][d3])
                        {
                            f[i][d1][d2][d3]=f[i][hp1][hp2][hp3]+1;
                            p[i][d1][d2][d3]=i*30*30*30+hp1*30*30+hp2*30+hp3;
                        }
                        if (d1==0)
                        {
                            if (f[i][hp1][hp2][hp3]+1<f[i+1][d2][d3][h[i+2]])
                            {
                                f[i+1][d2][d3][h[i+2]]=f[i][hp1][hp2][hp3]+1;
                                p[i+1][d2][d3][h[i+2]]=i*30*30*30+hp1*30*30+hp2*30+hp3;
                            }
                        }
                        if (hp1==0)
                        {
                            if (f[i][hp1][hp2][hp3]<f[i+1][hp2][hp3][h[i+2]])
                            {
                                f[i+1][hp2][hp3][h[i+2]]=f[i][hp1][hp2][hp3];
                                p[i+1][hp2][hp3][h[i+2]]=p[i][hp1][hp2][hp3];
                            }
                        }
                    }
    //output
    printf("%d\n",f[n][0][0][0]);
    ph1=0;
    ph2=0;
    ph3=0;
    tmp=n;
    while ( !(ph1==h[1]&&ph2==h[2]&&ph3==h[3]&&tmp==2) )
    {
        tp=p[tmp][ph1][ph2][ph3];
        th1=tp/30/30%30;
        th2=tp/30%30;
        th3=tp%30;
        kill=tp/30/30/30;
        printf("%d \n",kill);
        ph1=th1;
        ph2=th2;
        ph3=th3;
        tmp=kill;
    }
    printf("\n");
    return 0;
}


//数组好像开大了.... 


posted on 2013-01-30 10:07  电子幼体  阅读(245)  评论(0编辑  收藏  举报

导航