Advertisement
Guest User

MALAKIES_KHPOYRIDH

a guest
Jul 24th, 2010
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main ( void ) {
  4.   FILE *in= fopen ( "qubits.in" , "r" ) ;
  5.   FILE *out = fopen ( "qubits.out", "w" ) ;
  6.   int N , ar[20][3] , p[3] , turn = 0 , i , j , ontop = 0 , tmp ;
  7.  
  8.   fscanf ( in , "%d" , &N ) ;
  9.   N-- ;
  10.   p[0] = N ; p[1] = -1 ; p[2] = -1 ;
  11.   for ( i = 0 ; i <= N ; i++ )
  12.     ar[i][0] = i ;
  13.   if ( ( N % 2 ) == 1 ) {
  14.     p[1]++ ;
  15.     ar[p[1]][1] = ar[p[0]][0] ;
  16.     ar[p[0]][0] = -1 ;
  17.     p[0]-- ;
  18.     fprintf ( out , "%c %c\n" , 65 , 66 ) ;
  19.   }
  20.   else {
  21.     p[2]++ ;
  22.     ar[p[2]][2] = ar[p[0]][0] ;
  23.     ar[p[0]][0] = -1 ;
  24.     p[0]-- ;
  25.     fprintf ( out , "%c %c\n" , 65 , 67 ) ;
  26.   }
  27.   while ( 1 ) {
  28.     tmp =  -5 ;
  29.     if ( turn ) {
  30.       /* Find the smallest */
  31.       for ( i = 0 ; ar[p[i]][i] != N ; i++ ) ;
  32.       if ( ontop ) {
  33.         /*Calculate how many layers are under the smallest
  34.         ( arithmetical progression ( + 1 ) ) */
  35.         for ( j = 0 ; ( ar[p[i]-j][i] ) == ( ar[p[i]-j-1][i] + 1 ) ; j++ ) ;
  36.         tmp = j ;
  37.         for ( j = 0 ; ; j++ ) {
  38.           if ( p[j] == -1 )
  39.             continue ;
  40.           if ( ( ar[p[j]][j] ) == ( ar[p[i] - tmp][i] - 1 ) )
  41.               break ;
  42.         }
  43.         if ( ( tmp % 2 ) == 1 ) {
  44.           for ( ontop = 0 ; ( ontop == i ) || ( ontop == j ) ; ontop++ ) ;
  45.           p[ontop]++ ;
  46.           ar[p[ontop]][ontop] = ar[p[i]][i] ;
  47.           ar[p[i]][i] = -1 ;
  48.           p[i]-- ;
  49.           fprintf ( out , "%c %c\n" , 65 + i , 65 + ontop ) ;
  50.           ontop = 1 ;
  51.         }
  52.         else {
  53.           p[j]++ ;
  54.           ar[p[j]][j] = ar[p[i]][i] ;
  55.           ar[p[i]][i] = -1 ;
  56.           p[i]-- ;
  57.           fprintf ( out , "%c %c\n" , 65 + i , 65 + j ) ;
  58.         }
  59.         tmp = -2 ;
  60.       }
  61.       else {
  62.         /*Find the second smallest*/
  63.         for ( j = 0 ; ar[p[j]][j] != ( N - 1 ) ; j++ ) ;
  64.         /* Transfer the smallest to the second smallest */
  65.         p[j]++ ;
  66.         ar[p[j]][j] = ar[p[i]][i] ;
  67.         ar[p[i]][i] = -1 ;
  68.         p[i]-- ;
  69.         fprintf ( out , "%c %c\n" , 65 + i , 65 + j ) ;
  70.         ontop = 1 ;
  71.       }
  72.       if ( tmp == -2 )
  73.         ontop = 0 ;
  74.       tmp = -1 ;
  75.     }
  76.     else {
  77.       /* Find if there is an empty slot */
  78.       for ( i = 0 ; ( p[i] != -1 ) && ( i < 3 ) ; i++ ) ;
  79.       /* If an empty slot was found */
  80.       if ( i != 3 ) {
  81.         /* Find the non empty slot ( and not the smallest ) */
  82.         for ( j = 0 ; ( ar[p[j]][j] == N ) || ( p[j] == -1 ) ; j++ ) ;
  83.         /* Transfer the non empty to the empty */
  84.         p[i]++ ;
  85.         ar[p[i]][i] = ar[p[j]][j] ;
  86.         ar[p[j]][j] = -1 ;
  87.         p[j]-- ;
  88.         fprintf ( out , "%c %c\n" , 65 + j , 65 + i ) ;
  89.       }
  90.       else {
  91.         /*Find the non smallest slots*/
  92.         for ( i = 0 ; ar[p[i]][i] == N ; i++ ) ;
  93.         for ( j = 0 ; ( ar[p[j]][j] == N ) || ( j == i ) ; j++ ) ;
  94.         /*Move the biggest to i and the smallest to j ( well ,
  95.         that was the idea , but due to incompatibility it's
  96.         the smallest-number ( which is actually the big layer )
  97.         to the biggest-number ( which is actually the small layer )*/
  98.         if ( ar[p[j]][j] < ar[p[i]][i] ) {
  99.           tmp = i ; i = j ; j = tmp ;
  100.         }
  101.         p[i]++ ;
  102.         ar[p[i]][i] = ar[p[j]][j] ;
  103.         ar[p[j]][j] = -1 ;
  104.         p[j]-- ;
  105.         fprintf ( out , "%c %c\n" , 65 + j , 65 + i ) ;
  106.       }
  107.       turn = 1 ;
  108.     }
  109.     if ( tmp == -1 )
  110.       turn = 0 ;
  111.     if ( ( p[0] == -1 && p[1] == -1 ) || ( p[0] == -1 && p[2] == -1 ) || ( p[1] == -1 && p[2] == -1 ) )
  112.       break ;
  113.   }
  114.   fclose ( in ) ; fclose ( out ) ;
  115.   return ( 0 ) ;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement