bzoj1972: [Sdoi2010]猪国杀

刚开博客,放几道大模拟

猪国杀 20多k ....

www.lydsy.com/JudgeOnline/problem.php?id=1972

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXM 2001

#define Tao     ('P')
#define Sha     ('K')
#define Shan    ('D')
#define JueDou  ('F')
#define NanZhu  ('N')
#define WanJian ('W')
#define WuXie   ('J')
#define ZhuGe   ('Z')
#define DiYi    (-1)
#define None     0
#define YinQin   1

int LeftFan;
typedef struct PigBase *PtrPig;
typedef struct PigLink *PigLinker;
void MoPai(PtrPig, int);
void Win();
struct PigBase
{
    vector<char> ShouPai;
    bool dead, tiao, HaveZhuGe;
    int blood;
    PigBase(bool _tiao, char c1, char c2, char c3, char c4)
        : dead(false), tiao(_tiao), HaveZhuGe(false), blood(4)
    {
        ShouPai.push_back(c1);
        ShouPai.push_back(c2);
        ShouPai.push_back(c3);
        ShouPai.push_back(c4);
    }
    virtual bool IsZhu()const
    {
        return false;
    }
    virtual bool IsZhong()const
    {
        return false;
    }
    virtual bool IsFan()const
    {
        return false;
    }
    void PrintInfo()
    {
        if (dead)
            printf("DEAD\n");
        else
        {
            if (ShouPai.size() == 0)
            {
                putchar('\n');
                return;
            }
            for (vector<char>::iterator i = ShouPai.begin(); i + 1 != ShouPai.end(); ++i)
                printf("%c ", *i);
            printf("%c\n", ShouPai.back());
        }
    }
    virtual void ChuPai() = 0;
    void BinSi(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Tao);
        if (p == ShouPai.end())
        {
            dead = true;
            if (IsFan())
                --LeftFan;
            Win();
            if (IsFan())
                MoPai(sender, 3);
            if (IsZhong() && sender->IsZhu())
            {
                sender->ShouPai.clear();
                sender->HaveZhuGe = false;
            }
        }
        else
        {
            ShouPai.erase(p);
            ++blood;
        }
    }
    void GotSha(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Shan);
        if (p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
        }
        else
            ShouPai.erase(p);
    }
    virtual bool QuaryWuXie(PtrPig, PtrPig, PigLinker, int) = 0;
    virtual void GotJueDou(PtrPig) = 0;
    virtual void GotNanZhu(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Sha);
        if (p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
        }
        else
            ShouPai.erase(p);
    }
    virtual void GotWanJian(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Shan);
        if (p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
        }
        else
            ShouPai.erase(p);
    }
    void Tiao();
};
struct PigZhu: public PigBase
{
    vector<PtrPig> LeiFan;
    PigZhu(char c1, char c2, char c3, char c4)
        : PigBase(true, c1, c2, c3, c4) {}
    bool IsZhu()const
    {
        return true;
    }
    void ChuPai();
    bool QuaryWuXie(PtrPig, PtrPig, PigLinker, int);
    void GotJueDou(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Sha);
        if (p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
        }
        else
        {
            ShouPai.erase(p);
            sender->GotJueDou(this);
        }
    }
    void GotNanZhu(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Sha);
        if (p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
            if (!sender->tiao && find(LeiFan.begin(), LeiFan.end(), sender) == LeiFan.end())
                LeiFan.push_back(sender);
        }
        else
            ShouPai.erase(p);
    }
    void GotWanJian(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Shan);
        if (p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
            if (!sender->tiao && find(LeiFan.begin(), LeiFan.end(), sender) == LeiFan.end())
                LeiFan.push_back(sender);
        }
        else
            ShouPai.erase(p);
    }
};
struct PigZhong: public PigBase
{
    PigZhong(char c1, char c2, char c3, char c4)
        : PigBase(false, c1, c2, c3, c4) {}
    bool IsZhong()const
    {
        return true;
    }
    void ChuPai();
    bool QuaryWuXie(PtrPig, PtrPig, PigLinker, int);
    void GotJueDou(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Sha);
        if (sender->IsZhu() || p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
        }
        else
        {
            ShouPai.erase(p);
            sender->GotJueDou(this);
        }
    }
};
struct PigFan: public PigBase
{
    PigFan(char c1, char c2, char c3, char c4)
        : PigBase(false, c1, c2, c3, c4) {}
    bool IsFan()const
    {
        return true;
    }
    void ChuPai();
    bool QuaryWuXie(PtrPig, PtrPig, PigLinker, int);
    void GotJueDou(PtrPig sender)
    {
        vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), Sha);
        if (p == ShouPai.end())
        {
            --blood;
            if (blood == 0)
                BinSi(sender);
        }
        else
        {
            ShouPai.erase(p);
            sender->GotJueDou(this);
        }
    }
};

