• 小甲鱼数据结构和算法--马踏棋盘(骑士周游问题)


    代码如下:

    #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;
    }

    运行截图:

  • 相关阅读:
    断棍构造过程-波利亚翁方案-中餐馆过程
    狄利克雷过程
    狄利克雷分布
    共轭先验(conjugate prior)
    镜像与文件系统
    Oracle-04
    Oracle-02
    Oracle-01
    认识数据库
    c:forEach的作用
  • 原文地址:https://www.cnblogs.com/ncuhwxiong/p/7282779.html
Copyright © 2020-2023  润新知