如何使用C++实现马踏棋盘

发布时间:2022-02-15 14:48:13 作者:小新
来源:亿速云 阅读:138

这篇文章主要介绍如何使用C++实现马踏棋盘,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

马踏棋盘,用1枚马走遍棋盘。我用一个二维数组记录模拟的整个路径,x为列,y为行,以顺时针的方式寻找下一格,算法比较简单,就通过递归和循环回溯即可,就是如果是8*8的数组,最坏可能执行8^(x*y)次,耗时长到怀疑人生。

如何使用C++实现马踏棋盘

#include<iostream>
#define X 5
#define Y 5
 
void ShowResult();
using namespace std;
 
int chess[Y][X]={
    0
};
int counter=0;
 
int Next(int* x,int* y,int where){
 
    switch(where){
        case 0:
            if(*x+1<X&&*y-2>=0&&chess[*y-2][*x+1]==0){
                *x+=1;
                *y-=2;
                return 1;
            }
            break;
        case 1:
            if(*x+2<X&&*y-1>=0&&chess[*y-1][*x+2]==0){
                *x+=2;
                *y-=1;
                return 1;
            }
            break;
        case 2:
            if(*x+2<X&&*y+1<Y&&chess[*y+1][*x+2]==0){
                *x+=2;
                *y+=1;
                return 1;
            }
            break;
        case 3:
            if(*x+1<X&&*y+2<Y&&chess[*y+2][*x+1]==0){
                *x+=1;
                *y+=2;
                return 1;
            }
            break;
        case 4:
            if(*x-1>=0&&*y+2<Y&&chess[*y+2][*x-1]==0){
                *x-=1;
                *y+=2;
                return 1;
            }
            break;
        case 5:
            if(*x-2>=0&&*y+1<Y&&chess[*y+1][*x-2]==0){
                *x-=2;
                *y+=1;
                return 1;
            }
            break;
        case 6:
            if(*x-2>=0&&*y-1>=0&&chess[*y-1][*x-2]==0){
                *x-=2;
                *y-=1;
                return 1;
            }
            break;
        case 7:
            if(*x-1>=0&&*y-2>=0&&chess[*y-2][*x-1]==0){
                *x-=1;
                *y-=2;
                return 1;
            }
            break;
    }
    return 0;
}
 
int Explore(int x,int y){
    int x1=x;
    int y1=y;
    int flag;
    int where=0;
    
 
    counter++;
    chess[y][x]=counter;
    
 
    if(counter==X*Y){
        return 1;
    }
    
    
    
    flag=Next(&x1,&y1,where);
    while(flag==0&&where<7){
        where++;
        flag=Next(&x1,&y1,where);
    }
    
    
    while(flag){
        if(Explore(x1,y1)==1){
            return 1;
        }
        else{
            x1=x;
            y1=y;
            where++;
            flag=Next(&x1,&y1,where);
            while(flag==0&&where<7){
                where++;
                flag=Next(&x1,&y1,where);
            }
        }
    }
    if(flag==0){
        chess[y][x]=0;
        counter--;
    }
    return 0;
}
 
void ShowResult(){
    
    for(int i=0;i<Y;i++){
        for(int j=0;j<X;j++){
            cout.width(4);
            cout<<chess[i][j]<<' ';
        }
        cout<<endl;
    }
    cout<<endl;
}
 
int main(){
    int start=clock();
    int result=Explore(2,1);
    int end=clock();
    if(result){
        ShowResult();        
    }
    else{
        cout<<"have no path!"<<endl; 
    }
 
    cout<<"spend time:"<<(end-start)/CLOCKS_PER_SEC<<" s"<<endl;
    return 0;
}

以上是“如何使用C++实现马踏棋盘”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 怎么使用C++实现迷宫游戏
  2. 使用C++实现顺序链表

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:Python中结构型组合模式的示例分析

下一篇:springboot如何自动扫描添加BeanDefinition源码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》