struct PigLink
{
    PtrPig data;
    PigLinker next;
    PigLink(PtrPig _data = NULL, PigLinker _next = NULL)
        : data(_data), next(_next) {}
};

char PaiDui[MAXM];
int PaiDui_head;
int m;
PigLinker PigList;
PigLinker Zhu;

void Init()
{
    LeftFan = 0;
    int n;
    scanf("%d%d", &n, &m);
    char c1, c2, c3, c4, s[4];
    for (int i = 0; i < n; ++i)
    {
        scanf("%s %c %c %c %c", s, &c1, &c2, &c3, &c4);
        if (i == 0)
            Zhu = PigList = new PigLink(new PigZhu(c1, c2, c3, c4));
        else if (strcmp(s, "ZP") == 0)
        {
            PigList->next = new PigLink(new PigZhong(c1, c2, c3, c4));
            PigList = PigList->next;
        }
        else
        {
            PigList->next = new PigLink(new PigFan(c1, c2, c3, c4));
            PigList = PigList->next;
            ++LeftFan;
        }
    }
    PigList->next = Zhu;
    PigList = Zhu;
    PaiDui_head = 0;
    for (int i = 0; i < m; ++i)
        scanf(" %c", &PaiDui[i]);
}

void MoPai(PtrPig x, int t)
{
    for (int i = 0; i < t; ++i)
    {
        x->ShouPai.push_back(PaiDui[PaiDui_head++]);
        if (PaiDui_head == m)
            --PaiDui_head;
    }
}

void Win()
{
    if (Zhu->data->dead || !LeftFan)
    {
        printf(LeftFan ? "FP\n" : "MP\n");
        Zhu->data->PrintInfo();
        for (PigLinker i = Zhu->next; i != Zhu; i = i->next)
            i->data->PrintInfo();
        exit(0);
    }
}

int main()
{
    long ct = 1;
    for (Init();; PigList = PigList->next, ++ct)
    {
//        printf("%ld %d\n", ct, PaiDui_head);
        if (PigList->data->dead)
            continue;
        MoPai(PigList->data, 2);
        PigList->data->ChuPai();
    }
    return 0;
}

bool AllQuaryWuXie(PigLinker sender, PtrPig receiver, int k = 0)
{
    if (sender->data->QuaryWuXie(sender->data, receiver, sender, k))
        return true;
    for (PigLinker i = sender->next; i != sender; i = i->next)
        if (!i->data->dead && i->data->QuaryWuXie(sender->data, receiver, i, k))
            return true;
    return false;
}

void AllGotNanZhu(PigLinker sender)
{
    for (PigLinker i = sender->next; i != sender; i = i->next)
        if (!i->data->dead && !AllQuaryWuXie(sender, i->data))
            i->data->GotNanZhu(sender->data);
}

void AllGotWanJian(PigLinker sender)
{
    for (PigLinker i = sender->next; i != sender; i = i->next)
        if (!i->data->dead && !AllQuaryWuXie(sender, i->data))
            i->data->GotWanJian(sender->data);
}

