问题 F: GlitchBot

本文介绍了一个编程挑战,目标是纠正一个偏离目标的机器人的指令集。机器人原本应该遵循一系列指令到达指定目的地,但由于上传过程中的错误,一条指令被错误地修改,导致机器人无法到达正确的位置。任务是通过更改错误的指令来修正路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a list of instructions in order to reach a target destination. The list of instructions is originally correct to get the robot to the target. However, something is going wrong as we upload the instructions into the robot’s memory. During the upload, one random instruction from the list takes on a different value than intended. Yes,there is always a single bad instruction in the robot’s memory and it always results in the robot arriving at an incorrect destination as it finishes executing the list of instructions.
The robot can execute the instructions “Left”, “Right”, and “Forward”. The “Left” and “Right” instructions do not result in spatial movement but result in a 90-degree turn in the corresponding direction. “Forward” is the only instruction that results in spatial movement, causing the robot to move one unit in the direction it is facing. The robot always starts at the origin (0, 0) of a grid and faces north along the positive y-axis.
Given the coordinates of the target destination and the list of instructions that the robot has in its memory, you are to identify a correction to the instructions to help the robot reach the proper destination.

输入
The first line of the input contains the x and y integer coordinates of the target destination, where −50 ≤ x ≤ 50 and −50 ≤ y ≤ 50. The following line contains an integer n representing the number of instructions in the list, where 1 ≤ n ≤ 50. The remaining n lines each contain a single instruction. These instructions may be: “Left”, “Forward”, or “Right”.

输出
Identify how to correct the robot’s instructions by printing the line number (starting at 1) of an incorrect input instruction, followed by an instruction substitution that would make the robot reach the target destination. If there are multiple ways to fix the instructions, report the fix that occurs for the earliest line number in the sequence of instructions. There is always exactly one unique earliest fix.

样例输入
3 2
11
Forward
Right
Forward
Forward
Left
Forward
Forward
Left
Forward
Right
Forward

样例输出
8 Right

这道题是说,起始位置在(0,0),面朝北。然后给你一个坐标,是指定的目的坐标,然后一个数代表有几步,但其中有一步是错误的,让你找出这个错误,这道题范围不大,所以用暴力解决。(我的代码很长,比较笨的方法。)

