C程序-马踏棋盘

原理

马踏棋盘原理就是,在一个8*8方格中,定义一个起点根据马走日的原理,将方格中所有位置都走一遍,但不走重复的位置。

栈、结构体法

#include <stdlib.h>
#include <stdio.h>
#define ROW 8
#define COL 8
#define DIR 8

//初始化结构体
struct Node
{
    int x;
    int y;
    int dir;
    int count;
    struct Node *next;
};

//新建头节点
struct Node *head;
//新增头节点
void push(struct Node *node);
//返回头节点
struct Node *pop();
//删除指定结点
void delete ();
//打印节点
void print();

void printNode(struct Node *node);
//初始化节点
void initChess(int chess[ROW][COL]);
//开始执行马踏棋盘
void playChess(int chess[ROW][COL]);
//打印马踏棋盘
void printChess(int chess[ROW][COL]);
//新建添加节点
struct Node *createNode(int x, int y, int dir, int count);

int main()
{
    head = NULL;

    //struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    //node->x = 0;
    //node->y = 0;
    //node->dir = 0;
    //node->next = NULL;
    //push(node);

    //struct Node* node2 = (struct Node*)malloc(sizeof(struct Node));
    //node2->x = 1;
    //node2->y = 1;
    //node2->dir = 1;
    //node2->next = NULL;
    //push(node2);

    //print();

    //struct Node* node3 = pop();
    //printNode(node3);

    //delete();
    //free(node3);

    //print();

    int chess[ROW][COL];
    initChess(chess);
    playChess(chess);
    printChess(chess);
}

void push(struct Node *node)
{
    if (NULL == head)
    {
        head = node;
        head->next = NULL;
    }
    else
    {
        node->next = head;
        head = node;
    }
}

struct Node *pop()
{
    return head;
}

void delete ()
{
    head = head->next;
}

void print()
{
    struct Node *node = head;
    while (node != NULL)
    {
        printNode(node);
        node = node->next;
    }
}
//打印结构体内容
void printNode(struct Node *node)
{
    printf("x=%d,y=%d,dir=%d\n", node->x, node->y, node->dir);
}

void initChess(int chess[ROW][COL])
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            chess[i][j] = 0;
        }
    }
}

void playChess(int chess[ROW][COL])
{
    int x = 0, y = 0, dir = -1;
    printf("请您输入马踏棋盘开始坐标:");
    scanf("%d %d", &x, &y);
    int xDir[DIR] = {2, 2, 1, 1, -2, -2, -1, -1};
    int yDir[DIR] = {-1, 1, -2, 2, -1, 1, -2, 2};

    chess[x][y] = 1;
    struct Node *node = createNode(x, y, dir, 1);
    push(node);

    struct Node *currentNode = pop();

    while (NULL != currentNode)
    {
        if (currentNode->count == 64)
        {
            break;
        }

        int findNext = 0;
        for (int currentDir = (currentNode->dir + 1); currentDir < DIR; currentDir++)
        {
            int nextX = currentNode->x + xDir[currentDir];
            int nextY = currentNode->y + yDir[currentDir];
            if (nextX >= 0 && nextX < ROW && nextY >= 0 && nextY < COL && 0 == chess[nextX][nextY])
            {
                chess[nextX][nextY] = currentNode->count + 1;

                currentNode->dir = currentDir;

                struct Node *nexNode = createNode(nextX, nextY, -1, currentNode->count + 1);
                push(nexNode);
                findNext = 1;
                break;
            }
        }

        if (0 == findNext)
        {
            chess[currentNode->x][currentNode->y] = 0;
            delete ();
            free(currentNode);
        }

        currentNode = pop();
    }
}

void printChess(int chess[ROW][COL])
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            printf("%-4d", chess[i][j]);
        }
        printf("\n");
    }
}

struct Node *createNode(int x, int y, int dir, int count)
{
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    node->x = x;
    node->y = y;
    node->dir = dir;
    node->count = count;
    node->next = NULL;
    return node;
}

回溯法

#include <stdio.h>
#define X 8
#define Y 8
#define a 0
int chess[X][Y];

