栈-马踏棋盘深度探索(非递归)

流程图

在这里插入图片描述

代码

  1. 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);
//				displayStack(stack);
				break;
			}
		}
		else {
			map[top->coordinate.x][top->coordinate.y] = 0;
			popStack(stack);
		}
	}
	
    destroyStack(&stack);

    return 0;
}

  1. 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
  1. 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值