POJ 2623 Crashing Robots

#include <stdio.h>
#define MAX 101

int testCases;
int xLen, yLen;
int occupied[MAX][MAX];
int numOfRobots, numOfInstructions;
char initailDirection[2];
int startDirection[MAX];
int robot, repeat;
char action[2];
typedef struct Robot{
	int x;
	int y;
	int direction;
}Robot;
Robot RobotArray[MAX];
int directionArray[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

int main(){
	
	scanf("%d", &testCases);
	while (testCases--){
		scanf("%d%d", &xLen, &yLen);
		int i, j;
		for (i = 1; i <= xLen; i++)
			for (j = 1; j <= yLen; j++)
				occupied[i][j] = 0;

		scanf("%d%d", &numOfRobots, &numOfInstructions);

		for (i = 1; i <= numOfRobots; i++){
 			scanf("%d%d%s", &RobotArray[i].x, &RobotArray[i].y, &initailDirection);
			occupied[ RobotArray[i].x ][ RobotArray[i].y ] = i;
			switch(initailDirection[0]){
			case 'N':
				RobotArray[i].direction = 0;
				break;
			case 'E':
				RobotArray[i].direction = 1;
				break;
			case 'S':
				RobotArray[i].direction = 2;
				break;
			case 'W':
				RobotArray[i].direction = 3;
				break;
			}
		}

		int crash = 0;
		for (i = 1; i <= numOfInstructions; i++){
			scanf("%d%s%d", &robot, &action, &repeat);
			if (crash == 0){
				switch(action[0]){
				case 'L':
					//无论repeate多少次,一次旋转到位
					RobotArray[robot].direction = (RobotArray[robot].direction + 3 * repeat) % 4;
					break;
				case 'R':
					RobotArray[robot].direction  = (RobotArray[robot].direction + repeat) % 4;
					break;
				case 'F':
					occupied[ RobotArray[robot].x ][ RobotArray[robot].y ] = 0;
					//不能一次移动到位,要逐次移动并判断有无碰撞
					while (repeat--){
						RobotArray[robot].x += directionArray[ RobotArray[robot].direction ] [0];
						RobotArray[robot].y += directionArray[ RobotArray[robot].direction ] [1];
						if (RobotArray[robot].x < 1 || RobotArray[robot].x > xLen || RobotArray[robot].y < 1 || RobotArray[robot].y > yLen){
							crash = 1;
							printf("Robot %d crashes into the wall\n", robot);
							break;
						}
						if (occupied[ RobotArray[robot].x ][ RobotArray[robot].y ] != 0){
							crash = 1;
							printf("Robot %d crashes into robot %d\n", robot, occupied[ RobotArray[robot].x ][ RobotArray[robot].y ]);
							break;
						}
					}
					if (crash != 0)
						break;
					occupied[ RobotArray[robot].x ][ RobotArray[robot].y ] = robot;
				}
			}
		}

		if (crash == 0)
			printf("OK\n");

	}

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值