「リーグ戦日程」問題の分割解決アルゴリズム


/*
      :<<     >>      -[          ]
      :   
      :2002 09 15 (11:58:00-13:18:00)
            “  ”           

*/
#include    "stdio.h"
#include    "stdlib.h"

//:====================“       ”           ====================
/*
      :   
      :2002 09 15 (11:58:38-20:00:00)
            “       ”           
    ===================================================
        :
              n(n = 2^k)          ,      n-1 ,      
          n-1       ,             ,    。     
               。

        :
              n         1,2,3,...,n,         n n-1    ,
        i j        i     j      。
                     ,        (2^(n-1 )     ,    n 
             ,                  。
                 8     , 1 4                   
        (4 3 ),5 8                   (4 3 );      
                       4  。  ,      (4 4 )      
        1 4        5 8            。  ,  4 , 1 4  
            5 8     ,    ,           , 5 8   “  
          ”  。
          ,                 ,           
        4  。
        :
        
    ===================================================================
    |*|       1         2         3         4         5         6         7     |*|
    ===================================================================
    |*|    1     |    2    |    3    |    4    ||    5    |    6    |    7    |    8    |*|
    |*|    2     |    1    |    4    |    3    ||    6    |    7    |    8    |    7    |*|
    |*|    3     |    4    |    1    |    2    ||    7    |    8    |    5    |    6    |*|
    |*|    4     |    3    |    2    |    1    ||    8    |    5    |    6    |    5    |*|
    ========[   ]========================[   ]===================
    |*|    5     |    6    |    7    |    8    ||    1    |    4    |    3    |    2    |*|
    |*|    6     |    5    |    8    |    7    ||    2    |    1    |    4    |    3    |*|
    |*|    7     |    8    |    5    |    6    ||    3    |    2    |    1    |    4    |*|
    |*|    8     |    7    |    6    |    5    ||    4    |    3    |    2    |    1    |*|
    ========[   ]========================[   ]===================
*/
#define    MAXN    64
//     
int    calendar[MAXN + 1][MAXN];
void    Round_Robin_Calendar()
{
    int i,j,m,number,p,q;
    printf("      :(  :2^k) ");
    scanf("%d",&number);
    //            :// i    j   calendar[i][j]     
    calendar[1][1] = 2;        // 1    1   2     
    calendar[2][1] = 1;        // 2    1   1     
    m = 1;
    p = 1;
    while(m < number)
    {
        m ++;
        //p = p + p;
        p += p;
        q = 2 * p;    // 2^m         
        //         
        for(i = p + 1;i <= q;i++)
            for(j = 1;j<= p - 1;j++)
                calendar[i][j] = calendar[i - p][j] + p;    //       =            4[]
        //         
        //           1 
        calendar[1][p] = p + 1;        
        for(i = 2;i <= p;i++)
            calendar[i][p] = calendar[i - 1][p] + 1;
        //             ,          [      ]
        for(j = p + 1;j < q;j++)
        {
            for(i = 1;i < p;i++)
                calendar[i][j] = calendar[i + 1][j - 1];
            calendar[p][j] = calendar[1][j - 1];
        }
        //         
        for(j = p;j < q;j++)
            for(i = 1;i <= p;i++)
                calendar[calendar[i][j]][j] = i;    //    
        for(i = 1;i <= q;i++)
        {
            for(j = 1;j < q;j++)
                printf("%4d",calendar[i][j]);
            printf(" ");
        }
        printf(" ");
    }
}
//:====================“       ”           ====================

int main(int argc, char* argv[])
{
    Round_Robin_Calendar();
    printf("         ! ");
    return 0;
}