5.1.2 Starry Night 夜空繁星

5.1.2 Starry Night 夜空繁星

/*
PROG: starry
LANG: C++
*/

#include
< cstdio >
#include
< cstring >
#include
< cstdlib >
using   namespace  std;

struct  Arr{
    
int  top,down,left,right,num,col;
    Arr():top(
101 ),down( - 1 ),left( 101 ),right( - 1 ),num( 0 ){}
}St[
510 ];
int  N,H,W;
int  x1,y1,x2,y2;
int  a1,b1,a2,b2;
char  C[ 510 ];
char  M[ 110 ][ 110 ];
int  G[ 110 ][ 110 ];
int  S[ 110 ][ 110 ];
void  flood( int  x, int  y){
    S[x][y]
= N;
    
for ( int  d1 =- 1 ;d1 <= 1 ; ++ d1)
        
for ( int  d2 =- 1 ;d2 <= 1 ; ++ d2){
            
int  xi = x + d1,yi = y + d2;
            
if (M[xi][yi] == ' 1 ' && S[xi][yi] ==- 1 )
                flood(xi,yi);
        }
}
void  init()
{
    scanf(
" %d%d " , & W, & H);
    memset(M,
' # ' , sizeof (M));
    
for ( int  i = 1 ;i <= H; ++ i)
        scanf(
" %s " ,M[i] + 1 );
    memset(S,
- 1 , sizeof (S));
    N
= 0 ;
    
for ( int  i = 1 ;i <= H; ++ i)
        
for ( int  j = 1 ;j <= W; ++ j)
            
if (M[i][j] == ' 1 ' && S[i][j] ==- 1 ){
                N
++ ;
                flood(i,j);
            }
    
for ( int  i = 1 ;i <= H; ++ i)
        
for ( int  j = 1 ;j <= W; ++ j){
            
if ( - 1 == S[i][j])  continue ;
            Arr 
& now = St[S[i][j]];
            now.num
++ ;
            now.col
= S[i][j];
            
if (i < now.top) now.top = i;
            
if (j < now.left) now.left = j;
            
if (i > now.down) now.down = i;
            
if (j > now.right) now.right = j;
        }
}
void  turn0_1()
{
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            G[i][j]
= S[i][j];
}
void  turn0_2()
{
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            G[j][i]
= S[i][j];
    
int  tmp = x1; x1 = y1; y1 = tmp;
    tmp
= x2; x2 = y2; y2 = tmp;
}
void  turn1()
{
    
int  g[ 110 ][ 110 ];
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            g[i][y1
+ y2 - j] = G[i][j];
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            G[i][j]
= g[i][j];
}
void  turn2()
{
    
int  g[ 110 ][ 110 ];
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            g[x1
+ x2 - i][j] = G[i][j];
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            G[i][j]
= g[i][j];
}
void  turn3()
{
    
int  g[ 110 ][ 110 ];
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            g[i][y1
+ y2 - j] = G[i][j];
    
for ( int  i = x1;i <= x2; ++ i)
        
for ( int  j = y1;j <= y2; ++ j)
            G[i][j]
= g[i][j];
}
bool  tf( int  x, int  y)
{
    
for ( int  i = 1 ;i <= x2 - x1 + 1 ; ++ i)
        
for ( int  j = 1 ;j <= y2 - y1 + 1 ; ++ j)
            
if ((G[x1 + i - 1 ][y1 + j - 1 ] == y) ^ (S[a1 + i - 1 ][b1 + j - 1 ] == x))
                
return   0 ;
    
return   1 ;
}
bool  check( int  x, int  y)
{
    
if (St[x].num != St[y].num)  return   0 ;
    
if (St[x].down - St[x].top == St[y].down - St[y].top && St[x].right - St[x].left == St[y].right - St[y].left){
        a1
= St[x].top; b1 = St[x].left; a2 = St[x].down; b2 = St[x].right;
        x1
= St[y].top; y1 = St[y].left; x2 = St[y].down; y2 = St[y].right;
        turn0_1();
        
if (tf(x,y))  return   1 ;
        turn1();
        
if (tf(x,y))  return   1 ;
        turn2();
        
if (tf(x,y))  return   1 ;
        turn3();
        
if (tf(x,y))  return   1 ;
    }
    
if (St[x].down - St[x].top == St[y].right - St[y].left && St[x].right - St[x].left == St[y].down - St[y].top){
        a1
= St[x].top; b1 = St[x].left; a2 = St[x].down; b2 = St[x].right;
        x1
= St[y].top; y1 = St[y].left; x2 = St[y].down; y2 = St[y].right;
        turn0_2();
        
if (tf(x,y))  return   1 ;
        turn1();
        
if (tf(x,y))  return   1 ;
        turn2();
        
if (tf(x,y))  return   1 ;
        turn3();
        
if (tf(x,y))  return   1 ;
    }
    
return   0 ;
}
void  solve()
{
    
for ( int  i = 1 ;i <= N; ++ i)
        C[i]
= ' # ' ;
    
char  c = ' a ' ;
    
for ( int  i = 1 ;i <= N; ++ i){
        
if (C[i] != ' # ' continue ;
        C[i]
= c ++ ;
        
for ( int  j = i + 1 ;j <= N; ++ j)
            
if (check(i,j))
                C[j]
= C[i];
    }
}
void   out ()
{
    
for ( int  i = 1 ;i <= H; ++ i){
        
for ( int  j = 1 ;j <= W; ++ j)
            
if (S[i][j] > 0 )
                printf(
" %c " ,C[S[i][j]]);
            
else  printf( " 0 " );
        printf(
" \n " );
    }
}
int  main()
{
    freopen(
" starry.in " , " r " ,stdin);
    freopen(
" starry.out " , " w " ,stdout);
    init();
    solve();
    
out ();
    
return   0 ;
}
/*

Executing
   Test 1: TEST OK [0.000 secs, 2920 KB]
   Test 2: TEST OK [0.000 secs, 2920 KB]
   Test 3: TEST OK [0.011 secs, 2920 KB]
   Test 4: TEST OK [0.011 secs, 2920 KB]
   Test 5: TEST OK [0.011 secs, 2920 KB]

All tests OK.

Your program ('starry') produced all correct answers! This is your submission #5 for this problem. Congratulations! 
*/



你可能感兴趣的:(5.1.2 Starry Night 夜空繁星)