Ural 1015 Test the Difference!

Ural 1015 Test the Difference!

先按照一定的规则让等价的骰子变换到相同的形式。我的做法是:先把数字1变换到前面,再把和1相邻的四个数字中最小的旋转到1的左侧。
另外注意输出的处理有些麻烦。
Ps:最近刚刚开始进行C转C++、OI转ACM,类定义的不是太好,qsort()用得很粗糙。
以下是我代码:

#include < iostream >
#include
< cstring >
using   namespace  std;
const   int  maxn  =   100000 ;

class  newtype
{
    
private :
        
void  rotate_1();
        
void  rotate_2();
    
public :
        newtype():pos(
0 ) {memset(num, 0 , sizeof (num));}
        
void  update();
        
        
int  pos,num[ 6 ];
};
void  newtype::rotate_1()
{
//   0-2-1-4
     int  t = num[ 0 ];
    num[
0 ] = num[ 4 ];
    num[
4 ] = num[ 1 ];
    num[
1 ] = num[ 2 ];
    num[
2 ] = t;
}
void  newtype::rotate_2()
{
//   2-3-4-5
     int  t = num[ 2 ];
    num[
2 ] = num[ 3 ];
    num[
3 ] = num[ 4 ];
    num[
4 ] = num[ 5 ];
    num[
5 ] = t;
}
void  newtype::update()
{
    
int  pos_one;
    
for ( int  i = 0 ;i < 6 ;i ++ )
        
if (num[i] == 1 )
        {
            pos_one
= i;
            
break ;
        }
    
switch (pos_one)
    {
        
case   0 :
            rotate_1();
            rotate_2();
            rotate_2();
            rotate_2();
            
break ;
        
case   1 :
            rotate_1();
            rotate_2();
            
break ;
        
case   2 :
            rotate_2();
            rotate_2();
            rotate_2();
            
break ;
        
case   3 :
            
break ;
        
case   4 :
            rotate_2();
            
break ;
        
case   5 :
            rotate_2();
            rotate_2();
    }
    
    
int  num_min = num[ 0 ];
    
if (num_min > num[ 1 ]) num_min = num[ 1 ];
    
if (num_min > num[ 2 ]) num_min = num[ 2 ];
    
if (num_min > num[ 4 ]) num_min = num[ 4 ];
    
while (num[ 0 ] != num_min)
        rotate_1();
}

bool  equal(newtype  & a,newtype  & b)
{
    
for ( int  i = 0 ;i < 6 ;i ++ )
        
if (a.num[i] != b.num[i])
            
return   false ;
    
return   true ;
}

int  cmp( const   void   * a, const   void   * b)
{
    
for ( int  i = 0 ;i < 6 ;i ++ )
        
if (((newtype * )a) -> num[i] != ((newtype * )b) -> num[i])
            
return  (((newtype * )a) -> num[i] - ((newtype * )b) -> num[i]);
    
return  ((newtype * )a) -> pos - ((newtype * )b) -> pos;
}

int  n,cnt,list[ 8000 ];
newtype r[maxn];

int  cmp2( const   void   * a, const   void   * b)
{
    
int  va =* (( int * )a),vb =* (( int * )b);
    
return  r[va].pos - r[vb].pos;
}

int  main()
{    
    cin
>> n;
    
for ( int  i = 1 ;i <= n;i ++ )
    {
        
for ( int  j = 0 ;j < 6 ;j ++ )
            cin
>> r[i].num[j];
        r[i].pos
= i;
        r[i].update();
    }
    
    qsort(r
+ 1 ,n, sizeof (newtype),cmp);
    cnt
= 1 ;
    list[cnt]
= 1 ;
    newtype t
= r[ 1 ];
    
for ( int  i = 2 ;i <= n;i ++ )
    {
        
while (i <= &&  equal(r[i],t))
            i
++ ;
        
if (i <= n)
        {
            cnt
++ ;
            list[cnt]
= i;
            t
= r[i];
        }
    }
    qsort(list
+ 1 ,cnt, sizeof ( int ),cmp2);
    
    cout
<< cnt << endl;
    
for ( int  i = 1 ;i <= cnt;i ++ )
    {
        
int  j = list[i];
        
bool  first = true ;
        
while (equal(r[list[i]],r[j]))
        {
            
if (first) first = false ;
            
else  cout << "   " ;
            cout
<< r[j].pos;
            j
++ ;
        }
        cout
<< endl;
    }
    
return   0 ;
}

你可能感兴趣的:(Ural 1015 Test the Difference!)