UVa 127 "Accordian" Patience

UVa 127 "Accordian" Patience

不断地去寻找有没有可以移动的牌,直到不能移动。
以下是我的代码:

#include < iostream >
#include
< string >
#include
< stack >
#include
< cstdio >
using   namespace  std;
const   int  kMaxn( 57 );

bool  Match( const   string   & a, const   string   & b)
{
    
return  (a[ 0 ] == b[ 0 ||  a[ 1 ] == b[ 1 ]);
}

stack
< string >  s[kMaxn];

int  main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/

    
int  n( 0 );
    
string  t;
    
while (cin >> &&  t != " # " )
    {
        s[n].push(t);
        n
++ ;
        
if (n >= 52 )
        {
            
while ( true )
            {
                
int  pos,sign;
                
for (pos = 0 ;pos < n;pos ++ )
                {
                    
if (pos >= 3   &&  Match(s[pos].top(),s[pos - 3 ].top()))
                    {
                        sign
= 1 ;
                        
break ;
                    }
                    
if (pos >= 1   &&  Match(s[pos].top(),s[pos - 1 ].top()))
                    {
                        sign
= 2 ;
                        
break ;
                    }
                }
                
if (pos == n)
                    
break ;
                
if (sign == 1 )
                    s[pos
- 3 ].push(s[pos].top());
                
else
                    s[pos
- 1 ].push(s[pos].top());
                s[pos].pop();
                
if (s[pos].empty())
                {
                    
for ( int  i = pos;i < n - 1 ;i ++ )
                        s[i]
= s[i + 1 ];
                    
while ( ! s[n - 1 ].empty())
                        s[n
- 1 ].pop();
                    n
-- ;
                }
            }

            cout
<< n;
            cout
<< "  pile " ;
            
if (n > 1 )
                cout
<< " s " ;
            cout
<< "  remaining: " ;
            
for ( int  i = 0 ;i < n;i ++ )
                cout
<< "   " << s[i].size();
            cout
<< endl;

            
for ( int  i = 0 ;i < n;i ++ )
                
while ( ! s[i].empty())
                    s[i].pop();
            n
= 0 ;
        }
    }

    
return   0 ;
}

你可能感兴趣的:(UVa 127 "Accordian" Patience)