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;
}


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


实验10 综合作业—基础 本次作业的任务是进行图片分类实验,实验数据集由20个类别共2000张图片组成,其中1000张图片用于训练,另1000张图片用于测试(详情见附录A)。 图片分类程序的常见步骤 【建议】图片分类程序可以分为以下5个步骤: (1)为训练图片提取底层特征,用特征向量表示训练图片; (2)利用训练图片的特征向量和类别标注数据训练分类器; (3)为测试图片提取底层特征,用特征向量表示测试图片; (4)使用训练好的分类器对测试图片的特征向量进行预测分类; (5)对预测分类的结果进行评测。 底层特征 【要求】在本次大作业中,请大家查阅相关资料, 要求采用以下特征,对不同特征进行融合,并对比分析不同特征融合的结果: (1)灰度共生矩特征 (Gray Level Co-occurrence Matrix) (2)SIFT (Scale-invariant feature transform)特征。提示:图像的SIFT特征不能直接用于图像分类的输入,因为维度不一致,所以需要结合BOW(词袋)方法获取一副图像的SIFT统计特征,作为一副图像的SIFT特征。 (3)纹理特征。 (4)颜色特征。 (5)CNN特征。 【建议】对于以上特征可以尝试多种途径来提升效果; 分类方法 【要求】在本次大作业中,至少要用到以下分类器: (1)SVM:SVM是一种被广泛采用的分类方法,可从sklearn包中导入SVM模块,如:from sklearn import svm; (2)或自选另外至少一种分类方法。 【建议】可以根据数据本身的特点, 合理改进分类器 (如改进SVM的核函数等) 【建议】可以在实验过程中,观察不同的分类方法的效果优劣,并加以分析。 【建议】可以对多个分类器的分类结果进行融合, 提高最终的分类效果。 统一的预测分类结果文件格式 为了便于进行图片分类结果的评测和比较,在实验的过程中请将图片分类结果输出为以下统一的格式: (1)每一行由3个数字组成,分别是图片(在整个图片集中)的序号、图片的真实(标注)类别、图片分类程序的预测类别; (2)由于共有1000张图片,该文件共有1000行。 50 1 1 51 1 3 … 1999 20 20 结果评测指标 图片分类实验的评测指标平均准确率A是所有类别的准确率的平均值,即: A=(A1+A2+…+A20)/ 20 其中Ai是对类别i的测试图片进行预测分类的准确率,定义如下: Ai=Ri / Ni 其中Ri是类别i的测试图片中预测分类正确的图片的数目, Ni是类别i的测试图片的数目(对于本次作业的图片集Ni=50)。 结果显示 【要求】Confusion matrix: 将图片分类结果的Confusion matrix以一张图的形式显示出来。Confusion matrix是一个20x20的矩阵,其中的第(i,j)个元素,是测试图片中第i类图片被判断为第j类的概率。Confusion matrix中每一行中所有元素的和应为1。Confusion matrix中对角线上的第(i,i)个元素是第i类图片的分类准确率Ai,对角线上所有元素的平均值就是图片分类实验的平均准确率A。 【建议】尽量将Confusion matrix的图做的清楚,美观。例子:下面是一个6个类别的图片分类实验的Confusion matrix。 Confusion Matrix的数据: 0.9692 0.0031 0.0021 0.0051 0.0021 0.0185 0.0700 0.3762 0.0312 0.0425 0.3150 0.1650 0.0009 0.0009 0.9962 0.0009 0 0.0009 0.0504 0 0.0150 0.9283 0.0016 0.0047 0 0 0 0 0.9886 0.0114 0.0028 0.0083 0.0014 0 0.0028 0.9848 Confusion Matrix的图片显示,例如: 作业提交方式 本次作业至少需要提交以下内容: 提交内容 详细要求 作业文档 详细介绍采用的图片分类方法,包括使用的特征和分类方法的细节、最终的分类结果的混淆矩阵(Confusion Matrix)和图示结果等。 程序源代码 相关程序的全部源代码,要求能够正常编译和运行;如果程序中使用了网上的开源代码,请同时提供下载地址。 程序说明 详细说明如何编译源代码、如何运行图片分类程序、如何评测分类结果。 将根据“程序说明”编译你提交的源代码,并重新运行图片分类程序,如果出现代码无法编译、运行出错、运行结果与作业文档中的结果不一致等情况,在正常评分的基础上将酌情减分。 附录A:实验图片集 本次大作业采用的实验图片集含有20个类别共2000张图片(0.jpg-1999.jpg)每个类别各100张图片(其中训练图片50张,测试图片50),如下表所示。 类别 类别名称 训练图片 测试图片 0 African people and villages 0-49 50-99 1 Beach 100-149 150-199 2 Historical buildings 200-249 250-299 3 Buses 300-349 350-399 4 Dinosaurs 400-449 450-499 5 Elephants 500-549 550-599 6 Flowers 600-649 650-699 7 Horses 700-749 750-799 8 Mountains and glaciers 800-849 850-899 9 Food 900-949 950-999 10 Dogs 1000-1049 1050-1099 11 Lizards 1100-1149 1150-1199 12 Fashion 1200-1249 1250-1299 13 Sunsets 1300-1349 1350-1399 14 Cars 1400-1449 1450-1499 15 Waterfalls 1500-1549 1550-1599 16 Antiques 1600-1649 1650-1699 17 Battle ships 1700-1749 1750-1799 18 Skiing 1800-1849 1850-1899 19 Desserts 1900-1949 1950-1999严格按照要求一步一步教我写,因为我不会
05-22
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值