HDOJ 1107 模拟 水

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1107

这题条件较多,模拟过程较复杂,但只要细心些就行。

我的代码较长。

#include <iostream>
#include <vector>
using namespace std;

const int SIZE = 12;

class Disciple
{
public:
    //门派
    char group[2];
    //内力,武力,生命值
    int innerVal, physicalVal, healthVal;
    //坐标
    int coor_x, coor_y;
    //是否朝正方向走
    int isForward;
};

//武林大地图
vector<Disciple> bigMap[2][SIZE + 1][SIZE + 1];

//清空地图
void clearBigMap()
{
    for (int k = 0;k <= 1;k ++)
        for (int i = 1;i <= SIZE;i ++)
            for (int j = 1;j <= SIZE;j ++)
                bigMap[k][i][j].clear();
}


int getAttackVal(Disciple &aDisciple)
{
    double attackVal;
    if (strcmp(aDisciple.group, "S") == 0)
        attackVal = (0.5 * aDisciple.innerVal + 0.5 * aDisciple.physicalVal) * (aDisciple.healthVal + 10) / 100;
    else if (strcmp(aDisciple.group, "W") == 0)
        attackVal = (0.8 * aDisciple.innerVal + 0.2 * aDisciple.physicalVal) * (aDisciple.healthVal + 10) / 100;
    else if (strcmp(aDisciple.group, "E") == 0)
        attackVal = (0.2 * aDisciple.innerVal + 0.8 * aDisciple.physicalVal) * (aDisciple.healthVal + 10) / 100;
    //转成int
    return (int)attackVal;
}

void attack(int mapId)
{
    for (int i = 1;i <= SIZE;i ++)
        for (int j = 1;j <= SIZE;j ++)
        {
            //只有同一格子里面有两个不同派别的弟子时,才会战斗
            if ( bigMap[mapId][i][j].size() == 2 && (strcmp(bigMap[mapId][i][j][0].group, bigMap[mapId][i][j][1].group) != 0) )
            {
                //得到两个弟子的攻击力
                int attackValA = getAttackVal(bigMap[mapId][i][j][0]);
                int attackValB = getAttackVal(bigMap[mapId][i][j][1]);
                //掉血
                bigMap[mapId][i][j][0].healthVal -= attackValB;
                bigMap[mapId][i][j][1].healthVal -= attackValA;
            }
        }
}

void getNextPos(Disciple &aDisciple)
{
    //少林弟子
    if (strcmp(aDisciple.group, "S") == 0)
    {
        //正在朝下走
        if (aDisciple.isForward == 1)
        {
            if (aDisciple.coor_x + 1 > SIZE)
            {
                aDisciple.coor_x --;
                aDisciple.isForward = 0;
            }
            else
                aDisciple.coor_x ++;
        }
        //正在朝上走
        else
        {
            if (aDisciple.coor_x - 1 < 1)
            {
                aDisciple.coor_x ++;
                aDisciple.isForward = 1;
            }
            else
                aDisciple.coor_x --;
        }
    }
    //武当弟子
    else if (strcmp(aDisciple.group, "W") == 0)
    {
        //正在朝右走
        if (aDisciple.isForward == 1)
        {
            if (aDisciple.coor_y + 1 > SIZE)
            {
                aDisciple.coor_y --;
                aDisciple.isForward = 0;
            }
            else
                aDisciple.coor_y ++;
        }
        //正在朝左走
        else
        {
            if (aDisciple.coor_y - 1 < 1)
            {
                aDisciple.coor_y ++;
                aDisciple.isForward = 1;
            }
            else
                aDisciple.coor_y --;
        }
    }
    //峨眉弟子
    else if (strcmp(aDisciple.group, "E") == 0)
    {
        //如果位于右上角或左下角,永远走不动
        if ((aDisciple.coor_x == 1 && aDisciple.coor_y == 12) || (aDisciple.coor_x == 12 && aDisciple.coor_y == 1))
            return;
        //朝右下走
        if (aDisciple.isForward == 1)
        {
            if ((aDisciple.coor_x + 1 > SIZE) || (aDisciple.coor_y + 1 > SIZE))
            {
                aDisciple.coor_x --, aDisciple.coor_y --;
                aDisciple.isForward = 0;
            }
            else
                aDisciple.coor_x ++, aDisciple.coor_y ++;
        }
        //朝左上走
        else
        {
            if ((aDisciple.coor_x - 1 < 1) || (aDisciple.coor_y - 1 < 1))
            {
                aDisciple.coor_x ++, aDisciple.coor_y ++;
                aDisciple.isForward = 1;
            }
            else
                aDisciple.coor_x --, aDisciple.coor_y --;
        }
    }
}

