J - Crashing Robots

J - 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.

Sample Input:

在这里插入图片描述
在这里插入图片描述

Sample Output:

在这里插入图片描述

题目大意:

这道题讲了给出了一个m*n的方格中,放置了k个机器人,并且给出了b条指令,并且每个机器人都有他们初始位置以及一个方向,指令中 z,x,c,z代表机器人编号,c代表操作指令,z代表操作次数,当c为F的时候就是向机器人所指的方向走z步,如果是L就是向左转z次,如果是R就是向右转z次,有W,E,N,S四个方向,并且如果机器人撞墙就结束,输出,以及机器人之间相撞也结束,输出,(都是以第一次坠机为主,也就是说如果有4个机器人,在第一条指令中,机器人就已经撞墙,后面指令可以不用看了)

思路分析:

这道题主要讲的是模拟法,一个一个模拟,没有多难的算法,但是有点搞不懂为什么自己代码会是0ms运行时间,晕了,我觉得自己代码特别麻烦。

代码:

	#include<stdio.h>
	#include<string.h>
	int x2,y2,m;
	char c1[201],c3[4][4]={
	'E','S','W','N',
	'W','N','E','S',
	'S','W','N','E',
	'N','E','S','W'
	},
	c4[4][4]={
	'E','N','W','S',
	'W','S','E','N',
	'S','E','N','W',
	'N','W','S','E'
	};
	void lemon3(int a,int g);
	void lemon1()
	{
		scanf("%d %d",&x2,&y2);
	}
	void lemon2()
	{
		int b,c,d;
		scanf("%d %d",&b,&c);
		lemon3(c,b);
		
	}
	void lemon3(int a,int g)
	{
		int b,c,x[200][200]={0},d,count=0,num[2],h=0,j;
		int x1[201],y1[201],x3,y3;
		char c2,c5[100];
		m=0;
			for(d=1;d<=g;d++)
		{
			scanf("%d %d %c",&x1[d],&y1[d],&c1[d]);
			getchar();
			x[y1[d]][x1[d]]=d;
		}
			for(d=0;d<a;d++)
		{	
		scanf("%d %c %d",&b,&c2,&j);
		if(d!=a-1)
		getchar();
		x3=x1[b];
		y3=y1[b];
		if(c2=='F' && count==0)
		{
			for(c=1;c<=j;c++)
			{
			if(c1[b]=='E' && count==0)
			{
				x1[b]=x1[b]+1;
				if(x1[b]>x2)
				{
					strcpy(c5,"crashes into the wall");
					num[h++]=b;
					count=1;
					continue;
					}
				else if(x[y1[b]][x1[b]]!=0)
				{
				    strcpy(c5,"crashes into robot");
					num[h++]=x[y1[b]][x1[b]];
					num[h++]=b;
					count=1;
					continue;
				}
			}
			if(c1[b]=='W' && count==0)
			{
				x1[b]-=1;
					if(x1[b]<1)
				{
					strcpy(c5,"crashes into the wall");
					num[h++]=b;
					count=1;
					continue;
				}
				else if(x[y1[b]][x1[b]]!=0)
				{
				strcpy(c5,"crashes into robot");
					num[h++]=x[y1[b]][x1[b]];
					num[h++]=b;
					count=1;
					continue;
			}
			}
			if(c1[b]=='S' && count==0)
			{
				y1[b]-=1;
					if(y1[b]<1)
				{
					strcpy(c5,"crashes into the wall");
					num[h++]=b;
					count=1;
					continue;
				}
				else if(x[y1[b]][x1[b]]!=0)
				{
					strcpy(c5,"crashes into robot");
					num[h++]=x[y1[b]][x1[b]];
					num[h++]=b;
					count=1;
					continue;
				}
			}
			if(c1[b]=='N' && count==0) 
			{
				y1[b]+=1;
					if(y1[b]>y2)
				{
					strcpy(c5,"crashes into the wall");
					num[h++]=b;
					count=1;
					continue;
				}
				else if(x[y1[b]][x1[b]]!=0)
				{
					strcpy(c5,"crashes into robot");
					num[h++]=x[y1[b]][x1[b]];
					num[h++]=b;
					count=1;
					continue;
				}
			}
		}
	}
		if(c2=='R' && count==0)
		{
		if(c1[b]=='E' && count==0)
		{
			c1[b]=c3[0][j%4];
			continue;
		}
		if(c1[b]=='W'&& count==0)
		{
			c1[b]=c3[1][j%4];
			continue;
		}
		if(c1[b]=='N'&& count==0)
		{
			c1[b]=c3[3][j%4];
			continue;
		}
		if(c1[b]=='S'&& count==0)
		{
			c1[b]=c3[2][j%4];
			continue;
		}
	}
	if(c2=='L')
		{
		if(c1[b]=='E'&& count==0)
		{
			c1[b]=c4[0][j%4];
			continue;
		}
		if(c1[b]=='W'&& count==0)
		{
			c1[b]=c4[1][j%4];
			continue;
		}
		if(c1[b]=='N'&& count==0)
		{
			c1[b]=c4[3][j%4];
			continue;
		}
		if(c1[b]=='S'&& count==0)
		{
			c1[b]=c4[2][j%4];
			continue;
		}
	}
	x[y3][x3]=0;
	x[y1[b]][x1[b]]=b;
	}
	if(!count)
	{
		m=1;
	}
	else
	{
		if(h==2)
		{
			printf("Robot %d %s %d\n",num[1],c5,num[0]);
		}
		else
		{
		printf("Robot %d %s\n",num[0],c5);
	}
	}
	}
	int main()
	{
		int b,c,e;
		scanf("%d",&e);
		while(e--)
		{
		lemon1();
		lemon2();
		if(m==1)
		{
			printf("OK\n");
		}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值