gpt4 book ai didi

c - 机器人路径模拟器

转载 作者:行者123 更新时间:2023-11-30 20:57:27 25 4
gpt4 key购买 nike

我对编程非常陌生,自从我们最近开始使用 C 以来,我们的任务是在假设的无限网格上创建一个程序(机器人模拟器),当然,只使用三种可能的 Action :右转,左转并前进。输出只需为 x 和 y 坐标 (x,y)。现在的问题是,我只能理解并完成所有四个 Action (上、下、左、右 - 北、南、​​西、东(假设没有左转或右转选项,左意味着移动 1)位置向左,向右意味着向右移动 1 个位置等))。因此,只需对它们进行简单的减法即可得到最终坐标,但我根本不知道如何为具有 2 回合选项和前进的坐标制定规则。我将保留我已经完成的任务的代码,因此如果有人认为我可以升级该代码,或者对如何使其工作有任何建议或想法,请告诉我。

#include<stdio.h> 
static void position(int n, char* string)
{
int left = 0, right = 0, up = 0, down = 0;
for (int i = 0; i < n; i++) {
if (string[i] == 'L')
left++;
else if (string[i] == 'R')
right++;
else if (string[i] == 'U')
up++;
else if (string[i] == 'D')
down++;
}
printf("(%d,%d)", (right - left), (up - down));
}
int main()
{
printf("Enter your movement: ");
char string[1000];
scanf("%s", string);
int n;
for (n = 0; string[n] != '\0'; ++n);
string[100] = string[n];
position(n, string);
}

最佳答案

解决这个问题的一种方法是使用笛卡尔坐标系。

enter image description here

我只是在处理过程中跟踪运动,而不是尝试在最后计算所有内容。

#include<stdio.h> 

enum {NORTH/*UP*/,SOUTH/*DOWN*/, EAST/*LEFT*/, WEST/*RIGHT*/};
static void position(int n, char* string)
{ // let's use a Cartesian coordinate system
// assume a 10x10 grid
int X=0,Y=0; // start at this place within the grid (the center)
int direction = NORTH; // we are facing NORTH
int i;
for (i = 0; i < n; i++) {
if (string[i] == 'L') // TURN LEFT, no step
{ // pivot
switch (direction)
{
case NORTH:
direction = EAST; // face EAST, but do not step
break;
case SOUTH:
direction = WEST; // face WEST, but do not step
break;
case EAST:
direction = SOUTH; // face SOUTH, but do not step
break;
case WEST:
direction = NORTH; // face NORTH, but do not step
break;
}
}
else if (string[i] == 'R') // TURN RIGHT, no step
{ // pivot
switch (direction)
{
case NORTH:
direction = WEST; // face WEST, but do not step
break;
case SOUTH:
direction = EAST; // face EAST, but do not step
break;
case EAST:
direction = NORTH; // face NORTH, but do not step
break;
case WEST:
direction = SOUTH; // face SOUTH, but do not step
break;
}
}
else if (string[i] == 'A')
{ // advance
switch (direction)
{
case NORTH:
if (Y == 5)
printf("you cannot go NORTH any more.\n");
else
Y++;
break;
case SOUTH:
if (Y == -5)
printf("you cannot go SOUTH any more.\n");
else
Y--;
break;
case EAST:
if (X == -5)
printf("you cannot go EAST any more.\n");
else
X--;
break;
case WEST:
if (X == 5)
printf("you cannot go WEST any more.\n");
else
X++;
break;
}
}
else
{
printf("%c is not a valid command.\n", string[i]);
}
}
printf("(%d,%d)", X, Y);
}
int main()
{
char string[1000];
int n;
for(;;)
{ // only valid movements are turn Right,turn Left and Advance. (RLA)
printf("Enter your movement: ");
scanf("%s", string);
for (n = 0; string[n] != '\0'; ++n);
string[100] = string[n];
position(n, string);
}
}

示例 I/O

输入您的 Action :@

@ 不是有效命令。

(0,0)

输入您的 Action :LLLA

(1,0)

输入您的 Action :LLLA

(1,0)

输入您的 Action :LLLAAA

(3,0)

输入您的 Action :LLARRRAA

(2,-1)

输入您的运动:LRLRLRA

(0,1)

关于c - 机器人路径模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com