poj 2632解题报告

给定几个机器人的初始位置和朝向,根据给出的指令转向或前进,其间检测是否和其他机器人碰撞或者超出边界。根据计算结果打印即可。



//poj 2632
//304K 0MS 
#include <iostream>
#include <cstring>
using namespace std;


//机器人状态结构体
typedef struct stRobot
{
	int x;
	int y;
	char DirInput;  //输入时候的初始方向
	int direction;    //0为E 1为N  2为W 3为S方向
}stRobot, *LPstRobot;

void printInof(int **robotLoc,  int xLen, int yLen)
{
	for (int i = 0; i< xLen; i++)
	{
		for (int j = 0; j<yLen; j++)
		{
			cout<<robotLoc[i][j]<< " ";
		}
		cout<<endl;
	
	}
	cout<<endl;
}
int MoveFoward(int **robotLoc,  int xLen, int yLen,  LPstRobot robotInfo, int robotName)
{
	//printInof(robotLoc, xLen, yLen);
	int direction = robotInfo[robotName-1].direction;

	//E direction
	if ( direction == 0)
	{
		int x = robotInfo[robotName-1].x;
		int y = robotInfo[robotName-1].y;

		if ( x==xLen-1)
		{
			return -1;
		}
		else if (robotLoc[x+1][y] != 0)
		{
			return robotLoc[x+1][y];
		}

		else
		{
			robotLoc[x][y] = 0;
			robotLoc[x+1][y] = robotName;
			robotInfo[robotName-1].x = x+1;
			return 0;

		}
	}


	//N direction
	if ( direction == 1)
	{
		int x = robotInfo[robotName-1].x;
		int y = robotInfo[robotName-1].y;

		if ( y==yLen-1)
		{
			return -1;
		}
		else if (robotLoc[x][y+1] != 0)
		{
			return robotLoc[x][y+1];
		}

		else
		{
			robotLoc[x][y] = 0;
			robotLoc[x][y+1] = robotName;
			robotInfo[robotName-1].y = y+1;
			return 0;

		}
	}

	//W direction
	if ( direction == 2)
	{
		int x = robotInfo[robotName-1].x;
		int y = robotInfo[robotName-1].y;

		if ( x==0)
		{
			return -1;
		}
		else if (robotLoc[x-1][y] != 0)
		{
			return robotLoc[x-1][y];
		}

		else
		{
			robotLoc[x][y] = 0;
			robotLoc[x-1][y] = robotName;
			robotInfo[robotName-1].x = x-1;
			return 0;

		}
	}

	//S direction
	if ( direction ==3)
	{
		int x = robotInfo[robotName-1].x;
		int y = robotInfo[robotName-1].y;

		if ( y==0)
		{
			return -1;
		}
		else if (robotLoc[x][y-1] != 0)
		{
			return robotLoc[x][y-1];
		}

		else
		{
			robotLoc[x][y] = 0;
			robotLoc[x][y-1] = robotName;
			robotInfo[robotName-1].y = y-1;
			return 0;

		}
	}
	return 0;

}

int InsOneRun(int **robotLoc,  int xLen, int yLen,  LPstRobot robotInfo, int robotName, char ins, int repettimes )
{
	//成功返回0,撞墙返回-1,撞到其他机器人返回其编号
	int flag = 0;

	if (ins == 'R')
	{
		robotInfo[robotName-1].direction = (robotInfo[robotName-1].direction + 4*repettimes - repettimes) % 4; //像左转repeattimes后的方向
	}

	else if ( ins == 'L')
	{
		robotInfo[robotName-1].direction = (robotInfo[robotName-1].direction +  repettimes) % 4;
	}

	else if (ins == 'F')
	{
		for (int i = 0; i<repettimes; i++)
		{
			flag = MoveFoward(robotLoc, xLen, yLen, robotInfo, robotName);
			if (flag != 0)
			{
				//break;
				return flag;
			}
		}

	}
	return flag;
}

void OrderRun(int **robotLoc, int xLen, int yLen, int RobotNum, int InsNum, LPstRobot robotInfo)
{
	int RobotName = 0;
	char RobotAction ;
	int repeatTimes = 0;
	int result = 0;
	int flag = 0;
	char szResult[1024] = {0};

	for (int i =0 ; i< RobotNum; i++)
	{
		robotLoc[robotInfo[i].x ][robotInfo[i].y ] = i+1;   //将各个robotd的坐标映射到地图上
	}

	for (int i =0 ; i< InsNum; i++)
	{
		cin>>RobotName>>RobotAction>>repeatTimes;
		if (flag == 0)  //表明还没有出现问题
		{

			result = InsOneRun(robotLoc, xLen, yLen, robotInfo, RobotName, RobotAction, repeatTimes);
			if (flag == 0 && result!= 0)
			{
				flag = -1;
				if (result == -1)
				{
					sprintf(szResult, "Robot %d crashes into the wall",RobotName);
				}
				else if (result > 0)
				{
					sprintf(szResult, "Robot %d crashes into robot %d", RobotName, result);
				}

			}
		}
		

	}

	if (flag == 0)
	{
		cout<<"OK"<<endl;
	}
	else
		cout<<szResult<<endl;

}

int main()
{
	int k = 0;
	int A = 0, B = 0;
	int nRobNum = 0, nInstrNum = 0;
	int **pRobotLoc = NULL;

	cin>>k;
	for (int j = 0; j< k; j++)
	{
		cin>>A>>B;

		//申请一个二维数组来存放机器人的当前位置
		pRobotLoc = new int*[A];
		pRobotLoc[0] = new int[A*B];

		memset(pRobotLoc[0], 0, A*B*sizeof(int));

		for (int i= 1; i< A; i++)
		{
			pRobotLoc[i] = pRobotLoc[i-1] + B;
		}
		cin>>nRobNum>>nInstrNum;

		LPstRobot pRobotInfo = new stRobot[nRobNum];
		
		for (int i = 0; i< nRobNum; i++)
		{
			cin>>pRobotInfo[i].x>>pRobotInfo[i].y>>pRobotInfo[i].DirInput;
			switch (pRobotInfo[i].DirInput)
			{
			case 'E' :
				pRobotInfo[i].direction = 0;
				break;
			case 'N' :
				pRobotInfo[i].direction = 1;
				break;
			case 'W' :
				pRobotInfo[i].direction = 2;
				break;
			case 'S' :
				pRobotInfo[i].direction = 3;
				break;
			}
			pRobotInfo[i].x--;
			pRobotInfo[i].y--;
		}

		int nRobotName;
		char RobotAction;
		int repetTime;

		OrderRun(pRobotLoc, A, B, nRobNum, nInstrNum, pRobotInfo);
		delete pRobotInfo;
		delete pRobotLoc[0];
		delete pRobotLoc;
	}

	//system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1753目为"Flip Game",目给出了一个4x4的棋盘,每个格子有黑色或白色,每次翻转一个格子会同时翻转它上下左右四个格子的颜色,目标是把整个棋盘都变为同一种颜色,求把棋盘变成同种颜色的最小步数。 解题思路: 一般关于棋盘变色的目,可以考虑使用搜索来解决。对于POJ1753目,可以使用广度优先搜索(BFS)来解决。 首先,对于每个格子,定义一个状态,0表示当前格子是白色,1表示当前格子是黑色。 然后,我们可以把棋盘抽象成一个长度为16的二进制数,将所有格子的状态按照从左往右,从上往下的顺序排列,就可以用一个16位的二进制数表示整个棋盘的状态。例如,一个棋盘状态为: 0101 1010 0101 1010 则按照从左往右,从上往下的顺序把所有格子的状态连接起来,即可得到该棋盘的状态为"0101101001011010"。 接着,我们可以使用队列来实现广度优先搜索。首先将初始状态加入队列中,然后对于队列中的每一个状态,我们都尝试将棋盘上的每个格子翻转一次,生成一个新状态,将新状态加入队列中。对于每一个新状态,我们也需要记录它是从哪个状态翻转得到的,以便在得到最终状态时能够输出路径。 在搜索过程中,我们需要维护每个状态离初始状态的步数,即将该状态转换为最终状态需要的最小步数。如果我们找到了最终状态,就可以输出答案,即最小步数。 代码实现:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值