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();
} 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章