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

發佈了187 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章