首页 > 其他分享 >POJ 3760. 魔兽世界(修订版) 题解

POJ 3760. 魔兽世界(修订版) 题解

时间:2022-10-17 18:46:32浏览次数:61  
标签:3760 city cid int 题解 num POJ chara id

一句话,大模拟,照着题意敲就完了。

写的期间甚至因为疫情导致程序被锁在了机房 www

//3760. 魔兽世界(修订版)
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
/***********PreDefination***********/
const int ORDER[3][5]={{0,0,0,0,0},{3,4,5,2,1},{4,1,2,3,5}};
const string STAND[3]={"","red","blue"},TYPE[6]={"","dragon","ninja","iceman","lion","wolf"};
struct Soldier
{
    int type,stand,element,force,id,num;
    void born(int,int,int);
    void outStand();
    void outData();
}chara[60];
struct City
{
    int element,id[3],flag,num;
    bool killed;
    void init();
}city[60];
int elements[10],forces[10],cnt[3],n,m,t,nxt,hour,minute,tim;
/***********Structs***********/
void Soldier::born(int s,int t,int i)
{
    type=t;
    stand=s;
    element=elements[t];
    force=forces[t];
    id=i;
    num=0;
    return;
}
void Soldier::outStand()
{
    cout<<STAND[stand]<<' '<<TYPE[type]<<' '<<id;
    return;
}
void Soldier::outData()
{
    cout<<" with "<<element<<" elements and force "<<force<<endl;
    return;
}
void City::init()
{
    num=flag=element=0;
    killed=false;
    id[0]=id[1]=id[2]=-1;
    return;
}
/***********Operation***********/
void init()
{
    for(int i=0;i<=n+1;i++)
    {
        city[i].init();
    }
    city[0].element=city[n+1].element=m;
    hour=minute=nxt=cnt[1]=cnt[2]=0;
    return;
}
void outTime()
{
    if(hour<10)
        cout<<"00";
    else if(hour<100)
        cout<<'0';
    cout<<hour<<':';
    if(!minute)
        cout<<"00 ";
    else 
        cout<<minute<<' ';
    return;
}
void born(int s)
{
    int cid=(s==2?n+1:0),nextToBorn;
    nextToBorn=ORDER[s][cnt[s]%5];
    if(city[cid].element>=elements[nextToBorn])
    {
        cnt[s]++;
        city[cid].element-=elements[nextToBorn];
        chara[nxt].born(s,nextToBorn,cnt[s]);
        city[cid].id[s]=nxt++;
        outTime();
        chara[city[cid].id[s]].outStand();
        cout<<" born\n";
    }
    return;
}
void iceman(int pre)
{
    if(chara[pre].type==3)
    {
        if(chara[pre].num&1)
        {
            chara[pre].element=max(1,chara[pre].element-9);
            chara[pre].force+=20;
        }
        chara[pre].num++;
    }
    return;
}
void action(int n)
{
    int pre=city[n-1].id[1];
    if(~pre)
    {
        iceman(pre);
        outTime();
        chara[pre].outStand();
        cout<<" marched to city "<<n;
        chara[pre].outData();
    }
    pre=city[n+1].id[2];
    if(~pre)
    {
        iceman(pre);
        outTime();
        chara[pre].outStand();
        cout<<" marched to city "<<n;
        chara[pre].outData();
    }
    return;
}
bool fight(bool redFirst,int cid)
{
    int fir=city[cid].id[2],sec=city[cid].id[1],winId;
    bool res=false,secWin=true;
    if(redFirst)
        swap(fir,sec);
    outTime();
    chara[fir].outStand();
    cout<<" attacked ";
    chara[sec].outStand();
    cout<<" in city "<<cid;
    chara[fir].outData();
    if(chara[sec].element<=chara[fir].force)
    {
        outTime();
        chara[sec].outStand();
        cout<<" was killed in city "<<cid<<endl;
        res=true;
        secWin=false;
        if(chara[fir].type==1)//dragon yells
        {
            outTime();
            chara[fir].outStand();
            cout<<" yelled in city "<<cid<<endl;
        }
        outTime();
        chara[fir].outStand();
        cout<<" earned "<<city[cid].element<<" elements for his headquarter\n";
        if(chara[fir].type==5)//wolf wins, update data
        {
            if(chara[fir].num&1)
            {
                chara[fir].element<<=1;
                chara[fir].force<<=1;
            }
            chara[fir].num++;
        }
        if(chara[sec].type==4)//lion dies, update data
            chara[fir].element+=chara[sec].element;
    }
    else if(chara[sec].type!=2)
    {
        outTime();
        chara[sec].outStand();
        cout<<" fought back against ";
        chara[fir].outStand();
        cout<<" in city "<<cid<<endl;
        if(chara[fir].element<=(chara[sec].force>>1))
        {
            res=true;
            outTime();
            chara[fir].outStand();
            cout<<" was killed in city "<<cid<<endl;
            outTime();
            chara[sec].outStand();
            cout<<" earned "<<city[cid].element<<" elements for his headquarter\n";
            if(chara[fir].type==4)//lion dies, update data
                chara[sec].element+=chara[fir].element;
        }
        else if(chara[fir].type==1)//dragon yells
        {
            outTime();
            chara[fir].outStand();
            cout<<" yelled in city "<<cid<<endl;
        }
    }
    else if(chara[fir].type==1)//dragon yells
    {
        outTime();
        chara[fir].outStand();
        cout<<" yelled in city "<<cid<<endl;
    }
    chara[sec].element-=chara[fir].force;
    if(secWin&&chara[sec].type!=2)
        chara[fir].element-=chara[sec].force>>1;
    if(res)
    {
        winId=0;
        if(redFirst)
        {
            if(secWin)
                winId=2;
            else 
                winId=1;
        }
        else 
        {
            if(secWin)
                winId=1;
            else
                winId=2;
        }
        if(winId==1)
        {
            city[cid].id[2]=-1;
            if(city[cid].num<0)
                city[cid].num=0;
            city[cid].num++;
            if(city[cid].num==2&&city[cid].flag!=1)
            {
                city[cid].flag=1;
                outTime();
                cout<<"red flag raised in city "<<cid<<endl;
            }
        }
        else 
        {
            city[cid].id[1]=-1;
            if(city[cid].num>0)
                city[cid].num=0;
            city[cid].num--;
            if(city[cid].num==-2&&city[cid].flag!=2)
            {
                city[cid].flag=2;
                outTime();
                cout<<"blue flag raised in city "<<cid<<endl;
            }
        }
    }
    else 
        city[cid].num=0;
    return res;
}
void operate()
{
    int preId;
    bool beat=false,redFirst;
    tim=hour=minute=0;
    while(tim<=t)
    {
        //:00 try to born
        born(1);
        born(2);
        minute+=10;
        tim+=10;
        if(tim>t)
            break;
        //:10 move
        if(~city[1].id[2])
        {
            preId=city[1].id[2];
            if(chara[preId].type==3)
            {
                if(chara[preId].num&1)
                {
                    chara[preId].element=max(1,chara[preId].element-9);
                    chara[preId].force+=20;
                }
                chara[preId].num++;
            }
            outTime();
            chara[city[1].id[2]].outStand();
            cout<<" reached red headquarter";
            chara[city[1].id[2]].outData();
            if(~city[0].id[2])
            {
                outTime();
                cout<<"red headquarter was taken\n";
                beat=true;
            }
        }
        for(int i=1;i<=n;i++)
            action(i);
        if(~city[n].id[1])
        {
            preId=city[n].id[1];
            if(chara[preId].type==3)
            {
                if(chara[preId].num&1)
                {
                    chara[preId].element=max(1,chara[preId].element-9);
                    chara[preId].force+=20;
                }
                chara[preId].num++;
            }
            outTime();
            chara[city[n].id[1]].outStand();
            cout<<" reached blue headquarter";
            chara[city[n].id[1]].outData();
            if(~city[n+1].id[1])
            {
                outTime();
                cout<<"blue headquarter was taken\n";
                beat=true;
            }
        }
        for(int i=0;i<=n;i++)
        {
            if(~city[i+1].id[2])
            {
                city[i].id[2]=city[i+1].id[2];
                city[i+1].id[2]=-1;
            }
        }
        for(int i=n+1;i;i--)
        {
            if(~city[i-1].id[1])
            {
                city[i].id[1]=city[i-1].id[1];
                city[i-1].id[1]=-1;
            }
        }
        minute+=10;
        tim+=10;
        if(beat||tim>t)
            break;
        //:20 create elements
        for(int i=1;i<=n;i++)
            city[i].element+=10;
        minute+=10;
        tim+=10;
        if(tim>t)
            break;
        //:30 take elements
        for(int i=1;i<=n;i++)
        {
            if(~city[i].id[1]&&!~city[i].id[2])
            {
                outTime();
                chara[city[i].id[1]].outStand();
                cout<<" earned "<<city[i].element<<" elements for his headquarter\n";
                city[0].element+=city[i].element;
                city[i].element=0;
            }
            else if(~city[i].id[2]&&!~city[i].id[1])
            {
                outTime();
                chara[city[i].id[2]].outStand();
                cout<<" earned "<<city[i].element<<" elements for his headquarter\n";
                city[n+1].element+=city[i].element;
                city[i].element=0;
            }
        }
        minute+=10;
        tim+=10;
        if(tim>t)
            break;
        //:40 fight
        for(int i=1;i<=n;i++)
        {
            if(~city[i].id[1]&&~city[i].id[2])
            {
                redFirst=true;
                if(city[i].flag==2)
                    redFirst=false;
                else if(!city[i].flag&&!(i&1))
                    redFirst=false;
                if(fight(redFirst,i))
                    city[i].killed=true;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(city[i].killed&&~city[i].id[2])
            {
                if(city[n+1].element<8)
                    break;
                city[n+1].element-=8;
                chara[city[i].id[2]].element+=8;
            }
        }
        for(int i=n;i;i--)
        {
            if(city[i].killed&&~city[i].id[1])
            {
                if(city[0].element<8)
                    break;
                city[0].element-=8;
                chara[city[i].id[1]].element+=8;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(city[i].killed)
            {
                if(~city[i].id[1])
                    city[0].element+=city[i].element;
                else 
                    city[n+1].element+=city[i].element;
                city[i].element=0;
                city[i].killed=false;
            }
        }
        minute+=10;
        tim+=10;
        if(tim>t)
        {
            break;
        }
        //:50 headquarter elements
        outTime();
        cout<<city[0].element<<" elements in red headquarter\n";
        outTime();
        cout<<city[n+1].element<<" elements in blue headquarter\n";
        minute=0;
        hour++;
        tim+=10;
    }
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
    {
        cin>>m>>n>>t;
        for(int i=1;i<=5;i++)
        {
            cin>>elements[i];
        }
        for(int i=1;i<=5;i++)
        {
            cin>>forces[i];
        }
        cout<<"Case:"<<i<<endl;
        init();
        operate();
    }
    return 0;
}
/*
 * POJ
 * http://poj.org/problem?id=3760
 * C++20 -O0
 * 2022.10.9
 */

标签:3760,city,cid,int,题解,num,POJ,chara,id
From: https://www.cnblogs.com/2020gyk080/p/16800208.html

相关文章

  • 【题解】CF11D A Simple Task(状压 DP)
    【题解】CF11DASimpleTask题目链接CF11DASimpleTask题意概述给定一张\(n\)个点\(m\)条边的无向图,无重边自环,点数不超过\(19\),求无向图中环的数量。思路分......
  • [题解] Atcoder Regular Contest ARC 151 A B C D E 题解
    点我看题昨天刚打的ARC,题目质量还是不错的。A-EqualHammingDistances对于一个位置i,如果\(S_i=T_i\),那么不管\(U\)的这个位置填什么,对到\(S\)和\(T\)的海明距离增量......
  • CF463C 题解
    题目传送门题目分析贪心练手好题。首先,国际象棋中象的走法是斜着走,也就是这样:通过上面的图我们不难看出,如果一个象在黑格,另外一个在白格,那么它们之间一定不会互相攻击......
  • P3755 题解
    前言题目传送门!更好的阅读体验?为啥要用分块呀,cdq分治真好写!前置知识:三维偏序模版。思路记\(s_{i,j}\)表示:对角坐标为\((-\infty,-\infty)\)到\((i,j)\)的......
  • CF525E 题解
    前言题目传送门!更好的阅读体验?比较套路的折半搜索。代码实现略微繁琐。思路每个数有三个状态:不选、选\(a_i\)、选\(a_i!\)。数据范围\(n\le26\),暗示着爆搜,但是......
  • CF960E Alternating Tree题解:
    CF960EAlternatingTree题解:也许是第一道自己做的*2300。可简化为树上黑白染色。首先想到树形DP,如果是棵有根树,状态转移方程如下:\[{f[x][0]=f[y][1]+siz[y]*a[x]}\]......
  • DO、VO、BO、DTO、POJO
    DO:DomainObject 即数据库表字段在Java中一一对应关系(有人称它实体类)BO:BusinessObject即业务对象,Service层向上传传输的对象。是多个DO的组合形式VO:viewoject展示......
  • P3959 [NOIP2017 提高组] 宝藏 题解
    一道不错的状压dp题。注意到本质上打通的路径会构成一棵树,因此实际上总花费就是一个点的层高(根节点层高为0)乘上其到父亲节点的边的边权。据此可以考虑一种初步的状压......
  • java问题解答
    因为子类继承自父类,会沿用父类的东西(没被覆盖的函数以及可见的成员变量等),而这些东西子类是没有的,需要先初始化父类才能被使用子类构造方法中调用父类构造方法,一个作用是......
  • 2021 CCPC 威海站 VP记录(题解)
    2021CCPC威海站VP记录(题解)题目顺序为vp时开题顺序:A-Goodbye,Ziyin!签到,连边数小于等于2的可以作为二叉树根,若有大于4的直接输出0。code:voidsolve(){ int......