心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0

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

#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;
}
posted on 2011-04-14 11:37 lee1r 阅读(618) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:基础/模拟题目分类:数据结构

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