题目链接:
这题条件较多,模拟过程较复杂,但只要细心些就行。
我的代码较长。
#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;}