#include  < stdio.h >
#include 
< stdlib.h >

#define  pause system("pause")

int  r, c;
bool  ok =   false ;

bool  visite[ 100 ][ 100 ];
int   px[ 50 ], py[ 50 ];

int  yy[ 8 ] =  {  - 1 1 - 2 2 - 2 2 - 1 1  };
int  xx[ 8 ] =  {  - 2 , - 2 - 1 , - 1 ,   1 1 ,   2 2  };

bool  isok(  int  x,  int  y )
{
    
return  x >=   1   &&  x <=  r  &&  y >=   1   &&  y <=  c;
}

void  dfs(  int  x,  int  y,  int  length )
{
    px[length]
=  x, py[length] =  y;
    
    
if ( length ==  r *  c )
    {
        ok
=   true ;
        
return ;
    }
    
    
for int  i =   0 ; i <   8 ++ i )
    {
        
int  tx =  xx[i] +  x, ty =  yy[i] +  y;
        
        
if ( isok(tx,ty)  &&   ! visite[tx][ty]  &&   ! ok )
        {
            visite[tx][ty]
=   true ;
            
            dfs( tx, ty, length
+   1  );
            
            visite[tx][ty]
=   false ;
        }
    }
}

int  main()
{
    
int  test;
    scanf(
" %d " , & test);
    
    
for int  t =   1 ; t <=  test;  ++ t )
    {
        scanf(
" %d%d " , & c, & r);
        
        visite[
1 ][ 1 ] =   true ; ok =   false ;
        dfs( 
1 1 1  );
        
        printf(
" Scenario #%d:\n " , t );
        
        
if ( ok )
        {
            
for int  i =   1 ; i <=  r *  c;  ++ i )
            {
                printf(
" %c " , px[i] +   ' A ' -   1  );
                printf(
" %d " , py[i] );
            }
            printf(
" \n " );
        }
        
else  puts( " impossible " );
        
        
if ( t <  test ) puts( "" );
    }
    
    
return   0 ;
}
posted on 2008-10-14 12:00 Darren 阅读(327) 评论(0)  编辑 收藏 引用 所属分类: 搜索

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理