问题 F: GlitchBot

本文介绍了一个编程挑战,任务是纠正一个迷路机器人的指令集,使其能正确到达目标位置。机器人在执行指令过程中,会遇到一个错误的指令,导致无法到达预定目的地。文章提供了代码示例,展示了如何通过遍历和修改指令来找到并更正这个错误。

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

问题 F: GlitchBot
时间限制: 1 Sec 内存限制: 128 MB
提交: 223 解决: 109
[提交] [状态] [命题人:admin]
题目描述
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),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的。我们需要找出那个指令,并且改成正确的。

思路
因为数据范围比较小,我们可以把每条指令都改一下 暴力搜索一下,能不能走到目标点,如果改了,可以 那么 这就是答案 直接输出 break 掉就可以了


#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
using namespace std;

int step[200];
int x,y;
int n;
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};
string s;

int judge()
{
    int dir=0;
    int x1=0,y1=0;
    for(int i=0; i<n; i++)
    {
        if(step[i]==0)
        {

                x1+=dx[dir];
                y1+=dy[dir];

        }
        else if(step[i]==1){
            dir=(dir+3)%4;
        }
        else if(step[i]==2){
            dir=(dir+1)%4;
        }
    }
    return x1==x&&y1==y;
}

int main()
{
    cin>>x>>y>>n;
    for(int i=0; i<n; i++)
    {
        cin>>s;
        if(s[0]=='F')step[i]=0;
        else if(s[0]=='L')step[i]=1;
        else if(s[0]=='R')step[i]=2;
    }
    int flag=1;
    for(int i=0; i<n&&flag; i++)
    {
        for(int j=0; j<=2; j++)
        {
            step[i]=(step[i]+1)%3;
            if(judge())
            {
                printf("%d ",i+1);
                if(step[i]==0)cout<<"Forward"<<endl;
                else if(step[i]==1)cout<<"Left"<<endl;
                else if(step[i]==2)cout<<"Right"<<endl;
                flag=0;
                break;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值