博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ 1107 模拟 水
阅读量:7307 次
发布时间:2019-06-30

本文共 6305 字,大约阅读时间需要 21 分钟。

题目链接:

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

我的代码较长。

#include 
#include
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
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

你可能感兴趣的文章
基于Hadoop的云盘系统客户端技术选型说明
查看>>
linux下C语言多线程编程实例--修改后
查看>>
我的友情链接
查看>>
更新脚本
查看>>
用Android-X86和VMware打造高性能Android开发环境
查看>>
我的友情链接
查看>>
学习Hadoop找到的一些资源
查看>>
经纬度精度差别
查看>>
【08】分析类
查看>>
垃圾收集的种类
查看>>
HRegionServer启动后自动关闭的问题
查看>>
maven插件assembly利用profiles打不同环境发布包
查看>>
Android系统学习总结1--init和Zygote
查看>>
参考人体排毒时间表与自我时间调整(与诸君共勉)
查看>>
深入字节码 -- 使用 ASM 实现 AOP
查看>>
CListCtrl使用技巧
查看>>
怎样进行软件过程改进
查看>>
php读取excel类——PHP-ExcelReader
查看>>
内存监控工具
查看>>
linux 下查看一个进程运行路径
查看>>