F - GlitchBot VJ补题(模拟)

99 篇文章 1 订阅
33 篇文章 0 订阅

GlitchBot

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 9090-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)(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.

Input

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

Output

Identify how to correct the robot’s instructions by printing the line number (starting at 11) 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.

Sample Input 1 Sample Output 1
3 2
11
Forward
Right
Forward
Forward
Left
Forward
Forward
Left
Forward
Right
Forward
8 Right
Sample Input 2 Sample Output 2
-1 1
3
Right
Left
Forward
1 Forward

有点类似于2019年山东省省赛C题

#include<bits/stdc++.h>
using namespace std;
const int f = -1,l = 0,u = 1,r = 2,d = 3;
const int cx[] = {-1, 0, 1, 0};
const int cy[] = {0, 1, 0, -1};
int ex, ey, n;
struct Step
{
    int oper;
    int state;
    int x, y;
};
Step step[1005];
bool walk(int x, int y, int number, int state)
{
    for(int i = number; i < n; i++)
    {
        if(step[i].oper == f)
        {
            x += cx[state];
            y += cy[state];
        }
        else if(step[i].oper == r)
            state = (state + 1) % 4;
        else
            state = (state + 4 - 1) % 4;
    }
    return (x == ex && y == ey);
}

int main()
{
    string str;
    while(cin >> ex >> ey)
    {
        cin >> n;
        int x = 0, y = 0, state = u;
        for(int i = 0; i < n; i++)
        {
            cin >> str;

            step[i].x = x, step[i].y = y, step[i].state = state;
            if(str == "Forward")
            {
                step[i].oper = f;
                x += cx[state];
                y += cy[state];
            }
            else if(str == "Right")
            {
                step[i].oper = r;
                state = (state + 1) % 4;
            }
            else
            {
                step[i].oper = l;
                state = (state + 4 - 1) % 4;
            }
        }

        for(int i = 0; i < n; i++)
        {
            if(step[i].oper != f && walk(step[i].x + cx[step[i].state], step[i].y + cy[step[i].state], i + 1, step[i].state))
            {
                cout << i + 1 << " Forward" << endl;
                break;
            }
            if(step[i].oper != l && walk(step[i].x, step[i].y, i + 1, (step[i].state + 4 - 1) % 4))
            {
                cout << i + 1 << " Left" << endl;
                break;
            }
            if(step[i].oper != r && walk(step[i].x, step[i].y, i + 1, (step[i].state + 1) % 4))
            {
                cout << i + 1 << " Right" << endl;
                break;
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值