void PigZhu::ChuPai()
{
    bool UsedSha = false;
    for (vector<char>::iterator i = ShouPai.begin(); i != ShouPai.end();)
        if (*i == Tao)
        {
            if (blood != 4)
            {
                i = ShouPai.erase(i);
                ++blood;
            }
            else
                ++i;
        }
        else if (*i == Sha)
        {
            if (!UsedSha || HaveZhuGe)
            {
                PigLinker j;
                for (j = PigList->next; j != PigList; j = j->next)
                {
                    if (j->data->dead)
                        continue;
                    if (j->data->IsFan() && j->data->tiao)
                        break;
                    if (find(LeiFan.begin(), LeiFan.end(), j->data) != LeiFan.end())
                        break;
                    j = PigList;
                    break;
                }
                if (j != PigList)
                {
                    ShouPai.erase(i);
                    UsedSha = true;
                    j->data->GotSha(this);
                    i = ShouPai.begin();
                }
                else
                    ++i;
            }
            else
                ++i;
        }
        else if (*i == Shan)
        {
            ++i;
            continue;
        }
        else if (*i == JueDou)
        {
            PigLinker j;
            for (j = PigList->next; j != PigList; j = j->next)
            {
                if (j->data->dead)
                    continue;
                if (j->data->IsFan() && j->data->tiao)
                    break;
                if (find(LeiFan.begin(), LeiFan.end(), j->data) != LeiFan.end())
                    break;
            }
            if (j != PigList)
            {
                ShouPai.erase(i);
                if (!AllQuaryWuXie(PigList, j->data, DiYi))
                    j->data->GotJueDou(this);
                i = ShouPai.begin();
            }
            else
                ++i;
        }
        else if (*i == NanZhu)
        {
            ShouPai.erase(i);
            AllGotNanZhu(PigList);
            i = ShouPai.begin();
        }
        else if (*i == WanJian)
        {
            ShouPai.erase(i);
            AllGotWanJian(PigList);
            i = ShouPai.begin();
        }
        else if (*i == WuXie)
        {
            ++i;
            continue;
        }
        else if (*i == ZhuGe)
        {
            ShouPai.erase(i);
            HaveZhuGe = true;
            i = ShouPai.begin();
        }
}

void PigZhong::ChuPai()
{
    bool UsedSha = false;
    for (vector<char>::iterator i = ShouPai.begin(); i != ShouPai.end();)
        if (*i == Tao)
        {
            if (blood != 4)
            {
                i = ShouPai.erase(i);
                ++blood;
            }
            else
                ++i;
        }
        else if (*i == Sha)
        {
            if (!UsedSha || HaveZhuGe)
            {
                PigLinker j;
                for (j = PigList->next; j != PigList; j = j->next)
                {
                    if (j->data->dead)
                        continue;
                    if (j->data->IsFan() && j->data->tiao)
                        break;
                    j = PigList;
                    break;
                }
                if (j != PigList)
                {
                    PigList->data->Tiao();
                    ShouPai.erase(i);
                    UsedSha = true;
                    j->data->GotSha(this);
                    i = ShouPai.begin();
                }
                else
                    ++i;
            }
            else
                ++i;
        }
        else if (*i == Shan)
        {
            ++i;
            continue;
        }
        else if (*i == JueDou)
        {
            PigLinker j;
            for (j = PigList->next; j != PigList; j = j->next)
            {

                if (j->data->dead)
                    continue;
                if (j->data->IsFan() && j->data->tiao)
                    break;
            }
            if (j != PigList)
            {
                ShouPai.erase(i);
                PigList->data->Tiao();
                if (!AllQuaryWuXie(PigList, j->data, DiYi))
                {
                    j->data->GotJueDou(this);
                    if (dead)
                        return;
                }
                i = ShouPai.begin();
            }
            else
                ++i;
        }
        else if (*i == NanZhu)
        {
            ShouPai.erase(i);
            AllGotNanZhu(PigList);
            i = ShouPai.begin();
        }
        else if (*i == WanJian)
        {
            ShouPai.erase(i);
            AllGotWanJian(PigList);
            i = ShouPai.begin();
        }
        else if (*i == WuXie)
        {
            ++i;
            continue;
        }
        else if (*i == ZhuGe)
        {
            ShouPai.erase(i);
            HaveZhuGe = true;
            i = ShouPai.begin();
        }
}

void PigFan::ChuPai()
{
    bool UsedSha = false;
    for (vector<char>::iterator i = ShouPai.begin(); i != ShouPai.end();)
        if (*i == Tao)
        {
            if (blood != 4)
            {
                i = ShouPai.erase(i);
                ++blood;
            }
            else
                ++i;
        }
        else if (*i == Sha)
        {
            if (!UsedSha || HaveZhuGe)
            {
                PigLinker j;
                for (j = PigList->next; j != PigList; j = j->next)
                {
                    if (j->data->dead)

                        continue;
                    if (j->data->IsZhu() || (j->data->IsZhong() && j->data->tiao))
                        break;
                    j = PigList;
                    break;
                }
                if (j != PigList)
                {
                    PigList->data->Tiao();
                    ShouPai.erase(i);
                    UsedSha = true;
                    j->data->GotSha(this);
                    i = ShouPai.begin();
                }
                else
                    ++i;
            }
            else
                ++i;
        }
        else if (*i == Shan)
        {
            ++i;
            continue;
        }
        else if (*i == JueDou)
        {
            PigList->data->Tiao();
            ShouPai.erase(i);
            if (!AllQuaryWuXie(PigList, Zhu->data, DiYi))
            {
                Zhu->data->GotJueDou(this);
                if (dead)
                    return;
            }
            i = ShouPai.begin();
        }
        else if (*i == NanZhu)
        {
            ShouPai.erase(i);
            AllGotNanZhu(PigList);
            i = ShouPai.begin();
        }
        else if (*i == WanJian)
        {
            ShouPai.erase(i);
            AllGotWanJian(PigList);
            i = ShouPai.begin();
        }
        else if (*i == WuXie)
        {
            ++i;
            continue;
        }
        else if (*i == ZhuGe)
        {
            ShouPai.erase(i);
            HaveZhuGe = true;
            i = ShouPai.begin();
        }
}

