poj1573--Robot Motion/POJ 2632 Crashing Robots(两道关于机器人的模拟水题)

 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

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

Sample Output

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

题意:这道题特别难懂,我也是读了很长很长很长时间才看懂。大概意思就是有一个仓库,有很多机器人接受指令然后移动。每次只能一个机器人接受指令然后移动完,下一个指令才能被机器人执行。现在给你N个机器人和每个机器人的位置,M条指令和仓库的大小,问你机器人在执行指令时是否会撞墙,两个机器人是否会相撞,如果都没有,就输出OK。

就是模拟水题,直接模拟就完事了。

具体代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef struct{
	int i,j;
	int d;//1左2上3右4下 
}point;
int main()
{
	int N;
	cin>>N;
	while(N--)
	{
		int a,b;//仓库的长宽
		int num,k;//机器人数,指令数
		point robot[105];//初始化 
		int i,j;
		cin>>a>>b;
		cin>>num>>k;
		for(i=1;i<=num;i++)//记录每个机器人的位置 
		{
			char a3;
			int a1,a2;
			char p[10];
			scanf("%s",p);
			sscanf(p,"%d",&a1);
			scanf("%s",p);
			sscanf(p,"%d",&a2);
			scanf("%s",p);
			a3=p[0];
			robot[i].i=a1;
			robot[i].j=a2;
			if(a3=='E')
				robot[i].d=3;
			else if(a3=='W') 
				robot[i].d=1;
			else if(a3=='S') 
				robot[i].d=4;
			else
				robot[i].d=2;
		}
		int flag=0;
		int finally[10]={0};
		while(k--)
		{
			char a2;
			int a1,a3;
			char p[10];
			scanf("%s",p);
			sscanf(p,"%d",&a1);
			scanf("%s",p);
			a2=p[0];
			scanf("%s",p);
			sscanf(p,"%d",&a3);
			if(flag==0)
			{
				
				if(a2=='F')//移动位置 
				{				
					while(a3--)//机器人每走一步就判断一次是否出事故 
					{
						if(robot[a1].d==1)
							robot[a1].i--;
						else if(robot[a1].d==2)
							robot[a1].j++;
						else if(robot[a1].d==3)
							robot[a1].i++;
						else
							robot[a1].j--;
						//判断两个机器人是否相撞 						
						for(i=1;i<=num;i++) 
							if(robot[a1].i==robot[i].i&&robot[a1].j==robot[i].j&&(a1!=i))
							{
								flag=1;
								finally[0]=2;
								finally[1]=a1;
								finally[2]=i;
								break;
							}						
						//判断是否撞墙
						if(robot[a1].i<1||robot[a1].j<1||robot[a1].i>a||robot[a1].j>b) 
						{
							flag=1;
							finally[1]=a1;
							finally[0]=1;
						}
						if(flag==1)
							break;
					}					
				}
				else if(a2=='R')//右旋转 
				{
					a3%=4;
					robot[a1].d+=a3;
					robot[a1].d%=4;
				}
				else if(a2=='L')//左旋转 
				{
					a3%=4;
					robot[a1].d=robot[a1].d+4-a3;
					robot[a1].d%=4;
				}
			
			}		
		}
		if(finally[0]==0)
			printf("OK\n");
		else if(finally[0]==1)
			printf("Robot %d crashes into the wall\n",finally[1]);
		else
			printf("Robot %d crashes into robot %d\n",finally[1],finally[2]);
	}
	return 0;
}

Robot Motion


A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.

Input

There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

Output

For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.

Sample Input

3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0

Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

题意:给你一个地图,ESWN分别表示向东南西北走一步,两种情况:1、机器人能不能走出地图 2、机器人陷入死循环

模拟水题。

具体代码如下:

#include<iostream>
#include<cstdio>
using namespace std;
typedef struct{
	int t[12];//判断是否走过 
	char num[12];
}point;
int main()
{
	int n,m,k;
	while(cin>>n>>m>>k,n&&m&&k)
	{
		point a[12];
		int i,j;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				cin>>a[i].num[j];
				a[i].t[j]=0;
			}
		int sum=0;
		i=0,j=k-1;
		while(!(a[i].t[j]==1||(i<0||j<0||i>=n||j>=m)))
		{
			a[i].t[j]=1;
			if(a[i].num[j]=='N')
			{
				i--;				
			}				
			else if(a[i].num[j]=='S')
			{
				i++;
			}
			else if(a[i].num[j]=='W')
			{
				j--;
			}
			else if(a[i].num[j]=='E')
			{
				j++;
			}
			sum++;	
		}
		if(i<0||j<0||i>=n||j>=m)
			printf("%d step(s) to exit\n",sum);
		else
		{
			int back=0;
			int p=i,q=j;
			i=0,j=k-1;
			while(!(i==p&&j==q))
			{
				if(a[i].num[j]=='N')
				{
					i--;				
				}				
				else if(a[i].num[j]=='S')
				{
					i++;
				}
				else if(a[i].num[j]=='W')
				{
					j--;
				}
				else if(a[i].num[j]=='E')
				{
					j++;
				}
				back++;	
			}	
			printf("%d step(s) before a loop of %d step(s)\n",back,sum-back);
		}	
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42391248/article/details/81147443