bfs——Obsession with Robots

文章讲述了Berland的程序员Draude建造了一个机器人,但机器人有时不走最短路径。他需要确定是否存在一个地图,使得机器人记录的路径是起点到终点的最短路线。输入是机器人移动的记录,输出OK或BUG。
摘要由CSDN通过智能技术生成

Obsession with Robots

题面翻译

有一个机器人在一个无穷大的网格上,网格中有些格子机器人无法通过而剩下的可以。现在给出了他的路径,UDLR 分别表示往上下左右走 1 1 1 个单位长。请判断是否存在一个网格,使得机器人的这个路径合法,且为这个网格中起点到终点的最短路径。如果存在输出 OK,否则输出 BUG

输入字符串长度不超过 100 100 100

题目描述

The whole world got obsessed with robots,and to keep pace with the progress, great Berland’s programmer Draude decided to build his own robot. He was working hard at the robot. He taught it to walk the shortest path from one point to another, to record all its movements, but like in many Draude’s programs, there was a bug — the robot didn’t always walk the shortest path. Fortunately, the robot recorded its own movements correctly. Now Draude wants to find out when his robot functions wrong. Heh, if Draude only remembered the map of the field, where he tested the robot, he would easily say if the robot walked in the right direction or not. But the field map was lost never to be found, that’s why he asks you to find out if there exist at least one map, where the path recorded by the robot is the shortest.

The map is an infinite checkered field, where each square is either empty, or contains an obstruction. It is also known that the robot never tries to run into the obstruction. By the recorded robot’s movements find out if there exist at least one such map, that it is possible to choose for the robot a starting square (the starting square should be empty) such that when the robot moves from this square its movements coincide with the recorded ones (the robot doesn’t run into anything, moving along empty squares only), and the path from the starting square to the end one is the shortest.

In one movement the robot can move into the square (providing there are no obstrutions in this square) that has common sides with the square the robot is currently in.

输入格式

The first line of the input file contains the recording of the robot’s movements. This recording is a non-empty string, consisting of uppercase Latin letters L, R, U and D, standing for movements left, right, up and down respectively. The length of the string does not exceed 100.

输出格式

In the first line output the only word OK (if the above described map exists), or BUG (if such a map does not exist).

样例 #1

样例输入 #1

LLUUUR

样例输出 #1

OK

样例 #2

样例输入 #2

RRUULLDD

样例输出 #2

BUG

思路

本道题只需要满足:

  • 路不是重复走到之前已经走过的路上。
  • 坐标偏移(因为可能出现负的)

代码

//这道题我们就满足两个原则:坐标偏移、判断是否最短(也就是不是封闭曲线)

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 110;

bool st[N][N];
string a;
int n;
int x,y;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

bool check(int a,int b){
    if(st[a][b]) return true;//最短路如果走过那就不可能是最优的了
    
    int t=0;
    for(int i=0;i<4;i++){
        int xx=dx[i]+a,yy=dy[i]+b;
        if(st[xx][yy])t++;
    }    
    return t>1;
}

int main(){
    
    x=y=100;
    
    cin>>a;
    
    n=a.size();
    
    for(int i=0;i<n;i++){
        st[x][y]=true;
        if(a[i]=='L'){
            y--;
            if(check(x,y)){
                puts("BUG");
                return 0;
            }
        }else if(a[i]=='U'){
            x--;
            if(check(x,y)){
                puts("BUG");
                return 0;
            }
        }else if(a[i]=='D'){
            x++;
            if(check(x,y)){
                puts("BUG");
                return 0;
            }
        }else{
            y++;
            if(check(x,y)){
                puts("BUG");
                return 0;
            }
        }
    }
    
    puts("OK");
    
    return 0;
}
  • 18
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值