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 531338577 at 2013-03-18 23:00:16 on Problem 2632
#include<stdio.h>
struct Robot{
	int x,y;
	int direction;//0:N ,1:W ,2:S ,3:E
};
struct Instruction{
	int x,y;
	char ch;
};

int a,b,n,m;
int judge(Robot robots[],int i,int j);

int main()
{
	bool flag;
	int t,i,j,k,x,y;
	char ch;
	Robot robots[105];
	Instruction orders[105];
	scanf("%d",&t);
	while(t--)
	{
		flag=1;
		scanf("%d%d",&a,&b);
		scanf("%d%d",&n,&m);   
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%*c%c%*c",&robots[i].x,&robots[i].y,&ch);
			if(ch=='N')  robots[i].direction=0;
			else if(ch=='W')  robots[i].direction=1;
			else if(ch=='S')  robots[i].direction=2;
			else if(ch=='E')  robots[i].direction=3;
		}
		for(i=0;i<m;i++)  scanf("%d%*c%c%*c%d",&orders[i].x,&orders[i].ch,&orders[i].y);
		
		for(x=0;x<m;x++)
		{
			i=orders[x].x;
			j=orders[x].y;
			ch=orders[x].ch;
			if(ch=='L')
			{
				robots[i].direction=(robots[i].direction+j)%4;
			}
			else if(ch=='R')
			{
				robots[i].direction=(robots[i].direction-j)%4;
				if(robots[i].direction<0) robots[i].direction+=4;
			}
			else
			{
				k=judge(robots,i,j);
                if(k==0)  {printf("Robot %d crashes into the wall\n",i);flag=0;}
				else if(k>0)  {printf("Robot %d crashes into robot %d\n",i,k);flag=0;}
			}
		}
		if(flag)
		    printf("OK\n");
	}
}

int judge(Robot robots[],int i,int j)
{
	int k;
	int tem;
	if(robots[i].direction==0){
	      tem=robots[i].y;
	      robots[i].y+=j;        
	      for(k=1;k<=n;k++)
	      {
				if(k!=i&&robots[k].x==robots[i].x&&robots[k].y>=tem&&robots[k].y<=robots[i].y)
						return k;
	 	  }
	 	  if(robots[i].y>b)  return 0;
	 	  else return -1;
	}
	else if(robots[i].direction==1){
	      tem=robots[i].x;
	      robots[i].x-=j;
	      for(k=1;k<=n;k++)
	      {
				if(k!=i&&robots[k].y==robots[i].y&&robots[k].x>=robots[i].x&&robots[k].x<=tem)
						return k;
	 	  }
	 	  if(robots[i].x<1)  return 0;
	 	  else return -1;
	}
	else if(robots[i].direction==2){
	      tem=robots[i].y;
	      robots[i].y-=j;
	      for(k=1;k<=n;k++)
	      {
				if(k!=i&&robots[k].x==robots[i].x&&robots[k].y>=robots[i].y&&robots[k].y<=tem)
						return k;
	 	  }
	 	  if(robots[i].y<1)  return 0;
	 	  else return -1;
	}
	else if(robots[i].direction==3){
	      tem=robots[i].x;
	      robots[i].x+=j;
	      for(k=1;k<=n;k++)
	      {
				if(k!=i&&robots[k].y==robots[i].y&&robots[k].x>=tem&&robots[k].x<=robots[i].x)
						return k;
	 	  }
	 	  if(robots[i].x>a)  return 0;
	 	  else return -1;
	}
}



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