pku2286 The Rotation Game

有点像模拟的IDA*,做了才发现和大多数人的思路一样

题意:定义chessboard上的8个移位操作:处于操作端的元素放到所在列(行)的最尾,该列(行)的所有元素向操作端方向移动一格。求最短的操作序列使得中间8个形成正方形的元素相等。

Memory: 208K   Time: 266MS

    #include  < iostream >
    
using   namespace  std;

    
int  a1[ 200 ],a2[ 200 ],a3[ 200 ],a4[ 200 ],beg1,beg2,beg3,beg4,array[ 25 ],deep,cnt[ 4 ];
    
char  path[ 50 ];

    
void  shift( int  n, bool  left){
        
switch (n){
        
case   1 :
            
if (left){
                a1[beg1
+ 7 ] = a1[beg1];

                a3[beg3
+ 2 ] = a1[beg1 + 3 ];
                a4[beg4
+ 2 ] = a1[beg1 + 5 ];
                beg1
++ ;
            }
            
else {
                a1[beg1
- 1 ] = a1[beg1 + 6 ];
                
                a3[beg3
+ 2 ] = a1[beg1 + 1 ];
                a4[beg4
+ 2 ] = a1[beg1 + 3 ];
                beg1
-- ;
            }
            
break ;
        
case   2 :
            
if (left){
                a2[beg2
+ 7 ] = a2[beg2];
                
                a3[beg3
+ 4 ] = a2[beg2 + 3 ];
                a4[beg4
+ 4 ] = a2[beg2 + 5 ];
                beg2
++ ;
            }
            
else {
                
                a2[beg2
- 1 ] = a2[beg2 + 6 ];
                
                a3[beg3
+ 4 ] = a2[beg2 + 1 ];
                a4[beg4
+ 4 ] = a2[beg2 + 3 ];
                beg2
-- ;
            }
            
break ;
        
case   3 :
            
if (left){
                a3[beg3
+ 7 ] = a3[beg3];
                
                a1[beg1
+ 2 ] = a3[beg3 + 3 ];
                a2[beg2
+ 2 ] = a3[beg3 + 5 ];
                beg3
++ ;
            }
            
else {
                a3[beg3
- 1 ] = a3[beg3 + 6 ];

                a1[beg1
+ 2 ] = a3[beg3 + 1 ];
                a2[beg2
+ 2 ] = a3[beg3 + 3 ];
                beg3
-- ;
            }
            
break ;
        
case   4 :
            
if (left){
                a4[beg4
+ 7 ] = a4[beg4];
                
                a1[beg1
+ 4 ] = a4[beg4 + 3 ];
                a2[beg2
+ 4 ] = a4[beg4 + 5 ];
                beg4
++ ;
            }
            
else {
                a4[beg4
- 1 ] = a4[beg4 + 6 ];
                
                a1[beg1
+ 4 ] = a4[beg4 + 1 ];
                a2[beg2
+ 4 ] = a4[beg4 + 3 ];
                beg4
-- ;
            }
            
break ;
        }
    }

    
int  f(){
        
int  i,mx = 0 ;
        cnt[
1 ] = cnt[ 2 ] = cnt[ 3 ] = 0 ;

        cnt[array[a1[beg1
+ 2 ]]] ++ ;
        cnt[array[a1[beg1
+ 4 ]]] ++ ;
        cnt[array[a2[beg2
+ 4 ]]] ++ ;
        cnt[array[a2[beg2
+ 2 ]]] ++ ;
        cnt[array[a3[beg3
+ 3 ]]] ++ ;
        cnt[array[a1[beg1
+ 3 ]]] ++ ;
        cnt[array[a4[beg4
+ 3 ]]] ++ ;
        cnt[array[a2[beg2
+ 3 ]]] ++ ;

        
for (i = 1 ;i <= 3 ;i ++ )
            
if (cnt[i] > mx)
                mx
= cnt[i];
        
return   8 - mx;
    }

        



    
