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

你可能感兴趣的:(poj,robots,Crashing,2623)