1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
| #define _CRT_SECURE_NO_WARNINGS 1 #pragma warning(disable:4996) //#pragma execution_character_set("utf-8") #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <windows.h>
#define ERROR 0; #define OK 1;
#define Map_col 10 #define Map_row 10 // 1:障碍 // 2: 路径 // 0:空地 // -1: 标记为空地不可走。
#define MAXSIZE 100 #define SIZEADD 10 typedef struct { int y; // 行 int x; // 列 }ElemType;
typedef struct { ElemType* top; ElemType* base; int stacksize; }SqStack;
void init_map(int choice, int map[][Map_col]); void show_map(int map[][Map_col]); void init_stack(SqStack& T); int Get_top_elem(SqStack T, ElemType& e); int choose_direction(int map[][Map_col], SqStack T, ElemType& out); void push_stack(SqStack& T, int map[][Map_col], ElemType e); void pop_stack(SqStack& T, int map[][Map_col], ElemType& e); int if_win(const SqStack T); void solve_path(SqStack& T, int map[][Map_col]); void clear_stack(SqStack& T); void destroy_stack(SqStack& T);
int main() { SqStack T; //用顺序栈储存 要走过的格子的坐标 init_stack(T); int map[Map_col][Map_row];
while (TRUE) { char buffer[4]; int choice; fputs("\n请选择关卡(1,2,3), -1退出。\n: ", stdout); fgets(buffer, 3, stdin); choice =(int)strtol(buffer,NULL ,10); if (choice == -1) { fputs("\n游戏结束,再见!!", stdout); break; } else if (choice == 1 || choice == 2 || choice == 3) { init_map(choice, map); show_map(map); solve_path(T, map); } else fputs("输入不合法", stdout); }
return 0; } void solve_path(SqStack &T,int map[][Map_col]) { ElemType e; e.x = 1; e.y = 1; push_stack(T, map,e);// 先入起点 //map[e.y][e.x] = 2; ElemType in; ElemType out;
while (TRUE) { if (if_win(T) == 1) { fputs("解答成功!!\n", stdout); show_map(map); break; } else if(if_win(T) == -1) { fputs("\n本关卡无解!!请更换有效地图\n", stdout); break; } if (choose_direction(map, T, in)) { push_stack(T, map, in); // map[in.y][in.x] = 2; } else { pop_stack(T,map, out); // map[out.y][out.x] = -1; } //fputs("\n", stdout); //Sleep(1000); //show_map(map); } clear_stack(T); } int if_win(const SqStack T) { if (T.base == T.top) return -1;//游戏无解 ElemType now = *(T.top-1);
if (now.x == Map_col - 2 && now.y == Map_row - 2) { return 1; //找到路径 } else return 0; //还未找到 } void destroy_stack(SqStack& T) { if (T.base != NULL) { // 检查栈是否已初始化 free(T.base); // 释放栈底内存 T.base = NULL; T.top = NULL; T.stacksize = 0; } } void clear_stack(SqStack& T) { if (T.base != NULL) { T.top = T.base; } } //入栈 void push_stack(SqStack& T,int map[][Map_col], ElemType e) { if (T.top - T.base >= T.stacksize) { T.base = (ElemType*)realloc(T.base, (T.stacksize + SIZEADD) * sizeof(ElemType)); if (!T.base) { perror("malloc"); exit(-1); } T.top = T.base + T.stacksize; T.stacksize += SIZEADD; } *T.top++ = e; map[e.y][e.x] = 2; } void pop_stack(SqStack& T,int map[][Map_col], ElemType &e) { if (T.base == T.top) exit(-1); e = * --T.top; map[e.y][e.x] = -1; } int choose_direction(int map[][Map_col], SqStack T, ElemType &out) { ElemType now_pos; ElemType round_pos[4]; Get_top_elem(T,now_pos); // 定位 上下左右 四个格子 round_pos[0].x = now_pos.x; round_pos[0].y = now_pos.y - 1; round_pos[1].x = now_pos.x - 1; round_pos[1].y = now_pos.y; round_pos[2].x = now_pos.x + 1; round_pos[2].y = now_pos.y; round_pos[3].x = now_pos.x; round_pos[3].y = now_pos.y + 1; int j = 0; ElemType chose[4]; for (int i = 0; i < 4; i++) { //计算这个格子四周有多少个可走格子 if (map[round_pos[i].y][round_pos[i].x] == 0) { chose[j].x = round_pos[i].x; chose[j].y = round_pos[i].y; j++; } } if (j == 0) { return 0; //表示 该出栈了。 } else { srand(time(NULL)); int temp = rand() % j; out = chose[temp]; return 1; } } int Get_top_elem(SqStack T, ElemType &e) { if (T.base == T.top) return ERROR; e = *(T.top - 1); return OK; } void init_stack(SqStack& T) { T.top = (ElemType*)malloc(MAXSIZE*sizeof(ElemType)); T.base = T.top; //T.top = T.top + 1; T.stacksize = MAXSIZE; } void show_map(int map[][Map_col]) { for (int i = 0; i < Map_row; i++) { for (int j = 0; j < Map_col; j++) { switch (map[i][j]) { case -1: case 0: fputs(" ", stdout); break; case 1: fputs(" ■", stdout); break; case 2: fputs(" *", stdout); break; default: break; } } fputs("\n", stdout); }
} void init_map(int choice, int map[][Map_col]) { FILE* fp; fp = NULL; switch (choice) { case 1: fp = fopen("./map//Le1.txt", "r"); break; case 2: fp = fopen("./map/Le2.txt", "r"); break; case 3: fp = fopen("./map/Le3.txt", "r"); break; //fp = fopen("./map/Le3.txt", "r"); default: break; }
if (!fp) { perror("fopen"); exit(-1); }
for (int i = 0; i < Map_row; i++) { for (int j = 0; j < Map_col; j++) { fscanf(fp, "%d", &map[i][j]); } } fclose(fp); fp = NULL; }
|