当前位置 : 主页 > 编程语言 > c语言 >

poj-2632

来源:互联网 收集:自由互联 发布时间:2023-09-03
#include stdio.h #include malloc.h struct Robot { int x; int y; char direction; }; struct Instruction { int robotId; char action; int repeatTime; }; char direction[4] = {'N', 'E', 'S', 'W'}; char rotate(char originalDirection, char action)


#include <stdio.h> 

 #include <malloc.h> 


 struct Robot { 

     int x; 

     int y; 

     char direction; 

 }; 


 struct Instruction { 

     int robotId; 

     char action; 

     int repeatTime; 

 }; 


 char direction[4] = {'N', 'E', 'S', 'W'}; 


 char rotate(char originalDirection, char action) { 

     char returnDirection = 0; 

     switch (originalDirection) { 

         case 'N': 

             if (action == 'L') { 

                 returnDirection = 'W'; 

             } else { 

                 returnDirection = 'E'; 

             } 

             break; 

         case 'E': 

             if (action == 'L') { 

                 returnDirection = 'N'; 

             } else { 

                 returnDirection = 'S'; 

             } 

             break; 

         case 'S': 

             if (action == 'L') { 

                 returnDirection = 'E'; 

             } else { 

                 returnDirection = 'W'; 

             } 

             break; 

         case 'W': 

             if (action == 'L') { 

                 returnDirection = 'S'; 

             } else { 

                 returnDirection = 'N'; 

             } 

             break; 

     } 

     return returnDirection; 

 } 


 void forwardRobot(Robot * robot) { 

     switch (robot->direction) { 

         case 'N': 

             robot->y += 1; 

             break; 

         case 'E': 

             robot->x += 1; 

             break; 

         case 'S': 

             robot->y -= 1; 

             break; 

         case 'W': 

             robot->x -= 1;     

             break; 

     } 

 } 


 void moveRobotAsInstruction(Robot * robot, char action) { 

     if (action == 'L' || action == 'R') { 

         robot->direction = rotate(robot->direction, action); 

     } else if (action == 'F') { 

         forwardRobot(robot); 

     } 

 } 


 bool isCrashed(Robot * robot, int robotId, int robotNum, int EWdiatance, int NSdistance) { 

     if ((robot + robotId)->x <= 0 || (robot + robotId)->y <= 0 || 

          (robot + robotId)->y > NSdistance || (robot + robotId)->x > EWdiatance) { 

         printf("Robot %d crashes into the wall\n", robotId + 1); 

         return true; 

     } 

     for (int i = 0; i < robotNum; i++) { 

         if (i == robotId) { 

             continue; 

         } 

         if ((robot + i)->x == (robot + robotId)->x && 

             (robot + i)->y == (robot + robotId)->y) { 

             printf("Robot %d crashes into robot %d\n", robotId + 1, i + 1); 

             return true; 

         } 

     } 

     return false; 

 } 


 void moveRobots(int EWdiatance, int NSdistance, int robotNum, int instructionNum, Robot * robots, Instruction * instructions) { 

     for (int i = 0; i < instructionNum; i++) { 

         for (int j = 0; j < instructions[i].repeatTime; j++) { 

             // printf("moveRobot %d %c\n", instructions[i].robotId - 1, instructions[i].action); 

             moveRobotAsInstruction(robots + instructions[i].robotId - 1, instructions[i].action); 

             if (instructions[i].action != 'L' && instructions[i].action != 'R') { 

                 if (isCrashed(robots, instructions[i].robotId - 1, robotNum, 

                     EWdiatance, NSdistance)) { 

                     return; 

                 } 

             } 

         } 

     } 

     printf("OK\n"); 

 } 


 int main() { 


     int caseNum = 0; 

     scanf("%d", &caseNum); 

     for (int i = 0; i < caseNum; i++) { 

         int EWdiatance; 

         int NSdistance; 

         scanf("%d %d", &EWdiatance, &NSdistance); 

         int robotNum; 

         int instructionNum; 

         scanf("%d %d", &robotNum, &instructionNum); 

         Robot * robots = (Robot *)malloc(robotNum * sizeof(Robot)); 

         Instruction * instructions = (Instruction *)malloc(instructionNum * sizeof(Instruction)); 

         for (int i = 0; i < robotNum; i++) { 

             scanf("%d %d %c", &(robots[i].x), &(robots[i].y), &(robots[i].direction)); 

         } 

         for (int i = 0; i < instructionNum; i++) { 

             scanf("%d %c %d", &(instructions[i].robotId), &(instructions[i].action), &(instructions[i].repeatTime)); 

             // printf("%d %c %d\n", instructions[i].robotId, instructions[i].action, instructions[i].repeatTime); 

         } 

         moveRobots(EWdiatance, NSdistance, robotNum, instructionNum, robots, instructions); 

         free(robots); 

         robots = NULL; 

         free(instructions); 

         instructions  = NULL; 

     } 
}

纯模拟, 考察细心和耐心。

有一个技巧就像是转向的处理(不过我懒, 没搞), 可以搞一个struct 2bit unsigned char成员 存储方向, 在加减越界以后会回到起点或终点,

和转向连转4次回到初始方向一样, 否则写swicth case都巨麻烦。

啥时候自己能做到 纸上写该题code, 一次AC, 就在细心上差不多了。


上一篇:poj-2593
下一篇:没有了
网友评论