Skip to content

Commit bede715

Browse files
Dungeon game
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent f6ce20d commit bede715

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

174_dungeon_game/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test dungeon.c

174_dungeon_game/dungeon.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <limits.h>
4+
5+
static int calculateMinimumHP(int** dungeon, int dungeonRowSize, int dungeonColSize)
6+
{
7+
int i, j;
8+
int **dp = malloc(dungeonRowSize * sizeof(int *));
9+
for (i = 0; i < dungeonRowSize; i++) {
10+
dp[i] = malloc(dungeonColSize * sizeof(int));
11+
}
12+
13+
for (i = dungeonRowSize - 1; i >= 0; i--) {
14+
for (j = dungeonColSize - 1; j >= 0; j--) {
15+
if (i == dungeonRowSize - 1 && j == dungeonColSize - 1) {
16+
dp[i][j] = 1 - dungeon[i][j] > 1 ? 1 - dungeon[i][j] : 1;
17+
} else {
18+
int hp1 = i == dungeonRowSize - 1 ? INT_MAX : (dp[i + 1][j] - dungeon[i][j] > 1 ? dp[i + 1][j] - dungeon[i][j] : 1);
19+
int hp2 = j == dungeonColSize - 1 ? INT_MAX : (dp[i][j + 1] - dungeon[i][j] > 1 ? dp[i][j + 1] - dungeon[i][j] : 1);
20+
dp[i][j] = hp1 < hp2 ? hp1 : hp2;
21+
}
22+
}
23+
}
24+
25+
return dp[0][0];
26+
}
27+
28+
int main(void)
29+
{
30+
int row_size = 3, col_size = 3;
31+
int i, j, **dungeon = malloc(row_size * sizeof(int *));
32+
for (i = 0; i < row_size; i++) {
33+
dungeon[i] = malloc(col_size * sizeof(int));
34+
}
35+
#if 1
36+
dungeon[0][0] = 1;
37+
dungeon[0][1] = -3;
38+
dungeon[0][2] = 3;
39+
dungeon[1][0] = 0;
40+
dungeon[1][1] = -2;
41+
dungeon[1][2] = 0;
42+
dungeon[2][0] = -3;
43+
dungeon[2][1] = -3;
44+
dungeon[2][2] = -3;
45+
#else
46+
dungeon[0][0] = 3;
47+
dungeon[0][1] = -20;
48+
dungeon[0][2] = 30;
49+
dungeon[1][0] = -3;
50+
dungeon[1][1] = 4;
51+
dungeon[1][2] = 0;
52+
#endif
53+
54+
printf("dungeon:\n");
55+
for (i = 0; i < row_size; i++) {
56+
for (j = 0; j < col_size; j++) {
57+
printf("%d ", dungeon[i][j]);
58+
}
59+
printf("\n");
60+
}
61+
printf("%d\n", calculateMinimumHP(dungeon, row_size, col_size));
62+
return 0;
63+
}

0 commit comments

Comments
 (0)