马的遍历问题-问题分析

#include<stdio.h>

int a[100][100] = {0};

int tot;

int check(int n, int m)

{

    if(a[n][m]==0)

        return 1;

    else

        return 0;

}

int sb(int n, int m, int s, int I, int J)

{

    int i, j, N, M;

    if(s == n*m+1)

    {

        tot++;

        printf(“%d\n”, tot);

        for(N=0; N<n; N++)

        {

            for(M=0; M<m; M++)

                printf(“%3d “, a[N][M]);

            printf(“\n”);

        }

    }

    else

    {

        if(s == 1)

        {

            for(i=0; i<n; i++)

            for(j=0; j<m; j++)

            {

                a[i][j] = s;

                sb(n, m, s+1, i, j);

                a[i][j] = 0;

            }

        }

        if(s>1)

        {

            for(i=0; i<n; i++)

            for(j=0; j<m; j++)

            {

                if(((i==I+2)&&(j==J+1))||((i==I+2)&&(j==J-1))||

                   ((i==I+1)&&(j==J+2))||((i==I-1)&&(j==J+2))||

                   ((i==I-2)&&(j==J+1))||((i==I-2)&&(j==J-1))||

                   ((i==I-1)&&(j==J-2))||((i==I+1)&&(j==J-2)))

                    if(check(i, j))

                    {

                        a[i][j] = s;

                        sb(n, m, s+1, i, j);

                        a[i][j] = 0;

                    }

            }

        }

    }

}

int main()

{

    int i, j, n, m;

    while(scanf(“%d%d”, &n, &m)!=EOF && n!=0)

    {

        tot = 0;

        sb(n, m, 1, 0, 0);

    }

}

    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/qq_19195553/article/details/38677261
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