一句话,大模拟,照着题意敲就完了。
写的期间甚至因为疫情导致程序被锁在了机房 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