void move(int mapId)
{
    for (int i = 1;i <= SIZE;i ++)
        for (int j = 1;j <= SIZE;j ++)
        {
            for (int k = 0;k < bigMap[mapId][i][j].size();k ++)
            {
                //血量大于0才能存活下去
                if (bigMap[mapId][i][j][k].healthVal <= 0)
                    continue;
                getNextPos(bigMap[mapId][i][j][k]);
                int xx = bigMap[mapId][i][j][k].coor_x, yy = bigMap[mapId][i][j][k].coor_y;    
                bigMap[1 - mapId][xx][yy].push_back(bigMap[mapId][i][j][k]);
            }
        }
    //清空mapId地图
    for (int i = 1;i <= SIZE;i ++)
        for (int j = 1;j <= SIZE;j ++)
            bigMap[mapId][i][j].clear();
}

void printAns(int mapId)
{
    int memsNum[3], totalHealthVal[3];
    memset(memsNum, 0, sizeof(memsNum));
    memset(totalHealthVal, 0, sizeof(totalHealthVal));
    for (int i = 1;i <= SIZE;i ++)
        for (int j = 1;j <= SIZE;j ++)
            for (int k = 0;k < bigMap[mapId][i][j].size();k ++)
            {
                if (strcmp(bigMap[mapId][i][j][k].group, "S") == 0)
                    memsNum[0] ++, totalHealthVal[0] += bigMap[mapId][i][j][k].healthVal; 
                else if (strcmp(bigMap[mapId][i][j][k].group, "W") == 0)
                    memsNum[1] ++, totalHealthVal[1] += bigMap[mapId][i][j][k].healthVal;
                else if (strcmp(bigMap[mapId][i][j][k].group, "E") == 0)
                    memsNum[2] ++, totalHealthVal[2] += bigMap[mapId][i][j][k].healthVal;
            }
    for (int i = 0;i < 3;i ++)
        printf("%d %d\n", memsNum[i], totalHealthVal[i]);
    printf("***\n");
}

int main ()
{
    int casesNum;
    scanf("%d",&casesNum);
    for (int caseId = 1;caseId <= casesNum;caseId ++)
    {
        clearBigMap();
        int stepsNum;
        //步数
        scanf("%d",&stepsNum);
        char str[2];
        //读到“0”结束
        Disciple aDisciple;
        while (scanf("%s",str) != -1 && strcmp(str, "0") != 0)
        {
            strcpy(aDisciple.group, str);
            scanf("%d%d%d%d%d",&aDisciple.coor_x, &aDisciple.coor_y, &aDisciple.innerVal, &aDisciple.physicalVal, &aDisciple.healthVal);
            aDisciple.isForward = 1;
            bigMap[0][aDisciple.coor_x][aDisciple.coor_y].push_back(aDisciple);
        }
        int mapId = 0;
        //模拟stepsNum步
        for (int i = 1;i <= stepsNum;i ++)
        {
            attack(mapId);
            move(mapId);
            mapId = 1 - mapId;
        }
        printAns(mapId);
    }
    return 0;
}

转载于:https://www.cnblogs.com/peaceful-andy/archive/2012/09/04/2670880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值