#include<stdio.h>
int cmp(char s[], int cc)
{
	int c = cc;
	int i = 1;
	int a = 0, b = 0;
	int f = 1;
	while (1)
	{
		if (i == cc+1) break;
		while (i<=cc)
		{
			if (f == 1)
			{
				if (s[i] == 'L')
				{
					f = 4;
					i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 3;
					i++;
					break;
				}
				if (s[i] == 'F')
				{
					b++; i++;
					break;
				}
			}
			if (f == 2)
			{
				if (s[i] == 'L')
				{
					f = 3; i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 4; i++;
					break;
				}
				if (s[i] == 'F')
				{
					b--; i++;
					break;
				}
			}
			if (f == 3)
			{
				if (s[i] == 'L')
				{
					f = 1; i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 2; i++;
					break;
				}
				if (s[i] == 'F')
				{
					a++; i++;
					break;
				}
			}
			if (f == 4)
			{
				if (s[i] == 'L')
				{
					f = 2; i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 1; i++;
					break;
				}
				if (s[i] == 'F')
				{
					a--; i++;
					break;
				}
			}
		}
	}
	return a;
}
int cmp1(char s[], int cc)
{
	int c = cc;
	int i = 1;
	int a = 0, b = 0;
	int f = 1;
	while (1)
	{
		if (i == cc + 1) break;
		while (i <= cc)
		{
			if (f == 1)
			{
				if (s[i] == 'L')
				{
					f = 4;
					i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 3;
					i++;
					break;
				}
				if (s[i] == 'F')
				{
					b++; i++;
					break;
				}
			}
			if (f == 2)
			{
				if (s[i] == 'L')
				{
					f = 3; i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 4; i++;
					break;
				}
				if (s[i] == 'F')
				{
					b--; i++;
					break;
				}
			}
			if (f == 3)
			{
				if (s[i] == 'L')
				{
					f = 1; i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 2; i++;
					break;
				}
				if (s[i] == 'F')
				{
					a++; i++;
					break;
				}
			}
			if (f == 4)
			{
				if (s[i] == 'L')
				{
					f = 2; i++;
					break;
				}
				if (s[i] == 'R')
				{
					f = 1; i++;
					break;
				}
				if (s[i] == 'F')
				{
					a--; i++;
					break;
				}
			}
		}	//printf("%d %d\n", a, b);
	}

	return b;
}
int main()
{
	int x, y, n;
	scanf("%d%d", &x, &y);
	scanf("%d", &n);
	int f = 1;
	char s[15], r[100];
	int a = 0, b = 0;
	int cnt = 1, cc = 0;
	int aa[100], bb[100];
	for (int i = 0; i < 100; i++) aa[i] = 0, bb[i] = 0;
	while (n--)
	{
		while (1)
		{
			cc++;
			scanf("%s", s);
			r[cc] = s[0];
			if (f == 1)
			{
				if (s[0] == 'L')
				{
					f = 4;
					break;
				}
				if (s[0] == 'R')
				{
					f = 3;
					break;
				}
				if (s[0] == 'F')
				{
					b++;
					break;
				}
			}
			if (f == 2)
			{
				if (s[0] == 'L')
				{
					f = 3;
					break;
				}
				if (s[0] == 'R')
				{
					f = 4;
					break;
				}
				if (s[0] == 'F')
				{
					b--;
					break;
				}
			}
			if (f == 3)
			{
				if (s[0] == 'L')
				{
					f = 1;
					break;
				}
				if (s[0] == 'R')
				{
					f = 2;
					break;
				}
				if (s[0] == 'F')
				{
					a++;
					break;
				}
			}
			if (f == 4)
			{
				if (s[0] == 'L')
				{
					f = 2;
					break;
				}
				if (s[0] == 'R')
				{
					f = 1;
					break;
				}
				if (s[0] == 'F')
				{
					a--;
					break;
				}
			}
		}
		//printf("%d %d\n", a, b);
	}
	//cmp1(r, cc);
	for (int i = 1; i <= cc; i++)
	{
		int w, e;
		if (r[i] == 'L')
		{
			r[i] = 'R';
			w = cmp(r, cc), e = cmp1(r, cc);
			if (w == x&&e == y)
			{
				printf("%d Right\n", i);
				break;
			}
			else r[i] = 'L';
			r[i] = 'F';
			w = cmp(r, cc), e = cmp1(r, cc);
			if (w == x&&e == y)
			{
				printf("%d Forward\n", i);
				break;
			}
			else r[i] = 'L';
		}
		if (r[i] == 'R')
		{
			r[i] = 'L';
			w = cmp(r, cc), e = cmp1(r, cc);
			if (w == x&&e == y)
			{
				printf("%d Left\n", i);
				break;
			}
			else r[i] = 'R';
			r[i] = 'F';
			w = cmp(r, cc), e = cmp1(r, cc);
			if (w == x&&e == y)
			{
				printf("%d Forward\n", i);
				break;
			}
			else r[i] = 'R';
		}
		if (r[i] == 'F')
		{
			r[i] = 'R';
			w = cmp(r, cc), e = cmp1(r, cc);
			if (w == x&&e == y)
			{
				printf("%d Right\n", i);
				break;
			}
			else r[i] = 'F';
			r[i] = 'L';
			w = cmp(r, cc), e = cmp1(r, cc);
			if (w == x&&e == y)
			{
				printf("%d Left\n", i);
				break;
			}
			else r[i] = 'F';
		}
	}
	return 0;
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值