-
Notifications
You must be signed in to change notification settings - Fork 0
/
小马快跑.c
291 lines (241 loc) · 5.36 KB
/
小马快跑.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*************************************************************************
> File Name: 小马快跑.c
> Author: yunting
> Mail: 1368282581@qq.com
> Created Time: 2015年10月09日 星期五 14时26分58秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define N 8 //棋盘大小
#define Stack_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
int weight[N][N];
typedef struct
{
int x;
int y;
}PosType;
typedef struct
{
int ord;
PosType seat;
int di;
}ElemType;
typedef struct Stacknode
{
ElemType data;
struct Stacknode *next;
}StackNode;
typedef struct
{
StackNode *top;
}LinkStack;
//初始化一个空栈
Set_Empty_Stack(LinkStack *S)
{
S -> top = NULL;
}
//判断栈空操作
int Is_Empty_Stack(LinkStack *S)
{
if(S -> top == NULL) {
return 1;
} else {
return 0;
}
}
//进栈操作
int Push(LinkStack *S, ElemType *node)
{
StackNode *p;
p = (StackNode *) malloc (sizeof(StackNode));
if(p == NULL) {
fprintf(stderr, "line:%d\n", __LINE__);
return 0;
} else {
p->data = *node;
p->next = S -> top;
S -> top = p;
return 1;
}
}
//出栈操作
int Pop(LinkStack *S, ElemType *node)
{
StackNode *p;
if(Is_Empty_Stack(S)) {
printf("the stack is empty!pop\n");
return 0;
} else {
p = S -> top;
*node = p -> data;
S -> top = p -> next;
free(p);
return 1;
}
}
//取栈顶的值
int Get_Stack_Top(LinkStack *S, ElemType *node)
{
if(Is_Empty_Stack(S)) {
printf("the stack is empty!get_stack_top\n");
return 0;
} else {
*node = S -> top -> data;
return 1;
}
}
//8个候选方向
PosType NextPos(PosType curpos, int direction)
{
switch(direction)
{
case 1:
curpos.x += 1; curpos.y -= 2; break;
case 2:
curpos.x += 2; curpos.y -= 1; break;
case 3:
curpos.x += 2; curpos.y += 1; break;
case 4:
curpos.x += 1; curpos.y += 2; break;
case 5:
curpos.x -= 1; curpos.y += 2; break;
case 6:
curpos.x -= 2; curpos.y += 1; break;
case 7:
curpos.x -= 2; curpos.y -= 1; break;
case 8:
curpos.x -= 1; curpos.y -= 2; break;
}
return curpos;
}
//判断位置是否合法
int Can_Walk(PosType curpos)
{
if(curpos.x<0 || curpos.x>(N-1) || curpos.y<0 || curpos.y>(N-1)) {
return 0;
}
return 1;
}
PosType Get_Start_Seat()
{
PosType seat;
printf("please input the seat:\n\tX:");
scanf("%d", &seat.x);
printf("\tY:");
scanf("%d", &seat.y);
while(Can_Walk(seat) == 0) {
printf("\nthe seat is error ! please input again\n");
printf("\tX:");
scanf("%d",&seat.x);
printf("\n\tY:");
scanf("%d", &seat.y);
}
return seat;
}
int main()
{
LinkStack *S = NULL;
PosType start, next, st;
int g, i, j,horsestep = 0, test;
ElemType *p, *ptemp, *top, *a, *b, *c, *d;
int step[N][N];
int ords = 0;
int flag = 0, flag1=0;
memset(step, 0, sizeof(step));
S = (LinkStack *) malloc (sizeof(LinkStack));
Set_Empty_Stack(S); //初始化一个空栈
start = Get_Start_Seat(); //得到起始点
p = (ElemType *) malloc (sizeof(ElemType));
p->ord = ++ords;
p->seat.x = start.x;
p->seat.y = start.y;
p->di = 0;
if(Push(S, p) == 0) { //将初始位置压入栈
fprintf(stderr, "line:%d\n", __LINE__);
};
weight[p->seat.x][p->seat.y] = 1;
horsestep++; //马走一步
do{
flag++;
if(flag == 1000) {
printf("202\n");
return 0;
}
for(i=0; i<8; i++) {
next = NextPos(start, i+1);
if((weight[next.x][next.y] == 0) && (Can_Walk(next) == 1)) {
step[start.x][start.y] = i+1; //访问方式,从周边1-8访问,step记录该点被访问到的数
break;
}
}
if(i == 8) {
step[start.x][start.y] = 9;
}
while(step[start.x][start.y] > 8) {
flag1 ++;
c = (ElemType *) malloc (sizeof(ElemType));
if(Pop(S, c) == 0 ) { //取栈顶a
fprintf(stderr, "line:%d\n", __LINE__);
}
ords--;
weight[c->seat.x][c->seat.y] = 0;
horsestep--;
d = (ElemType *) malloc (sizeof(ElemType));
if(Get_Stack_Top(S, d) == 0) { //得到栈顶元素
fprintf(stderr, "line:%d\n", __LINE__);
}
st.x = d -> seat.x; //栈顶元素
st.y = d -> seat.y;
g = step[st.x][st.y];
if(g == 8) {
step[start.x][start.y] = 9;
} else {
for(i = g; i < 8; i++) {
next = NextPos(st, i+1);
if((weight[next.x][next.y] == 0) && (Can_Walk(next) == 1)) {
step[st.x][st.y] = i+1;
break;
}
}
if(i == 8) {
step[start.x][start.y] = 9;
} else {
step[start.x][start.y] = step[st.x][st.y];
}
}
free(c);free(d);
}
ptemp = (ElemType *) malloc (sizeof(ElemType));
ptemp -> ord = ++ords;
ptemp -> seat.x = next.x;
ptemp -> seat.y = next.y;
ptemp -> di = step[start.x][start.y];
if(Push(S, ptemp) == 0) {
fprintf(stderr, "line:%d\n", __LINE__);
}
weight[ptemp->seat.x][ptemp->seat.y] = 1;
horsestep++;
top = (ElemType *) malloc (sizeof(ElemType)); //为指针top分配空间
Get_Stack_Top(S, top); //取得栈顶值存入top
start.x = top -> seat.x;
start.y = top -> seat.y;
free(top);
free(ptemp);
} while(horsestep < 61);
memset(weight, 0, sizeof(weight));
for(i=0; i<horsestep; i++) {
a = (ElemType *) malloc (sizeof(ElemType));
Pop(S, a);
weight[a->seat.x][a->seat.y] = a->ord;
free(a);
}
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
printf("%5d", weight[i][j]);
}
printf("\n");
}
}