6.I - 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.

Input

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

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.

#include<iostream>
#define endl '\n'
using namespace std;
const int N = 107;
int dx[] = {1,0,-1,0}; //表示东南西北方向的模拟
int dy[] = {0,-1,0,1};
struct robot  // 机器人 
{
	int x,y;
	int direction;
}r[N];
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int k;
	cin>>k;
	while(k--)
	{
		int a,b;
		cin>>a>>b;//  a为EW方向    b为NS方向 
		int n,m;
		cin>>n>>m;//  n为机器人数量    m为指令 
		for(int i=1;i<=n;i++)
		{
			char d;
			cin>>r[i].x>>r[i].y>>d;
			if(d=='E') r[i].direction=0;
			if(d=='S') r[i].direction=1;
			if(d=='W') r[i].direction=2;
			if(d=='N') r[i].direction=3;
		}
		bool crash=false;
		for(int i=1;i<=m;i++)
		{
			int num;
			char action;
			int repetition;
			cin>>num>>action>>repetition;
			while(repetition--&&!crash)
			{
				if(action=='L') r[num].direction=(r[num].direction+3)%4;
				if(action=='R') r[num].direction=(r[num].direction+1)%4;
				if(action=='F')
				{
					int rx=r[num].x+dx[r[num].direction];
					int ry=r[num].y+dy[r[num].direction];
					for(int j=1;j<=n;j++)
					{
						if(j!=num&&rx==r[j].x&&ry==r[j].y)
						{
							cout<<"Robot "<<num<<" crashes into robot "<<j<<endl;
							crash=true;
						}
					}
					if(rx<1||rx>a||ry<1||ry>b)
					{           
                        cout<<"Robot "<<num<<" crashes into the wall"<<endl;
                        crash=true;
                    }
					if(!crash)
					{
						r[num].x=rx;
						r[num].y=ry;
					}
				}
			}
		}
		if(!crash)
		{
			cout<<"OK"<<endl;
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值