Untitled
raw download clone
C
views 9
,
size 2218 b
12144

#include <stdio.h>
#include <stdlib.h>

typedef struct _Obj
{
    int hp, atk, x, y;
} Obj;

typedef struct _Mon
{
    Obj m[1005];
} Mon;

int k;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int visited[10][10];
Obj *temp_return_from_encounter = NULL;
Obj *encounter(Obj Hero, Mon mon)
{
    for (int i = 0; i < k; i++)
    {

        if (mon.m[i].x == Hero.x && mon.m[i].y == Hero.y && mon.m[i].hp > 0)
        {

            temp_return_from_encounter = &(mon.m[i]);
            return temp_return_from_encounter;
        }
    }
    return NULL;
}
int battle(Obj *Hero, Obj *m)
{
    if (m->x == Hero->x && m->y == Hero->y && m->hp > 0)
    {

        int round = (m->hp / Hero->atk) + (m->hp % Hero->atk == 0 ? 0 : 1);
        Hero->hp -= (round - 1) * (m->atk);
        
        m->hp = 0;
        m->x = 0;
        m->y = 0;


        if (Hero->hp <= 0)
            return 0;
        else
            return 1;
    }
    return 1;
}
int can_go(int pos_x, int pos_y)
{

    if (pos_x < 1 || pos_x > 5 || pos_y < 1 || pos_y > 5 || visited[pos_x][pos_y] == 1)
        return 0;
    return 1;
}
int dfs(int x, int y, Obj Hero, Mon mon)
{
    Hero.x = x;
    Hero.y = y;

    temp_return_from_encounter = encounter(Hero, mon);
    if (temp_return_from_encounter != NULL)
    {
        if (!battle(&Hero, temp_return_from_encounter))
        {
            return 0;
        }
    }

    if (x == 5 && y == 5)
        return 1;

    for (int i = 0; i < 4; i++)
    {
        if (can_go(x + dir[i][0], y + dir[i][1]))
        {
            visited[x + dir[i][0]][y + dir[i][1]] = 1;
            if (dfs(x + dir[i][0], y + dir[i][1], Hero, mon))
            {
                return 1;
            }
            visited[x + dir[i][0]][y + dir[i][1]] = 0;
        }
    }
    return 0;
}

int main(void)
{
    Obj Hero;
    Mon mon;
    scanf("%d %d %d", &Hero.hp, &Hero.atk, &k);
    for (int i = 0; i < k; i++) {
        scanf("%d%d%d%d", &mon.m[i].hp, &mon.m[i].atk, &mon.m[i].x, &mon.m[i].y);
    }

    if (dfs(1, 1, Hero, mon)) printf("HEIR OF FIRE DESTROYED\n");
    else printf("YOU DIED\n");
}
close fullscreen
Login or Register to edit or fork this paste. It's free.