The Cat Kingdom is under attack by monsters from another dimension. To defend their homeland, warriors of various cat tribes have gathered to repel the invaders.
Your task is to simulate the battle on a 3 × 5 battlefield, managing the summoning of cat warriors and the invasion of monsters, and calculating the outcome of each second based on specific rules.
This is a partial judge problem, please trace the code and check the following description.
The main.c file handles the simulation of a 3 × 5 battlefield where cats and monsters interact over a given period. It begins by reading the details of all cat tribes, including their name, attack power, and attack speed.
For each time step, it processes events such as summoning new cats or introducing monsters.
When a cat is summoned, the program places it on the battlefield if space is available, or output a message if the battlefield is full.
If two cats of the same type and level are present, they merge into a higher-level cat. Monsters are also introduced with specific names and health points.
At each time step, the program calculates the total attack power of the cats and applies it to the first monster.
If a monster’s health reaches zero, it is removed. The program outputs the battlefield state for each second.
#include <stdio.h>
#include "function.h"
#define TABLE_ROW 3
#define TABLE_COLUMN 5
#define MAX_CAT_TYPE 5
#define MAX_MONSTER_NUM 100
Table *table[TABLE_ROW][TABLE_COLUMN];
Cat *cat[MAX_CAT_TYPE];
Monster *monster[MAX_MONSTER_NUM];
int catType, gameOverTime;
int eventType = 0, monsterCount = 0,newMonsterCount = 0;
int main() {
scanf("%d", &catType);
for(int i=0; i<catType; i++) cat[i] = newCatType();
scanf("%d", &gameOverTime);
for(int i=1; i<=gameOverTime; i++){
printf("Time %d:\n",i);
scanf("%d",&eventType);
if(eventType == 1){
int placed = 0;
for(int j=0; j<TABLE_ROW*TABLE_COLUMN; j++){
int x = j / TABLE_COLUMN;
int y = j % TABLE_COLUMN;
if(table[x][y] == NULL){
table[x][y] = newCat(cat);
printf("A new %s is summon to (%d,%d).\n",table[x][y]->cat->catName, x, y);
placed = 1;
break;
}
}
if (!placed) printf("No space to summon a new cat.\n");
for(int j=0; j<TABLE_ROW*TABLE_COLUMN; j++){
int x1 = j / TABLE_COLUMN;
int y1 = j % TABLE_COLUMN;
for(int k=0; k<TABLE_ROW*TABLE_COLUMN; k++){
if(j == k) continue;
int x2 = k / TABLE_COLUMN;
int y2 = k % TABLE_COLUMN;
if(table[x1][y1] != NULL && table[x2][y2] != NULL){
if(table[x1][y1]->cat->catType == table[x2][y2]->cat->catType && table[x1][y1]->catLevel == table[x2][y2]->catLevel){
table[x1][y1]->catLevel++;
table[x2][y2] = deleteCat(table[x2][y2]);
printf("The cat located at (%d, %d) and the cat located at (%d, %d) have merged and leveled up.\n", x1, y1, x2, y2);
printf("The new %s is now located at (%d, %d) and has reached level %d.\n", table[x1][y1]->cat->catName, x1, y1, table[x1][y1]->catLevel);
k = -1;
}
}
}
}
}
else if(eventType == -1){
monster[newMonsterCount] = newMonster();
printf("New monster %s (HP : %d)\n", monster[newMonsterCount]->monsterName, monster[newMonsterCount]->hp);
newMonsterCount++;
}
int attackSum = getAttackSum(table,i);
if(monsterCount<newMonsterCount && monster[monsterCount] != NULL){
monster[monsterCount]->hp -= attackSum;
if(monster[monsterCount]->hp <= 0){
printf("Monster %s is dead.\n", monster[monsterCount]->monsterName);
monster[monsterCount] = deleteMonster(monster[monsterCount]);
monsterCount++;
}
}
}
}
function.h
Cat* newCatType()
:
Cat
struct, and returns it.Table* newCat(Cat **catTypes)
:
Table* deleteCat(Table *table)
:
free()
)Monster* newMonster()
:
int getAttackSum(Table *table[][5], int currentTime)
:
Monster* deleteMonster(Monster *monster)
:
free()
)
#ifndef __FUNCTION_H__
#define __FUNCTION_H__
typedef struct {
char *catName;
int attack;
int speed;
int catType;
} Cat;
typedef struct {
int catLevel;
Cat *cat;
} Table;
typedef struct {
char *monsterName;
int hp;
} Monster;
Cat* newCatType();
Table* newCat(Cat **catTypes);
Table* deleteCat(Table *table);
Monster* newMonster();
int getAttackSum(Table *table[][5], int currentTime);
Monster* deleteMonster(Monster *monster);
#endif
The first line contains an integer N (number of cat tribes).
The next N lines each describe a cat tribe with:
The next one line containing a positive integer T, the duration of the simulation (in seconds).
The next T lines, one for each second, describe the event occurring at that time:
Constraints
(This part is already implemented in main.c)
For each second, first output : Time [CurrentTime]:
Then output the events and battlefield status according to the following rules:
A new [Scat] is summon to ([x],[y]).
No space to summon a new cat.
The cat located at ([x1],[y1]) and the cat located at ([x2],[y2]) have merged and leveled up.
The new [Scat] is now located at ([x1],[y1]) and has reached level [level].
New monster [Smon] (HP : [hp])
Monster [Smon] is dead.