int nextxy(int* x, int* y, int count)  //找到基于x,y位置的下一个可走的位置
{
    switch (count)
    {
    case 0:
        if (*x + 2 <= X - 1 && *y - 1 >= 0 && chess[*x + 2][*y - 1] == 0)
        {
            *x = *x + 2;
            *y = *y - 1;
            return 1;
        }
        break;
    case 1:
        if (*x + 2 <= X - 1 && *y + 1 <= Y - 1 && chess[*x + 2][*y + 1] == 0)
        {
            *x = *x + 2;
            *y = *y + 1;
            return 1;
        }
        break;
    case 2:
        if (*x + 1 <= X - 1 && *y - 2 >= 0 && chess[*x + 1][*y - 2] == 0)
        {
            *x = *x + 1;
            *y = *y - 2;
            return 1;
        }
        break;
    case 3:
        if (*x + 1 <= X - 1 && *y + 2 <= Y - 1 && chess[*x + 1][*y + 2] == 0)
        {
            *x = *x + 1;
            *y = *y + 2;
            return 1;
        }
        break;
    case 4:
        if (*x - 2 >= 0 && *y - 1 >= 0 && chess[*x - 2][*y - 1] == 0)
        {
            *x = *x - 2;
            *y = *y - 1;
            return 1;
        }
        break;
    case 5:
        if (*x - 2 >= 0 && *y + 1 <= Y - 1 && chess[*x - 2][*y + 1] == 0)
        {
            *x = *x - 2;
            *y = *y + 1;
            return 1;
        }
        break;
    case 6:
        if (*x - 1 >= 0 && *y - 2 >= 0 && chess[*x - 1][*y - 2] == 0)
        {
            *x = *x - 1;
            *y = *y - 2;
            return 1;
        }
        break;
    case 7:
        if (*x - 1 >= 0 && *y + 2 <= Y - 1 && chess[*x - 1][*y + 2] == 0)
        {
            *x = *x - 1;
            *y = *y + 2;
            return 1;
        }
        break;
    default:
        break;
    }
    return 0;
}

int TravelChessBoard(int x, int y, int tag)  //深度优先搜索地"马踏棋盘"
{
    int x1 = x, y1 = y, flag = 0, count = 0;
    chess[x][y] = tag;
    if (tag == X * Y)
    {
        return 1;
    }
    flag = nextxy(&x1, &y1, count);
    while (flag == 0 && count < 7)
    {
        count = count + 1;
        flag = nextxy(&x1, &y1, count);
    }
    while (flag)
    {
        if (TravelChessBoard(x1, y1, tag + 1))
            return 1;
        x1 = x;
        y1 = y;
        count = count + 1;
        flag = nextxy(&x1, &y1, count);  //寻找下一个(x,y)
        
        while (flag == 0 && count < 7)
        {  //循环地寻找下一个(x,y)
            count = count + 1;
            flag = nextxy(&x1, &y1, count);
        }
    }
    if (flag == 0)
        chess[x][y] = 0;
    //printf("循环次数:%d\n", a);
    return 0;
}
int main()
{
    int i, j , d ,b;
    for (i = 0; i < X; i++)
        for (j = 0; j < Y; j++)
            chess[i][j] = 0;
    printf("请输入初始坐标:");
    scanf_s("%d %d",&d, &b);
    if (TravelChessBoard(d, b, 1))
    {
        for (i = 0; i < X; i++)
        {
            for (j = 0; j < Y; j++)
                printf("%-5d", chess[i][j]);
            printf("\n");
        }
        printf("The horse has travelled the chess borad\n");
    }
    else
        printf("The horse cannot travel the chess board\n");
    return 0;
}

贪心法

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 8
#define COL 8
#define MAX_STEPS ROW*COL //64

//栈结构体
typedef struct stack {
    int x_adr;      //纵坐标
    int y_adr;      //横坐标
    int direction;  //方向
}HORSE_CHESS_STACK;

//存储路径棋盘
int chess[ROW + 1][COL + 1];

//下一步方向
//一号
int dir[8][2] = { {2,-1},{-2,-1},{-2,1},{2,1},
                 {1,-2},{-1,-2},{-1,2},{1,2} };

//二号
// int dir[8][2] = {{1,2},{-1,-2},{-2,1},{2,1},
//                {2,-1},{1,-2},{-1,2},{-2,-1}};

                  //栈顶
int top;
HORSE_CHESS_STACK Adr_Stack[MAX_STEPS];

//出栈次数
int out_stack;

//初始化数据
void init() {
    int n = MAX_STEPS;//N=64
    while (n--) {
        Adr_Stack[n].x_adr = 0;
        Adr_Stack[n].y_adr = 0;
        Adr_Stack[n].direction = -1;
    }
    Adr_Stack[0].x_adr = 0;
    Adr_Stack[0].y_adr = 0;
    Adr_Stack[0].direction = -1;

    for (int i = 1; i <= ROW; i++) {
        for (int j = 1; j <= COL; j++) {
            chess[ROW][COL] = 0;
        }
    }

    top = -1;
    out_stack = 0;
}

//debug 打印栈的情况
void print_stack() {
    printf("栈的情况:\n");
    for (int i = 0; i < MAX_STEPS; i++) {
        printf("x:%d  y:%d direction = %d\n", Adr_Stack[i].y_adr, Adr_Stack[i].x_adr, Adr_Stack[i].direction);
    }
    printf("\n\n");
}

