poj-2632的解题思路是什么?

更新于
2026-10-10 06:02:21
1阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计654个文字,预计阅读时间需要3分钟。

poj-2632的解题思路是什么?

伪原创以下开头内容,要求不雷同,不超过100字,直接输出结果:‘在这个信息爆炸的时代,如何从海量信息中筛选出有价值的内容,成为了一个亟待解决的问题。’


#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成员 存储方向, 在加减越界以后会回到起点或终点,

poj-2632的解题思路是什么?

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

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


本文共计654个文字,预计阅读时间需要3分钟。

poj-2632的解题思路是什么?

伪原创以下开头内容,要求不雷同,不超过100字,直接输出结果:‘在这个信息爆炸的时代,如何从海量信息中筛选出有价值的内容,成为了一个亟待解决的问题。’


#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成员 存储方向, 在加减越界以后会回到起点或终点,

poj-2632的解题思路是什么?

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

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