POJ2632 Crashing Robots 模拟


Crashing Robots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9345   Accepted: 3997

Description

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. 
POJ2632 Crashing Robots 模拟_第1张图片 
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
题目大意:模拟机器人的运动,给定地图,机器人的初始位置和方向,以及一些指令,判断机器人在运动时是否会撞墙或者撞到其他机器人。

思路:用一个二维数组模拟地图,定义-1为墙壁,0为空地,大于0的数组为此时该地有机器人,数字为机器人编号,运动时若不为0则记录该值,并记录当前运动的机器人编号然后读完数据即可输出。没有什么算法可言,只是需要耐心。

实现代码:

//************************************************************************//
//*Author : Handsome How                                                 *//
//************************************************************************//
//#pragma comment(linker, "/STA	CK:1024000000,1024000000")
#pragma warning(disable:4996) 
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>                                                
#include <cassert>
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
typedef long long LL;
const int dirx[] = { 0,1,0,-1 };							  
const int diry[] = { 1,0,-1,0 };							      
int fig[105][105];											
int X, Y;											
struct Robot
{
	int x;
	int y;
	int dir;
}robot[105];
void init(int x,int y)
{
	memset(fig, 0, sizeof(fig));
	fur(i, 1, y+1)fig[x + 1][i] = -1;
	fur(i, 1, x)fig[i][y + 1] = -1;
	fur(i, 0, 104)fig[0][i] = -1;
	fur(i, 1, 104)fig[i][0] = -1;			  //对地图初始化,边界为-1,其他为0
}
void turnl(int n) { robot[n].dir = (robot[n].dir + 3) % 4; }
void turnr(int n) { robot[n].dir = (robot[n].dir + 1) % 4; }
int go(int n, int t)
{
	int dir = robot[n].dir;
	fig[robot[n].x][robot[n].y] = 0;
	while (t--)
	{
		robot[n].x += dirx[dir];
		robot[n].y += diry[dir];
		if (fig[robot[n].x][robot[n].y]!=0)return fig[robot[n].x][robot[n].y];	//若不是空地立刻返回					
	}
	fig[robot[n].x][robot[n].y] = n;
	return 0;
}
int main()
{
	//freopen("E:\\data.in", "r", stdin);
	//freopen("E:\\data.out", "w", stdout);
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &X, &Y);
		init(X, Y);
		int crasha = 0, crashb = 0;
		int numb, q;
		char str[5];
		scanf("%d%d", &numb, &q);
		fur(i, 1, numb)
		{
			scanf("%d%d%s", &robot[i].x, &robot[i].y, str);
			fig[robot[i].x][robot[i].y] = i;
			switch (str[0])
			{
			case 'N': robot[i].dir = 0; break;
			case 'E': robot[i].dir = 1; break;
			case 'S': robot[i].dir = 2; break;
			case 'W': robot[i].dir = 3; break;
			}
		}
		int n, t;
		fur(i, 1, q)
		{
			scanf("%d%s%d", &n,str,&t);
			if (crashb == 0)	  //若以有结果则只需要读完数据
			{
				if (str[0] == 'L')
				{
					t = t % 4;
					while (t--)
						turnl(n);
					continue;
				}
				if (str[0] == 'R')
				{
					t = t % 4;
					while (t--)
						turnr(n);
					continue;
				}
				crashb = go(n, t);
				if (crashb)crasha = n;				
			}
		}
		if (crashb == 0)printf("OK\n");
		else if (crashb == -1)printf("Robot %d crashes into the wall\n",crasha);
		else printf("Robot %d crashes into robot %d\n",crasha,crashb);
	}								
	return 0;
}



你可能感兴趣的:(模拟)