#include\x20<stdio.h>\n#include\x20<stdlib.h>\n\n#define\x20MAX_TRAJECTORY_LENGTH\x20100\n\nstruct\x20Robot\x20{\n\x20\x20int\x20x;\n\x20\x20int\x20y;\n\x20\x20char\x20trajectory[MAX_TRAJECTORY_LENGTH];\n\x20\x20int\x20health;\n\x20\x20int\x20power;\n};\n\nvoid\x20Robot_move(struct\x20Robot*\x20robot,\x20char\x20direction);\nvoid\x20Path_print(const\x20struct\x20Robot*\x20robot);\nvoid\x20Obstacle_set(int*\x20map,\x20int\x20x,\x20int\x20y);\n\nint\x20main()\x20{\n\x20\x20int\x20power_limit;\n\x20\x20printf("请输入机器人功率上限设置:");\n\x20\x20scanf("%d",\x20&power_limit);\n\x20\x20\n\x20\x20int\x20map[6][6]\x20=\x20{0};\x20//\x20初始化二维空间\n\x20\x20int\x20obstacle_count;\n\x20\x20printf("请输入障碍物设置个数:");\n\x20\x20scanf("%d",\x20&obstacle_count);\n\x20\x20\n\x20\x20printf("请输入障碍物坐标:\n");\n\x20\x20for\x20(int\x20i\x20=\x200;\x20i\x20<\x20obstacle_count;\x20i++)\x20{\n\x20\x20\x20\x20int\x20x,\x20y;\n\x20\x20\x20\x20scanf("%d\x20%d",\x20&x,\x20&y);\n\x20\x20\x20\x20Obstacle_set((int*)map,\x20x,\x20y);\n\x20\x20}\n\x20\x20\n\x20\x20struct\x20Robot\x20robot\x20=\x20{0,\x200,\x20"",\x20100,\x2050};\x20//\x20初始化机器人\n\x20\x20int\x20move_count;\n\x20\x20printf("请输入机器人移动次数:");\n\x20\x20scanf("%d",\x20&move_count);\n\x20\x20\n\x20\x20printf("请输入机器人移动操作:\n");\n\x20\x20for\x20(int\x20i\x20=\x200;\x20i\x20<\x20move_count;\x20i++)\x20{\n\x20\x20\x20\x20char\x20direction;\n\x20\x20\x20\x20scanf("\x20%c",\x20&direction);\n\x20\x20\x20\x20Robot_move(&robot,\x20direction);\n\x20\x20}\n\x20\x20\n\x20\x20printf("机器人的移动轨迹:\n");\n\x20\x20Path_print(&robot);\n\x20\x20printf("机器人当前坐标:\n");\n\x20\x20printf("(%d,\x20%d)\n",\x20robot.x,\x20robot.y);\n\x20\x20printf("机器人当前血量:\n");\n\x20\x20printf("%d\n",\x20robot.health);\n\x20\x20printf("机器人当前功率:\n");\n\x20\x20printf("%d\n",\x20robot.power);\n\x20\x20\n\x20\x20return\x200;\n}\n\nvoid\x20Robot_move(struct\x20Robot*\x20robot,\x20char\x20direction)\x20{\n\x20\x20if\x20(robot->power\x20>\x200)\x20{\n\x20\x20\x20\x20int\x20new_x\x20=\x20robot->x;\n\x20\x20\x20\x20int\x20new_y\x20=\x20robot->y;\n\x20\x20\x20\x20char\x20move;\n\x20\x20\x20\x20\n\x20\x20\x20\x20switch\x20(direction)\x20{\n\x20\x20\x20\x20\x20\x20case\x20'W':\n\x20\x20\x20\x20\x20\x20\x20\x20if\x20(new_x\x20>\x200)\x20{\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20new_x--;\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20move\x20=\x20'W';\n\x20\x20\x20\x20\x20\x20\x20\x20}\n\x20\x20\x20\x20\x20\x20\x20\x20break;\n\x20\x20\x20\x20\x20\x20case\x20'S':\n\x20\x20\x20\x20\x20\x20\x20\x20if\x20(new_x\x20<\x205)\x20{\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20new_x++;\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20move\x20=\x20'S';\n\x20\x20\x20\x20\x20\x20\x20\x20}\n\x20\x20\x20\x20\x20\x20\x20\x20break;\n\x20\x20\x20\x20\x20\x20case\x20'A':\n\x20\x20\x20\x20\x20\x20\x20\x20if\x20(new_y\x20>\x200)\x20{\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20new_y--;\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20move\x20=\x20'A';\n\x20\x20\x20\x20\x20\x20\x20\x20}\n\x20\x20\x20\x20\x20\x20\x20\x20break;\n\x20\x20\x20\x20\x20\x20case\x20'D':\n\x20\x20\x20\x20\x20\x20\x20\x20if\x20(new_y\x20<\x205)\x20{\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20new_y++;\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20move\x20=\x20'D';\n\x20\x20\x20\x20\x20\x20\x20\x20}\n\x20\x20\x20\x20\x20\x20\x20\x20break;\n\x20\x20\x20\x20\x20\x20}\n\x20\x20\x20\x20\n\x20\x20\x20\x20if\x20(new_x\x20!=\x20robot->x\x20||\x20new_y\x20!=\x20robot->y)\x20{\n\x20\x20\x20\x20if\x20(((map\x20+\x20new_x)\x20+\x20new_y)\x20==\x200)\x20{\n\x20\x20\x20\x20\x20\x20robot->x\x20=\x20new_x;\n\x20\x20\x20\x20\x20\x20robot->y\x20=\x20new_y;\n\x20\x20\x20\x20\x20\x20robot->trajectory[strlen(robot->trajectory)]\x20=\x20move;\n\x20\x20\x20\x20\x20\x20robot->health\x20-=\x20(move\x20==\x20'W'\x20||\x20move\x20==\x20'S')\x20?\x208\x20:\x205;\n\x20\x20\x20\x20\x20\x20robot->power\x20-=\x2010;\n\x20\x20\x20\x20\x20\x20\n\x20\x20\x20\x20\x20\x20if\x20(robot->power\x20>\x20power_limit)\x20{\n\x20\x20\x20\x20\x20\x20\x20\x20robot->power\x20=\x200;\n\x20\x20\x20\x20\x20\x20\x20\x20printf("功率过高,机器人已停止移动!\n");\n\x20\x20\x20\x20\x20\x20}\n\x20\x20\x20\x20\x20}\n\x20\x20\x20\x20}\n\x20\x20}\n}\n\nvoid\x20Path_print(const\x20struct\x20Robot*\x20robot)\x20{\n\x20\x20for\x20(int\x20i\x20=\x200;\x20i\x20<\x20strlen(robot->trajectory);\x20i++)\x20{\n\x20\x20\x20\x20printf("%c",\x20robot->trajectory[i]);\n\x20\x20}\n\x20\x20printf("\n");\n}\n\nvoid\x20Obstacle_set(int*\x20map,\x20int\x20x,\x20int\x20y)\x20{\n\x20\x20*(*(map\x20+\x20x)\x20+\x20y)\x20=\x201;\n


原文地址: https://www.cveoy.top/t/topic/pwEs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录