//入栈
void push_stack(int x_real, int y_real) {
    top++;
    Adr_Stack[top].x_adr = x_real;
    Adr_Stack[top].y_adr = y_real;
    Adr_Stack[top].direction = -1;  //初始化走的方向
}

//出栈
void pop_stack() {
    Adr_Stack[top].x_adr = 0;
    Adr_Stack[top].y_adr = 0;
    Adr_Stack[top].direction = -1;  //初始化走的方向
    top--;
}

//标记位置
void mark_chess(int x, int y) {
    chess[y][x] = top + 1;
}

//打印路径
void print_chess_board() {
    printf("\nroute:\n");
    for (int i = 1; i <= ROW; i++) {
        for (int j = 1; j <= ROW; j++) {
            printf("%4d ", chess[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

//打印每一步的位置
int t = 1;
void print_steps() {
    printf("(%d,%d)", Adr_Stack[top].y_adr, Adr_Stack[top].x_adr);
    t++;
    if (t == ROW) {
        printf("\n");
        t = 0;
    }
}


void run_horse_tanxin() {
    int x_now, y_now;
    while (1) {

        //已经走完全图
        if (top >= MAX_STEPS - 1) {
            //打印棋盘
            print_chess_board();
            break;
        }

        //现在位置
        x_now = Adr_Stack[top].x_adr;
        y_now = Adr_Stack[top].y_adr;

        //对方向进行排序
        int next[ROW] = {};
        for (int i = 0; i < ROW; i++) {
            //下一步坐标
            int x_next = x_now + dir[i][0];
            int y_next = y_now + dir[i][1];

            if ((x_next > 0 && x_next <= COL) && (y_next > 0 && y_next <= ROW) && chess[y_next][x_next] == 0) {
                //对下一步的下一步判断是否可以走
                for (int j = 0; j < ROW; j++) {
                    int x_next_next = x_next + dir[j][0];
                    int y_next_next = y_next + dir[j][1];

                    //可以走,next 对应下标+1
                    if ((x_next_next > 0 && x_next_next <= COL) && (y_next_next > 0 && y_next_next <= ROW) && chess[y_next_next][x_next_next] == 0) {
                        next[i]++;
                    }
                }
            }
        }

        //依次返回 next 中最小元素的下标,返回后将元素赋值为最大
        int real_next[8] = { 0 };
        int k = 0;
        int t = ROW + 1;
        for (int i = 0; i < ROW; i++) {
            t = ROW + 1;
            for (int j = 0; j < 8; j++) {
                if (next[j] < t) {
                    real_next[i] = j;
                    t = next[j];
                    k = j;
                }
            }
            next[k] = ROW + 1;
        }

        //走下一步
        int dir_now = 0;
        for (dir_now = Adr_Stack[top].direction + 1; dir_now < ROW; dir_now++) {
            int x_real = x_now + dir[real_next[dir_now]][0];
            int y_real = y_now + dir[real_next[dir_now]][1];

            Adr_Stack[top].direction += 1;

            if ((x_real <= COL && x_real > 0) && (y_real <= ROW && y_real > 0) && chess[y_real][x_real] == 0) {
                //入栈
                push_stack(x_real, y_real);
                //标记棋盘
                mark_chess(x_real, y_real);
                break;
            }
        }

        //如果下一步走不了,则出栈回溯
        if (Adr_Stack[top].direction >= 7) {
            printf("\n out:(%d,%d) \n", Adr_Stack[top].y_adr, Adr_Stack[top].x_adr);
            chess[Adr_Stack[top].y_adr][Adr_Stack[top].x_adr] = 0;
            //记录出栈次数
            out_stack++;
            pop_stack();
        }

        //打印栈
        // print_stack();
        //打印走了以后所处位置
print_steps();
    }
}

int main() {
    int st_x, st_y;
    while (1) {
        printf("Please input: x  y :");
        scanf_s("%d %d", &st_x, &st_y);

        if (st_x > 0 && st_x <= ROW && st_y > 0 && st_y <= COL) {
            printf("Get x and y success!\n");
            break;
        }

        printf("Input wrong!please input x y again:");
    }
    init();
    //print_stack();
    //获得开始时间
    clock_t start = clock();

    //将起始位置入栈
    push_stack(st_x, st_y);
    //标记起始位置
    mark_chess(st_x, st_y);

    printf("\nroute address:\n");
    printf("(%d,%d)", st_x, st_y);

    //开始算法
    run_horse_tanxin();

    //计算结束时间
    clock_t finish = clock();
    double run_time = (double)(finish - start) / CLOCKS_PER_SEC;

    printf("Running time:%f seconds  \nOut stack times:%d\n", run_time, out_stack);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值