流程图
代码
- main.c
#include <stdio.h>
#include "stack.h"
#define ROW (8)
#define COL (8)
int map[ROW][COL] = {0};
int number = 0;
int isLegalCoordinate(int x, int y){
if (x < 0 || x >= COL || y < 0 || y >= ROW){
return 0;
}
else if (map[x][y] == 2){
return 0;
}
else {
return 1;
}
}
void explorPoint(STACK *stack, int x, int y){
NODE *top = getTopStack(stack);
if (isLegalCoordinate(x,y)){
NODE *p = (NODE *)malloc(sizeof(NODE));
map[x][y] = 2;
p->coordinate.x = x;
p->coordinate.y = y;
p->step = top->step + 1;
p->number = number++;
p->direction = 0;
p->lastnumber = top->number;
pushStack(stack, p);
free(p);
}
}
void outputArray(STACK *stack){
int x;
int y;
NODE *p = getTopStack(stack);
while (p != NULL){
x = p->coordinate.x;
y = p->coordinate.y;
map[x][y] = p->step;
p = p->next;
}
int i;
int j;
for (i = 0; i < ROW; i++){
for (j = 0; j < COL; j++){
printf("%3d ", map[i][j]);
}
printf("\n");
}
}
int main(){
STACK *stack = createStack();
int START_Point_X;
int START_Point_Y;
printf("请输入起点行列坐标:");
scanf("%d %d", &START_Point_X, &START_Point_Y);
NODE *start = (NODE *)malloc(sizeof(NODE));
start->coordinate.x = START_Point_X;
start->coordinate.y = START_Point_Y;
start->step = 0;
number = 1;
start->number = number++;
start->lastnumber = 0;
start->direction = 0;
map[START_Point_X][START_Point_Y] = 2;
pushStack(stack, start);
NODE *top;
int x;
int y;
while (1){
top = getTopStack(stack);
x = top->coordinate.x;
y = top->coordinate.y;
top->direction++;
if (top->direction <= 8){
switch(top->direction){
case 1:
explorPoint(stack, x-2, y+1);
break;
case 2:
explorPoint(stack, x-1, y+2);
break;
case 3:
explorPoint(stack, x+1, y+2);
break;
case 4:
explorPoint(stack, x+2, y+1);
break;
case 5:
explorPoint(stack, x+2, y-1);
break;
case 6:
explorPoint(stack, x+1, y-2);
break;
case 7:
explorPoint(stack, x-1, y-2);
break;
case 8:
explorPoint(stack, x-2, y-1);
break;
default :
break;
}
if (top->step == ROW*COL-1){
outputArray(stack);
break;
}
}
else {
map[top->coordinate.x][top->coordinate.y] = 0;
popStack(stack);
}
}
destroyStack(&stack);
return 0;
}
- stack.h
#ifndef QUEUE_H
#define QUEUE_H
#include <stdio.h>
#include <stdlib.h>
typedef struct coordinate{
int x;
int y;
}COORDINATE;
typedef struct node{
COORDINATE coordinate;
int step;
int number;
int lastnumber;
int direction;
struct node *next;
}NODE;
typedef struct stack{
NODE *top;
}STACK;
extern STACK *createStack();
extern int isEmptyStack(STACK *stack);
extern void pushStack(STACK *stack, NODE *p);
extern void popStack(STACK *stack);
extern void displayStack(STACK *stack);
extern NODE *getTopStack(STACK *stack);
extern void clrStack(STACK *stack);
extern void destroyStack(STACK **stack);
#endif
- stack.c
#include "stack.h"
STACK *createStack(){
STACK *stack = (STACK *)malloc(sizeof(STACK));
if (stack == NULL){
exit(0);
}
stack->top = NULL;
return stack;
}
int isEmptyStack(STACK *stack){
if (stack->top == NULL){
return 1;
}
return 0;
}
void pushStack(STACK *stack, NODE *p){
NODE *pnew= (NODE *)malloc(sizeof(NODE));
if (pnew == NULL){
exit(0);
}
pnew->coordinate.x = p->coordinate.x;
pnew->coordinate.y = p->coordinate.y;
pnew->step = p->step;
pnew->number = p->number;
pnew->lastnumber = p->lastnumber;
pnew->direction = p->direction;
pnew->next = stack->top;
stack->top = pnew;
}
void popStack(STACK *stack){
NODE *p;
if(!isEmptyStack(stack)){
p = stack->top;
stack->top = p->next;
free(p);
}
}
void displayStack(STACK *stack){
if(!isEmptyStack(stack)){
NODE *p = stack->top;
while(p != NULL){
printf("(%d,%d)", p->coordinate.x, p->coordinate.y);
printf("step:%d ", p->step);
printf("number:%d ", p->number);
printf("lastnumber:%d ", p->lastnumber);
printf("direction:%d \n", p->direction);
p = p->next;
}
}
}
NODE *getTopStack(STACK *stack){
if (!isEmptyStack(stack)){
return stack->top;
}
return NULL;
}
void clrStack(STACK *stack){
while (!isEmptyStack(stack)){
popStack(stack);
}
}
void destroyStack(STACK **stack){
clrStack(*stack);
free(*stack);
*stack = NULL;
}