记录编号 458516 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [SDOI 2010] 猪国杀 最终得分 100
用户昵称 GravatarHzoi_moyi 是否通过 通过
代码语言 C++ 运行时间 0.024 s
提交时间 2017-10-11 15:25:48 内存使用 0.41 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int pig=12;
const int card=4005;
int  tot[pig],cnt[pig],typ[pig],tl[pig],real[pig];
bool zb[pig],used[pig][card],dead[pig],like[pig];
char p[pig][card];//about every pig
int  n,m,sum,cot,nt[pig],pr[pig],aim;
bool op;
char a[5],la,tp;
queue<char>  q;//about the game
void init()
{
	scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
    	pr[i]=i-1,nt[i]=i+1;
    	tl[i]=tot[i]=cnt[i]=4;
    	scanf("%s",a);
    	if(a[0]=='M')  typ[i]=0;//main
    	if(a[0]=='Z')  typ[i]=1;//zhong
    	if(a[0]=='F')  typ[i]=2;//anti
    	for(int j=1;j<=4;j++)
    		scanf("%s",a),p[i][j]=a[0];
    }
    real[1]=1,pr[1]=n,nt[n]=1;
    for(int i=1;i<=m;i++)
    	scanf("%s",a),q.push(a[0]);//heap
}//scanf the data  *
void mp(int x,int tim)
{
	for(int i=1;i<=tim;i++)
	{
		if(!q.empty())  tp=q.front(),q.pop();
        else            tp=la;
        tot[x]++,cnt[x]++;
        p[x][tot[x]]=tp;
        if(q.empty())  la=tp;
    }
}//get new cards  *
void outit()
{
	if(dead[1])  printf("FP\n");
	else         printf("MP\n");//who win
	for(int i=1;i<n;i++)
	{
		if(dead[i])  printf("DEAD\n");
		else
		{
			if(cnt[i]==0){  printf("\n");continue;  }
			cot=0;
		    for(int j=1;j<=tot[i];j++)
		        if(!used[i][j])
		        {   
		        	cot++,printf("%c",p[i][j]);
		        	if(cot==cnt[i])  printf("\n");//prevent spare empty
		        	else             printf(" ");
		        }
		}
	}
	if(dead[n])  printf("DEAD");//prevent spare enter
	else
	{
		if(cnt[n]==0)  return;
		cot=0;
		for(int j=1;j<=tot[n];j++)
		    if(!used[n][j])
		    {
		    	cot++,printf("%c",p[n][j]);
		    	if(cot!=cnt[n])  printf(" ");
		    }
	}
}//printf the answer  *
bool check()
{ 
	if(dead[1])  return 1;//main died
	op=1;
	for(int i=1;i<=n;i++)
		if(!dead[i]&&typ[i]==2)
			op=0;//all anti died
	if(op)  return 1;
	return 0;
}//end the game  *
bool have(int x)
{
	for(int i=1;i<=tot[x];i++)
		if(!used[x][i]&&p[x][i]=='J')
		{
			used[x][i]=1;//cout<<x<<" "<<i<<" "<<p[x][i]<<endl;
			cnt[x]--;
			if(x!=1)  real[x]=typ[x],like[x]=0;
			return 1;
		}
	return 0;
}//have a wuxie then use it  *
bool wx(int ap,int om,bool kind)
{
	if(!real[om])  return kind;
	if(kind==1&&(typ[ap]==real[om]||(typ[ap]==0&&real[om]==1)))
		if(have(ap))
			return wx(ap,om,kind^1);
    if(kind==0&&(typ[ap]==3-real[om]||(typ[ap]==0&&(real[om]==2||like[om]))))
    	if(have(ap))
    		return wx(ap,om,kind^1);
	for(int i=nt[ap];i!=ap;i=nt[i])
	{
		if(kind==1&&(typ[i]==real[om]||(typ[i]==0&&real[om]==1)))
			if(have(i))
			{
			    kind=wx(i,om,kind^1);
			    break;
			}
		if(kind==0&&(typ[i]==3-real[om]||(typ[i]==0&&(real[om]==2||like[om]))))
		    if(have(i))
		    {
		    	kind=wx(i,om,kind^1);
		    	break;
		    }
	}
    return kind;
}//wu xie ke ji
void jz(int x,int killer)
{
    if(tl[x]==0)
    {
        for(int k=1;k<=tot[x];k++)
            if(!used[x][k]&&p[x][k]=='P')
            {
                used[x][k]=1;//cout<<x<<" "<<k<<" "<<p[x][k]<<endl;
                tl[x]++,cnt[x]--;
                break;
            }//have some peaches
        if(tl[x]<1)
        {
            dead[x]=1;
            if(check())  return;
            if(typ[x]==2)  mp(killer,3);
            else  if(typ[x]==1&&killer==1)
            {
                tot[1]=cnt[1]=zb[1]=0;
                memset(p[1],0,sizeof(p[1]));
                memset(used[1],0,sizeof(used[1]));
            }
            nt[pr[x]]=nt[x];
            pr[nt[pr[x]]]=pr[x];
        }//really died                  
    }   
}//kill or save the pig  *
void jd(int x,int y)
{
	if(typ[y]==0&&typ[x]==1)  swap(x,y);
    if(typ[x]==0&&typ[y]==1)
    {
        tl[y]--,jz(y,x);
        return;
    }//main vs zhong
    for(int i=1;i<=tot[x];i++)
        if(!used[x][i]&&p[x][i]=='K')
        {
            op=0,used[x][i]=1,cnt[x]--;
            for(int j=1;j<=tot[y];j++)
                if(!used[y][j]&&p[y][j]=='K')
                {
                    used[y][j]=op=1;
                    cnt[y]--;//cout<<y<<" "<<j<<" "<<p[y][j]<<endl;
                    break;
                }
            if(op==0)
            {
                tl[y]--,jz(y,x);
                if(real[y]&&x!=1)  real[x]=typ[x],like[x]=0;
                return;
            }//y first have no kill
        }
    tl[x]--,jz(x,y);
    if(real[x]&&y!=1)  real[y]=typ[y],like[y]=0;
    return;//x first have no kill
}//jue dou *
void nm(int x)
{
    for(int i=nt[x];i!=x;i=nt[i])
    {
        if(!wx(x,i,1))  continue;
        op=0;
        for(int j=1;j<=tot[i];j++)
            if(!used[i][j]&&p[i][j]=='K')
            {
                used[i][j]=op=1;
                cnt[i]--;//cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
                break;
            }
        if(!op)
        {
            	tl[i]--,jz(i,x);
            	if(check())  return;
            	if(!real[x]&&i==1)  like[x]=1;
        }
    }
}//nan zhu ru qin
void wj(int x)
{
    for(int i=nt[x];i!=x;i=nt[i])
    {
    	if(!wx(x,i,1))  continue;
        op=0;
        for(int j=1;j<=tot[i];j++)
            if(!used[i][j]&&p[i][j]=='D')
            {
                used[i][j]=op=1;
                cnt[i]--;//cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
                break;
            }
        if(!op)
        {
            tl[i]--,jz(i,x);
            if(check())  return;
            if(!real[x]&&i==1)  like[x]=1;
        }
    }
}//wan jian qi fa
int main()
{
	freopen("kopk.in","r",stdin);
	freopen("kopk.out","w",stdout);
	//freopen("t.txt","r",stdin);
	init();
	//int www=0;
	while(1)
	{
		//www++;//if(www==2)  while(1);
		/*cout<<"*******  "<<www<<"  *******"<<endl;
		for(int i=1;i<=n;i++)
		{
            cout<<i<<endl;
			cout<<"tili:"<<tl[i]<<" "<<"dead:"<<dead[i]<<" "<<"cnt:"<<cnt[i]<<endl;
			cout<<"real:"<<real[i]<<" "<<"like:"<<like[i]<<endl;
		    for(int j=1;j<=tot[i];j++)
		    	if(!used[i][j])
		    		cout<<p[i][j]<<" ";
		    cout<<endl<<endl;
		}*/
		if(check())  break;
		for(int i=1;i<=n;i++)
		{   //if(i==2) while(1);
			if(check())  break;
			if(dead[i])  continue;
			mp(i,2);
			cot=0;
            int wwwww=0;
            for(int j=1;j<=tot[i];j++)
            {
            	if(dead[i])   break;
            	if(check())   break;
            	if(used[i][j])  continue;//wwwww++;if(wwwww==13) while(1);
            	if(tl[i]!=4&&p[i][j]=='P')
            	{
            		used[i][j]=1;//cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
            		tl[i]++,cnt[i]--;
            	}//peach  *
            	if(p[i][j]=='K')
            	{
            		if(cot!=0&&!zb[i])   continue;//only once
                    if(typ[i]==0&&!like[nt[i]]&&real[nt[i]]!=2)  continue;
                    if(typ[i]==1&&real[nt[i]]!=2)   continue;
                    if(typ[i]==2&&real[nt[i]]!=1)   continue;//have no enemy  
                    cot++;
                    used[i][j]=1,cnt[i]--,op=0;
                    if(real[nt[i]])  real[i]=3-real[nt[i]],like[i]=0;
                    for(int k=1;k<=tot[nt[i]];k++)
                    	if(!used[nt[i]][k]&&p[nt[i]][k]=='D')
                    	{
                    		used[nt[i]][k]=op=1;//cout<<nt[i]<<" "<<k<<" "<<p[nt[i]][k]<<endl;
                    		cnt[nt[i]]--;
                    		break;
                    	}//miss
                    if(!op)
                    {
                    	tl[nt[i]]--;
                        jz(nt[i],i);
                    }
                }//kill   *
            	if(p[i][j]=='F')
            	{
            		aim=0;
            		if(typ[i]==2)  aim=1;//anti's aim is main
            		else
                    	for(int k=nt[i];k!=i;k=nt[k])
                    		if(real[k]==2||(i==1&&like[k]))
                    		{
                    			aim=k;
                    			break;
                    		}//cout<<"*************"<<aim<<endl;
                    if(aim==0)  continue;
                    if(real[aim]&&i!=1)  real[i]=typ[i],like[i]=0;
                    used[i][j]=1,cnt[i]--,op=0;//cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
                    if(real[aim])
                        if(!wx(i,aim,1))
                        {
                            op=1,j=0;
                            continue;
                        }
                    if(op==0)   jd(aim,i);
                }//fight   *
                if(p[i][j]=='N') 
                {
                    used[i][j]=1,cnt[i]--;	//cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
                    nm(i);
                }//north
                if(p[i][j]=='W')  
                {
                	used[i][j]=1,cnt[i]--;//cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
                    wj(i);//wound
                }
            	if(p[i][j]=='Z')
            	{
            		used[i][j]=zb[i]=1;//cout<<i<<" "<<j<<" "<<p[i][j]<<endl;
            		cnt[i]--;
            	}//zhuge *

            	/*if(used[i][j])
            	{
            	    cout<<"*******  "<<www<<" "<<i<<" "<<p[i][j]<<" hou *******"<<endl;
		            for(int i=1;i<=n;i++)
		            {
                        cout<<i<<endl;
			            cout<<"tili:"<<tl[i]<<" "<<"dead:"<<dead[i]<<" "<<"cnt:"<<cnt[i]<<endl;
			            cout<<"real:"<<real[i]<<" "<<"like:"<<like[i]<<endl;
		                for(int j=1;j<=tot[i];j++)
		    	            if(!used[i][j])
		    		            cout<<p[i][j]<<" ";
		                cout<<endl<<endl;
		            }
	            }*/
                if(used[i][j]&&p[i][j]!='P')  j=0;
            }
          
		}
	}
    outit();
	return 0;
}