zoukankan      html  css  js  c++  java
  • 小甲鱼数据结构和算法--马踏棋盘(骑士周游问题)

    代码如下:

    #include<stdio.h>
    #include<time.h>
    
    #define X 5
    #define Y 5
    int chess[X][Y];
    
    void printChess(){
        int i,j;
            printf("This is horse Chess:
    ");
            for( i=0;i<X;i++){
                for(j=0;j<Y;j++){
                        printf("%2d	",chess[i][j]);
                }
                printf("
    ");
            }
    }
    int next(int *x,int *y,int step){
        switch(step)
        {
            case 0:
                if(*y+2<=Y-1 && *x-1>=0 && chess[*x-1][*y+2]==0)
                {
                    *y+=2;
                    *x-=1;
                    return 1;
                }
                break;
            case 1:
                if(*y+2<=Y-1 && *x+1<=X-1 && chess[*x+1][*y+2]==0)
                {
                    *y+=2;
                    *x+=1;
                    return 1;
                }
                break;
            case 2:
                if(*y+1<=Y-1 && *x+2<=X-1 && chess[*x+2][*y+1]==0)
                {
                    *y+=1;
                    *x+=2;
                    return 1;
                }
                break;
            case 3:
                if(*y-1>=0 && *x+2<=X-1 && chess[*x+2][*y-1]==0)
                {
                    *y-=1;
                    *x+=2;
                    return 1;
                }
                break;
            case 4:
                if(*y-2>=0 && *x+1<=X-1 && chess[*x+1][*y-2]==0)
                {
                    *y-=2;
                    *x+=1;
                    return 1;
                }
                break;
            case 5:
                if(*y-2>=0 && *x-1>=0 && chess[*x-1][*y-2]==0)
                {
                    *y-=2;
                    *x-=1;
                    return 1;
                }
                break;
            case 6:
                if(*y-1>=0 && *x-2>=0 && chess[*x-2][*y-1]==0)
                {
                    *y-=1;
                    *x-=2;
                    return 1;
                }
                break;
            case 7:
                if(*y+1<=Y-1 && *x-2>=0 && chess[*x-2][*y+1]==0)
                {
                    *y+=1;
                    *x-=2;
                    return 1;
                }
                break;
            default:
                break;
        }
        return 0;
    }
    
    int horse(int x,int y,int tag){
        int x_t=x,y_t=y;
        int flag=0,count=0;
        chess[x][y]=tag;
        if(tag==X*Y){
            printChess();
            return 1;
        }
        flag=next(&x_t,&y_t,count);
        while(!flag && count<=7){
            count++;
            flag=next(&x_t,&y_t,count);
        }
        while(flag){
            if(horse(x_t,y_t,tag+1))
            return 1;
            x_t=x,y_t=y,count++;
            flag=next(&x_t,&y_t,count);
            while(!flag && count<=7){
                count++;
                flag=next(&x_t,&y_t,count);
            }
        }
        if(!flag)chess[x][y]=0;
        return 0;
    }
    
    int main()
    {
        int i,j;
        for(i=0;i<X;i++){
            for(j=0;j<Y;j++){
                chess[i][j] = 0;
            }
        }
        clock_t begin,end;
        begin=clock();
        if(!horse(2,0,1)){
            printf("The horse Chess is unavailable!");
        }
        end=clock();
        printf("This time used is %lf
    ",(double)(end-begin)/CLOCKS_PER_SEC);
        return 0;
    }

    运行截图:

  • 相关阅读:
    oa_mvc_easyui_删除(6)
    oa_mvc_easyui_详细页(5)
    oa_mvc_easyui_分页(4)
    oa_mvc_easyui_后台布局(3)
    oa_mvc_easyui_登录完成(2)
    oa_mvc_easyui_项目搭建及登录页面验证码(1)
    第六篇 ajax
    AOP切入点表达式
    开发的时候,有异步回调的时候,问题终于解决了
    mysql数据表结构查询
  • 原文地址:https://www.cnblogs.com/ncuhwxiong/p/7282779.html
Copyright © 2011-2022 走看看