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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值