Crashing Robots

描述:

In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occupy a circular floor space with a diameter of 1 meter. Assume there are N robots, numbered from 1 through N. You will get to know the position and orientation of each robot, and all the instructions, which are carefully (and mindlessly) followed by the robots. Instructions are processed in the order they come. No two robots move simultaneously; a robot always completes its move before the next one starts moving.

A robot crashes with a wall if it attempts to move outside the area of the warehouse, and two robots crash with each other if they ever try to occupy the same spot.


输入:

The first line of input is K, the number of test cases. Each test case starts with one line consisting of two integers, 1 <= A, B <= 100, giving the size of the warehouse in meters. A is the length in the EW-direction, and B in the NS-direction.

The second line contains two integers, 1 <= N, M <= 100, denoting the numbers of robots and instructions respectively.
Then follow N lines with two integers, 1 <= Xi <= A, 1 <= Yi <= B and one letter (N, S, E or W), giving the starting position and direction of each robot, in order from 1 through N. No two robots start at the same position.


Figure 1: The starting positions of the robots in the sample warehouse


Finally there are M lines, giving the instructions in sequential order.
An instruction has the following format:
< robot #> < action> < repeat>
Where is one of

  • L: turn left 90 degrees,
  • R: turn right 90 degrees, or
  • F: move forward one meter,

and 1 <= < repeat> <= 100 is the number of times the robot should perform this single move.


输出:

Output one line for each test case:


  • Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)
  • Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.
  • OK, if no crashing occurs.


Only the first crash is to be reported.


样例输入:

4
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20


样例输出:

Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2


题目大意:

先输入一个正整数代表数据组数,对于每一组数据先输入2个正整数表示地图大小,再输入两个数num和com表示机器人个数和指令条数,接下去num行表示机器人的初始位置和面对方向,后com行每行一个指令< robot #> < action> < repeat>表示第几个机器人做了什么动作完成了几次,判断在指令过程中机器人是否会相互碰撞,是否会撞墙。输出最先出现的问题,如果所有指令都完成都没发生碰撞就输出OK。



代码如下:

#include<stdio.h>
int t,mapx,mapy,num,com,flag,mark,mark1,mark2,mark3;
struct robot
{
	int x;
	int y;
	int dic;												//0=w,1=s,2=e,3=n
}gw[105];
void check(int a,int b)
{	
	for(int i=0;i<num;i++)									//每走一步判断该机器人会不会与其他机器人相撞 
	{
		if(i!=mark)
		{
			if(a==gw[i].x&&b==gw[i].y)
			{
				flag=2;
				mark1=mark+1;
				mark2=i+1;
				break;
			}		
		}
	}
}
void sove(int q,char w,int e)
{
	if(w=='L')
	{
		gw[q-1].dic=(gw[q-1].dic+e)%4;							//顺时针旋转e次后的方向 
	}
	else if(w=='R')
	{
		gw[q-1].dic=(gw[q-1].dic-e+100)%4;						//逆时针旋转e次后的方向  
	}	
	else
	{
		for(int i=0;i<e;i++)
		{
			if(gw[q-1].dic==0)
			{
				gw[q-1].x-=1;
				if(gw[q-1].x<=0||gw[q-1].x>mapx||gw[q-1].y<=0||gw[q-1].y>mapy)			//碰壁的情况将flag赋为0 
				{
					flag=0;
					mark3=mark+1;
					break;
				}	
				check(gw[q-1].x,gw[q-1].y);												
				if(flag!=1)
				break;
			}
			else if(gw[q-1].dic==1)
			{
				gw[q-1].y-=1;
				if(gw[q-1].x<=0||gw[q-1].x>mapx||gw[q-1].y<=0||gw[q-1].y>mapy)
				{
					flag=0;
					mark3=mark+1;
					break;
				}					
				check(gw[q-1].x,gw[q-1].y);
				if(flag!=1)
				break;				
			}		
			else if(gw[q-1].dic==2)
			{
				if(gw[q-1].x<=0||gw[q-1].x>mapx||gw[q-1].y<=0||gw[q-1].y>mapy)
				{
					flag=0;
					mark3=mark+1;
					break;
				}					
				gw[q-1].x+=1;
				check(gw[q-1].x,gw[q-1].y);
				if(flag!=1)
				break;				
			}
			else
			{
				if(gw[q-1].x<=0||gw[q-1].x>mapx||gw[q-1].y<=0||gw[q-1].y>mapy)
				{
					flag=0;
					mark3=mark+1;
					break;
				}					
				gw[q-1].y+=1;
				check(gw[q-1].x,gw[q-1].y);
				if(flag!=1)
				break;				
			}							
		}
	}
}
void enter()
{
	scanf("%d",&t);
	int a[105],b[105];
	char c[105];
	while(t--)
	{
		flag=1;
		scanf("%d %d",&mapx,&mapy);
		scanf("%d %d",&num,&com);
		for(int i=0;i<num;i++)
		{
			scanf("%d %d %c",&a[i],&b[i],&c[i]);
			gw[i].x=a[i];
			gw[i].y=b[i];
			if(c[i]=='E')
			gw[i].dic=2;
			if(c[i]=='N')
			gw[i].dic=3;
			if(c[i]=='W')
			gw[i].dic=0;
			if(c[i]=='S')
			gw[i].dic=1;									
		}
		for(int i=0;i<com;i++)
		{
			scanf("%d %c %d",&a[i],&c[i],&b[i]);
		}
		for(int i=0;i<com;i++)
		{
			mark=a[i]-1;
			sove(a[i],c[i],b[i]);
			if(flag!=1)
			break;
		}
		if(flag==1)
		printf("OK\n");
		else if(flag==2)
		printf("Robot %d crashes into robot %d\n",mark1,mark2);
		else
		printf("Robot %d crashes into the wall\n",mark3);
	}
}
int main()
{
	enter();
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值