Kattis - glitchbot —F - GlitchBot (暴力)

链接:

https://cn.vjudge.net/problem/1148484/origin

题意:

机器人的某一步出错,导致到达不了指定位置,找出哪一步,并改正。

思路:

遍历即可,第一个答案输出

代码:

#include <bits/stdc++.h>
using namespace std;
int step[200];
int dx[]  = {0 , 1 , 0 , -1};//上右下左顺序固定
int dy[]  = {1 , 0 , -1 , 0};
int x_0,y_0,n;
bool judge()
{
    int direction = 0;
    int x = 0,y = 0;
    for(int  i = 0 ; i < n; i++)
    {
        if(step[i] == 0)
        {
            x = x + dx[direction];
            y = y + dy[direction];
        }
        else if(step[i] == 1) direction = (direction + 3) % 4;
        else if(step[i] == 2) direction = (direction + 1) % 4;
     }
     return (x == x_0 && y == y_0);
}
int main()
{
    cin>>x_0>>y_0>>n;
    string s;
    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;
    }
    bool 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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值