POJ2632——Crashing Robot

POJ2632



/*

***********************************************************
算法原理:
1)方向的问题用Direction[10]的'N','W','S','E'的环形结构
2)将Warehouse[100][100]初始化为 0 
   在输入时循环将当前的Warehouse[A][B]改为 1
   被机器人占领的坐标
   Warehouse[robotState[i].x][robotState[i].y] = i + 100

Notice:
整个过程比较细碎,调试要耐心
***********************************************************

*/
#include<iostream>

using namespace std;
struct RobotState
{
	int id = 0;
	int x = 0;
	int y = 0;
	char direction = 0;

};

char Direction[10] = {'W','S','E','N','W','S','E','N','W','S' };	//转向,一个环形结构
int Warehouse[100][100];											//墙设置为0
RobotState robotState[100];
int flag, robot_id, crash_id;			

int K, A, B, N, M;						//K is the numbers of test cases
										//A is the length in E-W; B is the length in N-S
										//N is the numbers of robots; M is the numbers of instructions

void executeCommond(RobotState &i,char commond,int repeat)
{
	int posA = i.x, posB = i.y, p; char d = i.direction;
	int step = repeat % 4;
	for (p = 4; p < 8 && Direction[p] != d; p++);
	if ('L' == commond)
		d = Direction[p + step];
	else if ('R' == commond)
		d = Direction[p - step];
	else
	{
		Warehouse[posA][posB] = 1;								//在'F'指令下再恢复有效位置
		while (repeat-- && (Warehouse[posA][posB] == 1))		//处于边界内且没有机器人的位置
		{														//即 Warehouse[i][j] = 1
			switch (d)
			{
			case 'N':posB++; break;
			case 'S':posB--; break;
			case 'W':posA--; break;
			case 'E':posA++; break;
			default:break;
			}
		}
		int temp = Warehouse[posA][posB];
		if (temp == 0)
		{
			flag = 1; 
			robot_id = i.id + 1;
		}
		else if (temp > 1)
		{
			flag = 2;
			robot_id = i.id + 1;
			crash_id = temp - 100 + 1;
		}
		else
			Warehouse[posA][posB] = i.id + 100;
	}
	i.direction = d, i.x = posA, i.y = posB;
}
int main()
{

	cin >> K;			
	while (K--)			
	{
		flag = 0;
		cin >> A >> B;
		for (int i = 0; i <= A; i++)								//设置有效面积为1
			for (int j = 0; j <= B; j++)
				Warehouse[i][j] = 1;

		cin >> N >> M;
		for (int i = 0; i < N; i++)
		{
			robotState[i].id = i;
			cin >> robotState[i].x >> robotState[i].y
				>> robotState[i].direction;
			Warehouse[robotState[i].x][robotState[i].y] = i + 100; //表示被机器人所占
		}
		while (M--)
		{
			int j, r; char c;			//j = id, r = repeat ,c = commond
			cin >> j >> c >> r;
			if (!flag)
				executeCommond(robotState[j - 1], c, r);
		}
		if (1 == flag)
			cout << "Robot " << robot_id << " crashes into the wall" << endl;
		else if (2 == flag)
			cout << "Robot " << robot_id << " crashes into robot " << crash_id << endl;
		else
			cout << "OK" << endl;
	}
}


/*

***********************************************************
改进:
将executeCommond放进main()
便可以把struct RobotState中的 int id删去

Notice:
数组下标和序号的统一
***********************************************************

*/
#include<iostream>

using namespace std;
struct RobotState
{
	int x = 0;
	int y = 0;
	char direction = 0;
};

char Direction[10] = {'W','S','E','N','W','S','E','N','W','S' };	//转向,一个环形结构
int Warehouse[100][100];											//墙设置为0
RobotState robotState[100];		

int main()
{
	int K, A, B, N, M;						//K is the numbers of test cases
											//A is the length in E-W; B is the length in N-S
											//N is the numbers of robots; M is the numbers of instructions
	int flag, robot_id, crash_id;
	cin >> K;			
	while (K--)			
	{
		flag = 0;
		cin >> A >> B;
		for (int i = 0; i <= A; i++)								//设置有效面积为1
			for (int j = 0; j <= B; j++)
				Warehouse[i][j] = 1;

		cin >> N >> M;
		for (int i = 0; i < N; i++)
		{
			cin >> robotState[i].x >> robotState[i].y
				>> robotState[i].direction;
			Warehouse[robotState[i].x][robotState[i].y] = i + 100 + 1; //表示被机器人所占
		}
		while (M--)
		{
			int j, r; char c;			//j = id, r = repeat ,c = commond
			cin >> j >> c >> r;
			RobotState &i = robotState[j - 1];
			if (!flag)
			{
				int posA = i.x, posB = i.y, p; char d = i.direction;
				int step = r % 4;
				for (p = 4; p < 8 && Direction[p] != d; p++);
				if ('L' == c)
					d = Direction[p + step];
				else if ('R' == c)
					d = Direction[p - step];
				else
				{
					Warehouse[posA][posB] = 1;								//'F'移动位置,恢复为有效位置 1
					while (r-- && (Warehouse[posA][posB] == 1))				//处于边界内且没有机器人的位置
					{														//即 Warehouse[i][j] = 1
						switch (d)
						{
						case 'N':posB++; break;
						case 'S':posB--; break;
						case 'W':posA--; break;
						case 'E':posA++; break;
						default:break;
						}
					}
					int temp = Warehouse[posA][posB];
					if (temp == 0)
					{
						flag = 1;
						robot_id = j ;
					}
					else if (temp > 1)
					{
						flag = 2;
						robot_id = j;
						crash_id = temp - 100;
					}
					else
						Warehouse[posA][posB] = j + 100;
				}
				i.direction = d, i.x = posA, i.y = posB;
			}
		}
		if (1 == flag)
			cout << "Robot " << robot_id << " crashes into the wall" << endl;
		else if (2 == flag)
			cout << "Robot " << robot_id << " crashes into robot " << crash_id << endl;
		else
			cout << "OK" << endl;
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值