我的程式大約都寫出來了...可是一直有bug~~
可否請大大幫忙找問題~~ >< 我是新手~~ ================================ #include
#include
#define N 5
typedef struct loc {
int x, y, d, k;
} LOC_S;
LOC_S stack[N*N];
int top = 0;
int a[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int b[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int chess[N][N]; int main(void)
{
int i, j;
int k = 1, x = 0, y = 0, d, n_x, n_y;
LOC_S knight;
for (i = 0; i < N; i )
for (j = 0; j < N; j )
chess[i][j] = 0;
chess[x][y] = k ;
d = 0;
while (k <= N * N)
{
for ( ;d < 8; d )
{
n_x = x a[d];
n_y = y b[d];
if (in_range(n_x,n_y)&&chess[n_y][n_x]==0)/*判斷有沒有超過陣列索引值,若超過的話傳回fault並跳出FOR迴圈*/ {
chess[n_y][n_x] = k;
knight.x = x;
knight.y = y;
knight.d = d;
knight.k = k;
push(knight);
d = 0;
x = n_x; y = n_y;
k ;
break;
}
}
if (d == 8)
{
knight = pop();
x = knight.x;
y = knight.y;
d = knight.d;
k = knight.k;
n_x = x a[d];
n_y = y b[d];
chess[n_y][n_x] = 0;
d ;
}
}
for(i=1;i<=5;i )
{
for(j=1;j<=5;j )
{
printf(" =", chess[i][j]);
}
printf("\n");
} return 0 ;
getch(); } stack* creatstack(void) //新增節點的副程式
{ stack* st;
st=(stack*)malloc(sizeof(stack));
st->count=0;
st->top=NULL;
return st;
} stack* push(stack* stack,int x,int y,int d)//新增座標內容到stack裡
{
stack_node* newptr;
newptr=(stack_node*)malloc(sizeof(stack_node));
newptr->x=x; //把新節點放到stack的第一個位置
newptr->y=y;
newptr->d=d;
newptr->link=stack->top;
stack->top=newptr;
return stack; }
stack* pop(stack* stack) //刪除節點
{
stack_node* ptr;
ptr=stack->top; //刪除第一個節點
stack->top=ptr->link; //將開頭的節點指向下一個節點
free(ptr);
return stack;
}