循环日程表 问题(递归分治)

/*循环日程表问题(递归分治)*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 2600
//int f=0;
int a[maxn][maxn];
void cir(int i,int j,int n)
{
    int p,q;
    if(n==2)//递归出口
    {
        a[i][n]=j;
        a[i][n-1]=i;
        a[j][n]=i;
        a[j][n-1]=j;
        // printf("第 %d 次递归
",++f);
        return ;
    }
    cir(i,i+n/2-1,n/2);
    for(p=i; p<=i+n/2-1; p++)
        for(q=1; q<=n/2; q++)
        {
            a[p+n/2][q+n/2]=a[p][q];
            // printf("kkkkkkk a[%d][%d] = a[%d][%d] = %d",p+n/2,q+n/2,p,q,a[p][q]);
            // system("pause");
        }
    cir(i+n/2,j,n/2);
    for(p=i+n/2; p<=j; p++)
    //其实本来很简单,我误以为j一直等于n,为了美观我就没把n改为j,一直错。
        for(q=1; q<=n/2; q++)
        {
            a[p-n/2][q+n/2]=a[p][q];
            //  printf("jjjjjjj a[%d][%d] = a[%d][%d] = %d",p-n/2,q+n/2,p,q,a[p][q]);
            // system("pause");
        }
}
int main()
{
    int k;
    while(~scanf("%d",&k))
    {
        memset(a,0,sizeof(a));
        cir(1,1<<k,1<<k);
        for(int i=1; i<=(1<<k); i++)
        {
            for(int j=1; j<=(1<<k); j++)
                printf("%4d",a[i][j]);
            printf("
");
        }
    }
    return 0;
}
/*
2
   1   2   3   4
   2   1   4   3
   3   4   1   2
   4   3   2   1
3
   1   2   3   4   5   6   7   8
   2   1   4   3   6   5   8   7
   3   4   1   2   7   8   5   6
   4   3   2   1   8   7   6   5
   5   6   7   8   1   2   3   4
   6   5   8   7   2   1   4   3

   7   8   5   6   3   4   1   2
   8   7   6   5   4   3   2   1

*/
原文地址:https://www.cnblogs.com/heqinghui/p/3219995.html