bool PigZhu::QuaryWuXie(PtrPig sender, PtrPig receiver, PigLinker me, int k)
{
    vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), WuXie);
    if (p == ShouPai.end())
        return false;
    if (k == DiYi)
    {
        if (!receiver->tiao)
            return false;
        if (receiver == this || receiver->IsZhong()
            || (sender->IsFan() && sender->tiao))
        {
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, sender, DiYi) && !AllQuaryWuXie(me, receiver, YinQin);
        }
    }
    else if (k == None)
    {
        if (!receiver->tiao)
            return false;
        if (receiver == this || receiver->IsZhong())
        {
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, receiver, YinQin);
        }
    }
    else if (k == YinQin)
    {
        if ((sender->IsFan() && sender->tiao) || (receiver->IsFan() && receiver->tiao))
        {
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, sender, DiYi) && !AllQuaryWuXie(me, receiver, DiYi);
        }
    }
    return false;
}
bool PigZhong::QuaryWuXie(PtrPig sender, PtrPig receiver, PigLinker me, int k)
{
    vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), WuXie);
    if (p == ShouPai.end())
        return false;
    if (k == DiYi)
    {
        if (!receiver->tiao)
            return false;
        if (receiver->IsZhu() || receiver->IsZhong()
            || (sender->IsFan() && sender->tiao))
        {
            Tiao();
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, sender, DiYi) && !AllQuaryWuXie(me, receiver, YinQin);
        }
    }
    else if (k == None)
    {
        if (!receiver->tiao)
            return false;
        if (receiver->IsZhu() || receiver->IsZhong())
        {
            Tiao();
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, receiver, YinQin);
        }
    }
    else if (k == YinQin)
    {
        if ((sender->IsFan() && sender->tiao) || (receiver->IsFan() && receiver->tiao))
        {
            Tiao();
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, sender, DiYi) && !AllQuaryWuXie(me, receiver, DiYi);
        }
    }
    return false;
}
bool PigFan::QuaryWuXie(PtrPig sender, PtrPig receiver, PigLinker me, int k)
{
    vector<char>::iterator p = find(ShouPai.begin(), ShouPai.end(), WuXie);
    if (p == ShouPai.end())
        return false;
    if (k == DiYi)
    {
        if (!receiver->tiao)
            return false;
        if (receiver->IsFan() || sender->IsZhu()
            || (sender->IsZhong() && sender->tiao))
        {
            Tiao();
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, sender, DiYi) && !AllQuaryWuXie(me, receiver, YinQin);
        }
    }
    else if (k == None)
    {
        if (!receiver->tiao)
            return false;
        if (receiver->IsFan())
        {
            Tiao();
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, receiver, YinQin);
        }
    }
    else if (k == YinQin)
    {
        if (sender->IsZhu() || (sender->IsZhong() && receiver->tiao)
            || receiver->IsZhu() || (receiver->IsZhong() && receiver->tiao))
        {
            Tiao();
            ShouPai.erase(p);
            return !AllQuaryWuXie(me, sender, DiYi) && !AllQuaryWuXie(me, receiver, DiYi);
        }
    }
    return false;
}

void PigBase::Tiao()
{
    tiao = true;
    vector<PtrPig>::iterator p = find(((PigZhu *)(Zhu->data))->LeiFan.begin(), ((PigZhu *)(Zhu->data))->LeiFan.end(), this);
    if (p != ((PigZhu *)(Zhu->data))->LeiFan.end())
        ((PigZhu *)(Zhu->data))->LeiFan.erase(p);
}


阅读更多
文章标签: bzoj
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