Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

终于过了

Posted by huangchang9001 at 2011-06-25 22:09:57 on Problem 3760
其实就是代码要写的长些,然后就容易出错误,只要将相关变量定义好,会重复的代码,编入一个函数内就好!
有几个要注意的地方:
1.获取生命元,是总部从城市的到的生命元不是战士从总部获取的生命元,所以在时间点10,和40,都可以发生。
2.插旗.当发生战斗后,有胜者则败者在此城市连胜为0,胜者连胜加。特殊情况是体重括号内连胜是战斗时刻不发生也是连胜,加入有1,2,3,这3个战斗时刻,1 红胜,2 没发生战斗,3 红胜,红方连胜为2 ,若2 发生战斗虽然红方没输,但这不是连胜,红方连胜在时刻2过后为0
下面是本人用C写的代码
#include<stdio.h>
#include<string.h>
#include<memory.h>
// 1 , 2 ,3 , 4, 5 dragon 、ninja、iceman、lion、wolf 
int test,casen;
int M,N,T;//valid need scaned first
int losob[5];//life_of_solider_on_born: dragon、ninja、iceman、lion、wolf
int aosob[5];//attack_of_solider_on_born:dragon 、ninja、iceman、lion、wolf
int rcl,re,lre;//red_command_life(elements),
int bcl,be,lbe;
int rsbs[5]={3,4,5,2,1};//red_solider_born_sequence
int bsbs[5]={4,1,2,3,5};
int ct;//current_time
int rbs,rbn;//red_born_sequence,r_born_numbers
int bbs,bbn;
//0 red,1 blue
int city[22][2];//value in [1,5],city[i][0]=0 表示在城市i中没有红军战士 
int ele[22][2],aele[22][2];//前者是城市i中战士当前生命值,后者是发生战斗前的生命值
int force[22][2],aforce[22][2];//like above
int number[22][2];
int kill[22][2];//在城市i中战士所杀人数
int win[22][2];//红方或蓝方连续获胜次数
int flag[22];//0 无旗,1 红旗 2 蓝旗
int life[22];//the elements in the city i
int db[22];//relation with wolf
int pl[22];//relation with lion
int hpy[22];//relation with dragon
int rw,bw;//rw the number of red has winned
char nos[5][8]={"dragon","ninja","iceman","lion","wolf"};
char color[2][6]={"red","blue"};
void dt(){//display time
	printf("%03d:%02d ",ct/60,ct%60);
}
void w_zero()
{
	if(rcl>=losob[rsbs[rbs]-1])
	{
		rcl-=losob[rsbs[rbs]-1];
		dt();
		printf("red %s %d born\n",nos[rsbs[rbs]-1],++rbn);
		city[0][0]=rsbs[rbs];
		ele[0][0]=losob[rsbs[rbs]-1];
		force[0][0]=aosob[rsbs[rbs]-1];
		kill[0][0]=0;
		number[0][0]=rbn;
		if(rbs==4) rbs=0;
		else rbs++;
	}
    if(bcl>=losob[bsbs[bbs]-1])
	{
		bcl-=losob[bsbs[bbs]-1];
		dt();
		printf("blue %s %d born\n",nos[bsbs[bbs]-1],++bbn);
		city[N+1][1]=bsbs[bbs];
		ele[N+1][1]=losob[bsbs[bbs]-1];
		force[N+1][1]=aosob[bsbs[bbs]-1];
		kill[N+1][1]=0;
		number[N+1][1]=bbn;
		if(bbs==4) bbs=0;
		else bbs++;
	}
}
void w_one()
{
	int i;
	i=N+1;
	while(i>0)
	{
		city[i][0]=city[i-1][0];
		city[i-1][0]=0;
		if(i==N+1&&city[i][0]>0){
			lbe=be;
			be++;
		}
		ele[i][0]=ele[i-1][0];
		ele[i-1][0]=0;

		force[i][0]=force[i-1][0];
		force[i-1][0]=0;
		number[i][0]=number[i-1][0];
		number[i-1][0]=0;
	    kill[i][0]=kill[i-1][0];
		kill[i-1][0]=0;
		if(i%2==0&&city[i][0]==3)
		{
			if(ele[i][0]<=9)
				ele[i][0]=1;
			else
				ele[i][0]-=9;
			force[i][0]+=20;
		}
		i--;
	}
	i=0;

	while(i<=N)
	{
		city[i][1]=city[i+1][1];
		city[i+1][1]=0;
		if(i==0&&city[i][1]>0){
			lre=re;
			re++;
		}
		ele[i][1]=ele[i+1][1];
		ele[i+1][1]=0;
		force[i][1]=force[i+1][1];
		force[i+1][1]=0;
		number[i][1]=number[i+1][1];
		number[i+1][1]=0;
		kill[i][1]=kill[i+1][1];
		kill[i+1][1]=0;
		if((N+1-i)%2==0&&city[i][1]==3)
		{
			if(ele[i][1]<=9)
				ele[i][1]=1;
			else
				ele[i][1]-=9;
			force[i][1]+=20;
		}
		i++;
	}
	if(city[0][1]>0)
	{
		dt();
		printf("blue %s %d reached red headquarter with %d elements and force %d\n",
			nos[city[0][1]-1],number[0][1],ele[0][1],force[0][1]);
	}
    if(re==2)
	{
		dt();
		printf("red headquarter was taken\n");
	}
	i=1;
	while(i<=N)
	{
		if(city[i][0]>0)
		{
			dt();
			printf("red %s %d marched to city %d with %d elements and force %d\n",
				nos[city[i][0]-1],number[i][0],i,ele[i][0],force[i][0]);
		}
		if(city[i][1]>0)
		{
			dt();
			printf("blue %s %d marched to city %d with %d elements and force %d\n",
				nos[city[i][1]-1],number[i][1],i,ele[i][1],force[i][1]);
		}
		i++;
	}
	if(city[N+1][0]>0)
	{
		dt();
		printf("red %s %d reached blue headquarter with %d elements and force %d\n",
			nos[city[N+1][0]-1],number[N+1][0],ele[N+1][0],force[N+1][0]);
	}
	if(be==2)
	{
		dt();
		printf("blue headquarter was taken\n");
	}
		//printf();
}
void w_two()
{
	int i;
	i=1;
	while(i<=N) life[i++]+=10;
}
void dgetlife(int i,int rb)
{
	dt();
	printf("%s %s %d earned %d elements for his headquarter\n",color[rb],nos[city[i][rb]-1],number[i][rb],life[i]);
}
void w_three()
{
	int i;
	i=1;
	while(i<=N)
	{
		if(city[i][0]>0&&city[i][1]==0)
		{
			rcl+=life[i];
			dgetlife(i,0);
			life[i]=0;
		}
		else if(city[i][0]==0&&city[i][1]>0)
		{
			bcl+=life[i];
			dgetlife(i,1);
			life[i]=0;
		}
		i++;
	}
}
void death(int i,int rb)
{
//	dt();
//	printf("%s %s %d was killed int city %d\n",color[rb],nos[city[i][rb]-1],number[i][rb],i);
//	city[i][rb]=0;
	force[i][rb]=0;
	ele[i][rb]=0;
	kill[i][rb]=0;
//	number[i][0]=0;
}
void ddeath(int i ,int rb)
{
	dt();
	printf("%s %s %d was killed in city %d\n",color[rb],nos[city[i][rb]-1],number[i][rb],i);
}
void happy(int i,int rb)
{
	dt();
	printf("%s %s %d yelled in city %d\n",color[rb],nos[city[i][rb]-1],number[i][rb],i);
}
void dreattack(int i,int rb1,int rb2)
{
	dt();
   	printf("%s %s %d fought back ",color[rb1],nos[city[i][rb1]-1],number[i][rb1]);
   	printf("against %s %s %d in city %d\n",color[rb2],nos[city[i][rb2]-1],number[i][rb2],i);

}
void reattack(int i,int rb1,int rb2)
{
	int dead;
	dead=0;
	if(city[i][rb1]!=2)
	{
		if(ele[i][rb2]<=force[i][rb1]/2){
			if(city[i][rb2]==4){
				pl[i]=ele[i][rb2];
			}
			dead=1;
			if(rb1==0) rw++;
			else bw++;
		}
	}
	if(dead==0&&city[i][rb2]==1)
		hpy[i]=1;
	if(dead==1)
		death(i,rb2);
}
void adattack(int i,int rb1,int rb2)
{
	dt();
	printf("%s %s %d attacked ",color[rb1],nos[city[i][rb1]-1],number[i][rb1]);
    printf("%s %s %d in city %d ",color[rb2],nos[city[i][rb2]-1],number[i][rb2],i);
	printf("with %d elements and force %d\n",aele[i][rb1],aforce[i][rb1]);
}
void attack(int i,int rb1,int rb2)
{
	if(ele[i][rb2]<=force[i][rb1])
	{
		kill[i][rb1]++;
		if(city[i][rb1]==5&&kill[i][rb1]%2==0){
			db[i]=2;
		}
		if(city[i][rb2]==4){
			pl[i]=ele[i][rb2];
		}
		death(i,rb2);
		if(city[i][rb1]==1)
			hpy[i]=1;
		if(rb1==0) rw++;
		else bw++;
	}
	else 
		reattack(i,rb2,rb1);
}
void fight(int i)
{
	if(flag[i]==1)
		attack(i,0,1);
	else if(flag[i]==2)
		attack(i,1,0);
	else{
		if(i%2==1) attack(i,0,1);
		else attack(i,1,0);
	}
}
void getlife(int i,int rb)
{
	if(rb==0)
	{
		if(rcl>=8*rw)
		{
			rcl-=8;
			ele[i][0]+=8;

		}
		dgetlife(i,rb);
		rw--;
	}
	else
	{
		if(bcl>=8)
		{
			bcl-=8;
			ele[i][1]+=8;
		}
		dgetlife(i,rb);
	}
}
void raisflag(int i,int rb)
{
	dt();
	printf("%s flag raised in city %d\n",color[rb],i);
}
void dfour(int i,int rb1,int rb2)
{
	adattack(i,rb1,rb2);
	ele[i][rb2]-=aforce[i][rb1];
	if(ele[i][rb2]<0)
		ele[i][rb2]=0;
	if(ele[i][rb2]==0)
	{
		ele[i][rb1]*=db[i];
		force[i][rb1]*=db[i];
		ele[i][rb1]+=pl[i];
		ddeath(i,rb2);
		if(hpy[i]==1)
			happy(i,rb1);
		getlife(i,rb1);
	}
	else
	{
		if(city[i][rb2]!=2)
		{
			dreattack(i,rb2,rb1);
			ele[i][rb1]-=(aforce[i][rb2]/2);
			if(ele[i][rb1]<0)
	        	ele[i][rb1]=0;
		}
		if(ele[i][rb1]==0){
			ddeath(i,rb1);
			ele[i][rb2]+=pl[i];
		}
		if(hpy[i]==1)
			happy(i,rb1);
		if(ele[i][rb1]==0)
			getlife(i,rb2);
	}
	if(ele[i][0]>0&&ele[i][1]==0)
	{
		win[i][1]=0;
		win[i][0]++;
		if(flag[i]==2)
			flag[i]=0;
		else if(flag[i]==0&&win[i][0]==2)
		{
			flag[i]=1;
			raisflag(i,0);
		}
	}
	else if(ele[i][0]==0&&ele[i][1]>0)
	{
		win[i][0]=0;
		win[i][1]++;
		if(flag[i]==1)
			flag[i]=0;
		else if(flag[i]==0&&win[i][1]==2)
		{
			flag[i]=2;
			raisflag(i,1);
		}
	}
	else
	{
		win[i][0]=0;
		win[i][1]=0;
	}
}
void w_four()
{
	int i;
	rw=0;
	bw=0;
	i=1;
	while(i<=N)
	{
		db[i]=1;
		pl[i]=0;
		hpy[i]=0;
		if(city[i][0]>0&&city[i][1]>0){
			aele[i][0]=ele[i][0];
			aele[i][1]=ele[i][1];
			aforce[i][1]=force[i][1];
			aforce[i][0]=force[i][0];
			fight(i);
		}
		i++;
	}
	i=1;
	while(i<=N)
	{
		if(city[i][0]>0&&city[i][1]>0)
		{
			if(flag[i]==1)
			{
				dfour(i,0,1);
			}
			else if(flag[i]==2)
			{ 
				dfour(i,1,0);
			}
			else
			{
				if(i%2==1) dfour(i,0,1);
				else dfour(i,1,0);
			}
		//	if()
		}
		i++;
	}
	i=1;
	while(i<=N)
	{
		if(ele[i][0]>0&&ele[i][1]==0)
		{
			rcl+=life[i];
			//force[i][1]=0;
			city[i][1]=0;
			number[i][1]=0;
			life[i]=0;
		}
		else if(ele[i][0]==0&&ele[i][1]>0)
		{
			bcl+=life[i];
			city[i][0]=0;
			number[i][0]=0;
			life[i]=0;
		}
		i++;
	}
}
void w_five(){
	dt();
	printf("%d elements in red headquarter\n",rcl);
	dt();
	printf("%d elements in blue headquarter\n",bcl);
}
void wot()//work_on_(the)time
{
	int work;
	work=(ct%60)/10;
	if(work==0)
		w_zero();
	else if(work==1)
		w_one();
	else if(work==2)
		w_two();
	else if(work==3)
		w_three();
	else if(work==4)
		w_four();
	else
		w_five();
}
void output()
{
	printf("Case:%d\n",casen++);
	while(ct<=T)
	{
		wot();
		if(re==2||be==2) break;
		ct+=10;
	}
}
void input()
{
	int i;
	scanf("%d%d%d",&M,&N,&T);
	i=0;
	while(i<5) scanf("%d",&losob[i++]);
	i=0;
	while(i<5) scanf("%d",&aosob[i++]);
}
void init()
{
	ct=0;
	memset(city,0,sizeof(city));
	memset(flag,0,sizeof(flag));
	memset(life,0,sizeof(life));
	memset(ele,0,sizeof(ele));
	memset(force,0,sizeof(force));
	memset(number,0,sizeof(number));
	memset(kill,0,sizeof(kill));
	memset(win,0,sizeof(win));
	rcl=M;
	bcl=M;
	rbs=0;
	rbn=0;
	bbs=0;
	bbn=0;
	re=0;
	lre=0;
	be=0;
	lbe=0;
}
void main()
{
	scanf("%d",&test);
	casen=1;
	while(test--)
	{
		input();
		init();
		output();
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator