poj 2632解题报告

来源:互联网 发布:php post在线测试 编辑:程序博客网 时间:2024/06/07 05:40

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



//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 directionif ( 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 directionif ( 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 directionif ( 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 directionif ( 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;}elsecout<<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;}


原创粉丝点击