数据结构与算法——马踏棋盘(c++栈实现)

马踏棋盘问题是旅行商(TSP)或哈密顿问题(HCP)的一个特例。在国际棋盘棋盘上,用一个马按照马步跳遍整个棋盘,要求每个格子都只跳到一次,最后回到出发点。这是一个 NP问题,通常采用回溯法或启发式搜索类算法求解。 

在此采用栈进行回溯法求解

#include <iostream>
#include<stdlib.h>
#include <stdio.h>
#include <iomanip>
using namespace std;
const int StackInitSize=10;
const int StackInc=1;
typedef struct {
    int x;
    int y;
} Position;
typedef struct
{
    int n=5;//迷宫规模n*n
    int Board[10][10]={{0}};//棋盘最大规模
    Position start;//马开始位置
}ChessBoard;
typedef struct {
    int ord;
    //顺序(第几步)
    Position seat;
    //位置
    int di;
    //查找方向
} SElemType;//马的当前状态
struct SStack
{
    SElemType * base,*top;
    int stacksize;
};
bool StackInit(SStack &S)
{
    S.base=new SElemType[StackInitSize];
    if(!S.base)
        return false;
    S.top=S.base;
    S.stacksize=StackInitSize;
    return true;
}
bool Push(SStack &S,SElemType e)
{
    SElemType *base;
    if(S.top-S.base==S.stacksize)
    {
        base=(SElemType*)realloc(S.base,(S.stacksize+StackInc)*sizeof(SElemType));
        if(!base)
            return false;
        S.base=base;
        S.top=S.base+S.stacksize;
        S.stacksize+=StackInc;
    }
     *S.top=e;
     S.top++;
     return true;
}
bool Pop(SStack &S,SElemType &e)
{
    if(S.top==S.base)
        return false;
    S.top--;
    e=*S.top;
    return true;
}
void HorseOnBoard(int n,int x,int y)
{
    ChessBoard CBoard;
    CBoard.start.x=x;
    CBoard.start.y=y;
    CBoard.n=n;
    int h_move[8][2]={{2,1},{-1,2},{1,2},{-2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
    int step=0;
    int i=0;
    SStack S;
    StackInit(S);
    Position p=CBoard.start;
    SElemType horse;
    horse.di=0;
    horse.ord=step;
    horse.seat=CBoard.start;
    Push(S,horse);
do{
       bool f=Pop(S,horse);
       if(!f)
           break;
       if (step>=1)
        {
            i=horse.di+1;
            CBoard.Board[horse.seat.x][horse.seat.y]=0;
            step--;

        }
        for(i;i<8;i++)
        {
            p.x=horse.seat.x+h_move[i][0];
            p.y=horse.seat.y+h_move[i][1];
            if(CBoard.Board[p.x][p.y]==0&&(p.x<=CBoard.n-1)&&(p.x>=0)&&(p.y<=CBoard.n-1)&&(p.y>=0))
            {
                step++;
                horse.di=i;
                horse.ord=step;
                Push(S,horse);
                CBoard.Board[horse.seat.x][horse.seat.y]=step;
                horse.seat=p;
                i=0;

                if(step==CBoard.n*CBoard.n-1)
                {
                    step++;
                    CBoard.Board[horse.seat.x][horse.seat.y]=step;
                    Push(S,horse);
                }
                continue;
            }

        }

    }while(step<CBoard.n*CBoard.n-1);

    if(step<CBoard.n*CBoard.n)
       cout<<"找不到合适的路径"<<endl;
    else
    {
          cout<<"马在"<<n<<"*"<<n<<"的棋盘上的足迹如下:"<<endl;
          for (int a = 0; a < n; a++) {
            for(int b = 0; b < n; b++) {
                cout<<setw(2)<<CBoard.Board[a][b]<<setw(2)<<' ';
                }
            cout<<' '<<endl;
    }
    }

}
int main()
{
    HorseOnBoard(5,0,0);
    return 0;
}

 

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值