uva 127 - "Accordian" Patience

这题绝对是大坑,我TLE了不下十次,不用STL感觉代码很操蛋,在网上搜了很多STL的解题报告,唯独这一个擦边过了

看看吧:

//这个题确实让我恶心到了,搞了两天,各种TLE,蛋碎,学C++以来第一次用STL ,就当是熟悉一下吧,不过codeblocks的提示确实是有些问题。。。
//思路:直接模拟,每次移动之后一定要从头开始才能确保按顺序移动,直接模拟一定要用scanf输入,这很关键....
//基本上算是卡着时间AC的
#include <iostream>
#include <cstring>
#include <vector>
#include <stack>
#include <cstdio>
using namespace std;
struct Card
{
    char x,y;
};
 bool ismove(const Card a,const Card b)
{
    if(a.x == b.x || a.y == b.y)
        return true;
    return false;
}
int main()
{
    vector<stack<Card> > cards;
    char s1,s2;
    while(scanf(" %c%c",&s1,&s2) && s1!='#')
    {
        Card cad;
        cad.x=s1;
        cad.y=s2;
        stack<Card> temp;
        temp.push(cad);
        cards.push_back(temp);
        if(cards.size()==52)
        {
            while(true)
            {
                int pos;
                for(pos=0;pos!=cards.size();++pos)
                {
                    if(pos>2 && ismove(cards[pos].top(),cards[pos-3].top()))
                    {
                        cards[pos-3].push(cards[pos].top());
                        cards[pos].pop();
                        break;
                    }
                    if(pos>0 && ismove(cards[pos].top(),cards[pos-1].top()))
                    {
                        cards[pos-1].push(cards[pos].top());
                        cards[pos].pop();
                        break;
                    }
                }
                if(pos==cards.size())
                    break;
                if(cards[pos].empty())
                {
                    cards.erase(cards.begin()+pos);
                }
            }
            if(cards.size()==1)
                printf("%d pile remaining:",cards.size());
            else
                printf("%d piles remaining:",cards.size());
            for(int i=0;i<cards.size();++i)
            {
                printf(" %d",cards[i].size());
            }
            printf("\n");
            cards.clear();
        }
    }
    return 0;
}

要用scanf,建议以后读入都用scanf

下边这个代码虽然超时,,但可读性非常强,晒晒,人家的

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <stack>
using namespace std;
#define N 52

int Match(const string &s1, const string &s2)
{
    return s1[0]==s2[0]||s1[1]==s2[1];
}
int main()
{
    string s;
    vector <stack < string> > piles;
    while(cin>>s && s!="#")
    {
        stack<string> tmp;
        tmp.push(s);
        piles.push_back(tmp);
        if(piles.size()==52)
        {
            int i;
            while(1)
            {
                for(i = 0; i != piles.size(); i++)
                {
                    if(i>=3&&Match(piles[i].top(), piles[i-3].top()))
                    {
                        piles[i-3].push(piles[i].top());
                        piles[i].pop();
                        break;
                    }
                    if(i>=1&&Match(piles[i].top(), piles[i-1].top()))
                    {
                        piles[i-1].push(piles[i].top());
                        piles[i].pop();
                        break;
                    }
                }
                if(i==piles.size())
                    break;
                if(piles[i].empty())
                    piles.erase(piles.begin()+i);
            }
            cout<<piles.size()<<(piles.size() == 1 ? " pile remaining:" : " piles remaining:");
            for(i = 0; i < piles.size(); i++)
                cout<<' '<<piles[i].size();
            cout<<endl;
            piles.clear();
        }

    }
    return 0;
}





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