void  init(){
        beg1
= beg2 = beg3 = beg4 = 100 ;
        a1[
100 ] = 1 ;
        a1[
101 ] = 3 ;
        a1[
102 ] = 7 ;
        a1[
103 ] = 12 ;
        a1[
104 ] = 16 ;
        a1[
105 ] = 21 ;
        a1[
106 ] = 23 ;

        a2[
100 ] = 2 ;
        a2[
101 ] = 4 ;
        a2[
102 ] = 9 ;
        a2[
103 ] = 13 ;
        a2[
104 ] = 18 ;
        a2[
105 ] = 22 ;
        a2[
106 ] = 24 ;

        a3[
100 ] = 5 ;
        a3[
101 ] = 6 ;
        a3[
102 ] = 7 ;
        a3[
103 ] = 8 ;
        a3[
104 ] = 9 ;
        a3[
105 ] = 10 ;
        a3[
106 ] = 11 ;

        a4[
100 ] = 14 ;
        a4[
101 ] = 15 ;
        a4[
102 ] = 16 ;
        a4[
103 ] = 17 ;
        a4[
104 ] = 18 ;
        a4[
105 ] = 19 ;
        a4[
106 ] = 20 ;
        
/*
                     a1:   a2:
                     1     2
                     3     4
        a3: 5  6  7  8  9  10 11
                     12    13
        a4: 14 15 16 17 18 19 20
                     21    22
                     23    24
        
*/
    }

    
bool  idfs( int  d){
        
if (d == deep){
            
if (array[a1[beg1 + 2 ]] == array[a1[beg1 + 4 ]] 
                
&&  array[a1[beg1 + 2 ]] == array[a2[beg2 + 4 ]] 
                
&&  array[a1[beg1 + 2 ]] == array[a2[beg2 + 2 ]]
                
&&  array[a1[beg1 + 2 ]] == array[a3[beg3 + 3 ]]
                
&&  array[a1[beg1 + 2 ]] == array[a1[beg1 + 3 ]]
                
&&  array[a1[beg1 + 2 ]] == array[a4[beg4 + 3 ]]
                
&&  array[a1[beg1 + 2 ]] == array[a2[beg2 + 3 ]])
                
return   true ;
            
else
                
return   false ;
        }

        
if (d + f() > deep)
            
return   false ;

        shift(
1 , true );
        path[d]
= ' A ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
1 , false );
        
        shift(
2 , true );
        path[d]
= ' B ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
2 , false );

        shift(
3 , false );
        path[d]
= ' C ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
3 , true );

        shift(
4 , false );
        path[d]
= ' D ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
4 , true );

        shift(
2 , false );
        path[d]
= ' E ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
2 , true );

        shift(
1 , false );
        path[d]
= ' F ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
1 , true );
        
        shift(
4 , true );
        path[d]
= ' G ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
4 , false );

        shift(
3 , true );
        path[d]
= ' H ' ;
        
if (idfs(d + 1 ))
            
return   true ;
        shift(
3 , false );

        
return   false ;
    }


        

    
int  main(){
        
int  i;
        
while (scanf( " %d " , & array[ 1 ])  &&  array[ 1 ]){
            
for (i = 2 ;i <= 24 ;i ++ )
                scanf(
" %d " , & array[i]);
            
for (deep = 0 ;deep < 100 ;deep ++ ){
                init();
                
if (idfs( 0 ))
                    
break ;
            }
            
if (deep == 0 )
                printf(
" No moves needed\n%d\n " ,array[a1[beg1 + 2 ]]);
            
else {
                
for (i = 0 ;i < deep;i ++ )
                    printf(
" %c " ,path[i]);
                printf(
" \n%d\n " ,array[a1[beg1 + 2 ]]);
            }
        }
        
return   0 ;
    }

你可能感兴趣的:(game)