POJ 2632 Crashing Robots 模拟

题意:一个矩形,分成了A*B个大小相同的正方形,把n个机器人放在某些小正方形里,给他们一些指令,他们会一步一步按指令行动,每次只有一个机器人行动。如果两个机器人在同一个小正方形里了他们会相撞;如果出界了,会撞到墙。输出做一些指令之后机器人的第一次相撞的状态。

#include <iostream>
using namespace std;

int map[101][101];
int a,b;

struct rob
{
	int x, y, dre;
} robot[101];

struct instruction
{
	int ro_id, step;
	char point;
} ins[101];

bool proccess ( struct instruction act )
{
	int i,  id = act.ro_id;
	while ( act.step-- )
	{
		if ( act.point == 1 )
			robot[id].dre = ( robot[id].dre + 1 ) % 4;
		else if ( act.point == -1 )
		{
			robot[id].dre -= 1;
			if ( robot[id].dre == -1 )
				robot[id].dre = 3;
		}
		else if ( act.point == 0 )
		{
			map[robot[id].x][robot[id].y] = 0;
			switch ( robot[id].dre )
			{
			case 0: robot[id].y ++; break;
			case 1: robot[id].x --; break;
			case 2: robot[id].y --; break;
			case 3: robot[id].x ++; break;
			}
			if ( robot[id].y < 1 || robot[id].x < 1 || robot[id].x > a || robot[id].y > b )
			{
				cout << "Robot " << id << " crashes into the wall" << endl;
				return false;
			}
			else if ( map[robot[id].x][robot[id].y] != 0 )
			{
				cout<< "Robot " << id << " crashes into robot " << map[robot[id].x][robot[id].y] << endl;
				return false;
			}
			map[robot[id].x][robot[id].y] = id;
		}
	}
	return true;
}

int main()
{
	char ch;
	int k,n,m;
	cin >> k;
	while ( k-- )
	{
		memset(map,0,sizeof(map));
		memset(ins,0,sizeof(ins));
		
		cin >> a >> b;
		cin >> n >> m;
		int i;
		for ( i = 1; i <= n; i++ )
		{
			cin >> robot[i].x >> robot[i].y >> ch;
			if ( ch == 'N' )
				robot[i].dre = 0;
			else if ( ch == 'W' )
				robot[i].dre = 1;
			else if ( ch == 'S' )
				robot[i].dre = 2;
			else if ( ch == 'E' )
				robot[i].dre = 3;
			map[robot[i].x][robot[i].y] = i;
		}
		
		for ( i = 1; i <= m; i++ )
		{
			cin >> ins[i].ro_id >> ch >> ins[i].step;
			if ( ch == 'L' )
				ins[i].point = 1;
			else if ( ch == 'R' )
				ins[i].point = -1;
			else
				ins[i].point = 0;
		}
		
		for ( i = 1; i <= m; i++ )
		{
			if ( ! proccess ( ins[i] ) )
				break;
		}
		if  ( i > m )
			cout << "OK" << endl;
	}
	return 0;
}


你可能感兴趣的:(POJ 2632 Crashing Robots 模拟)