首页 > 代码库 > 小甲鱼数据结构和算法--马踏棋盘(骑士周游问题)

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

代码如下:

#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:\n");
        for( i=0;i<X;i++){
            for(j=0;j<Y;j++){
                    printf("%2d\t",chess[i][j]);
            }
            printf("\n");
        }
}
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\n",(double)(end-begin)/CLOCKS_PER_SEC);
    return 0;
}

运行截图:

技术分享

 

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