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 19851105 at 2005-04-25 03:27:27 on Problem 1600
#include<iostream.h>

int board[30][30];
class Centipede
{
public:	
	static int count;
	Centipede *next; 

	void set(char,int,int,int);
	void set(int,int,int,int,int);
	void walk();
	void crush();
	void fallOff();  
private:
	int direction[2];
	int Long; 
	int head[2];
}; 
int Centipede::count=0;

void Centipede::set(char d,int l,int x,int y)
{
	switch(d)
	{
	case'U':
		direction[0]=0;direction[1]=1;break;
	case'D':
		direction[0]=0;direction[1]=-1;break;
	case'R':
		direction[0]=1;direction[1]=0;break;
	case'L':
		direction[0]=-1;direction[1]=0;
	}
	set(direction[0],direction[1],l,x,y);
}

void Centipede::set(int d0,int d1,int l,int x,int y)
{
	direction[0]=d0;
	direction[1]=d1;
	Long=l;
	head[0]=x;
	head[1]=y;
	for(int i=0;i<Long;i++)
		board[head[1]-i*direction[1]][head[0]-i*direction[0]]=1;  
}

void Centipede::walk()
{  
	if(Long>0)
	{ 
		if((head[0]+direction[0])>29||(head[1]+direction[1])>29||(head[0]+direction[0])<0||
			(head[1]+direction[1])<0)
			fallOff();
		else
		{ 
			head[0]+=direction[0];
			head[1]+=direction[1];
			board[head[1]][head[0]]++;
			board[head[1]-Long*direction[1]][head[0]-Long*direction[0]]--;
		}
	} 
}

void Centipede::crush()
{ 
	for(int i=0;i<Long;i++)
		if(board[head[1]-i*direction[1]][head[0]-i*direction[0]]>1)
		{
			if(Long==1)
			{
				Long=0;
				count--;
			}
			else if(i==0)
			{
				head[0]=head[0]-direction[0];
				head[1]=head[1]-direction[1];
				Long-=1;
			}
			else if(i==Long-1)
				Long-=1;
			else
			{
				count++;
				Centipede * newc=new Centipede;
				newc->next=next;
				next=newc;
				newc->set(direction[0],direction[1],Long-i-1,head[0]-(i+1)*direction[0],
					head[1]-(i+1)*direction[1]);

				Long=i;
			}
		}
}

void Centipede::fallOff()
{ 
	Long--;
	if(Long==0)
		count--; 
	board[head[1]-Long*direction[1]][head[0]-Long*direction[0]]--;
}

void main()
{
	int number,Long,x,y;
	char direction;
	Centipede * head=NULL,*pf;
	cin>>number;
	Centipede::count=number;
	for(int i=0;i<number;i++)
	{
		cin>>direction>>Long>>x>>y;
		Centipede *p=new Centipede;
		p->next=NULL;
		p->set(direction,Long,x,y);
		if(head==NULL)
			head=p;
		else
		{
			for(pf=head;pf->next!=NULL;pf=pf->next)
				; 
			pf->next=p;
		}
	} 
 	while(Centipede::count!=0)
	{  
		for(pf=head;pf!=NULL;pf=pf->next)
		{	pf->walk(); 
			Centipede *pf2;
			for(pf2=head;pf2!=NULL;pf2=pf2->next)
				pf2->crush();  
		} 
	}  
 
	cout<<"   ";
	for(i=0;i<10;i++)
		cout<<"0 ";
	for(i=0;i<10;i++)
		cout<<"1 ";
	for(i=0;i<10;i++)
		cout<<"2 ";
	cout<<endl<<"   ";
	for(i=0;i<10;i++)
		cout<<i<<' ';
	for(i=0;i<10;i++)
		cout<<i<<' ';
	for(i=0;i<10;i++)
		cout<<i<<' ';
	cout<<endl;

	for(i=29;i>=0;i--)
	{
		if(i>9)
			cout<<i<<' ';
		else
			cout<<0<<i<<' ';
		for(int j=0;j<30;j++)
			if(board[i][j]==0)
				cout<<". ";
			else
				cout<<"X ";
		cout<<endl;
	}
	while(head!=NULL)
	{
		pf=head->next;
		delete head;
		head=pf;
	}
}

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