#727. CF8B Obsession with Robots

CF8B Obsession with Robots

题目描述

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.

题意翻译

有一个机器人在移动,要从某个点到另一个点,给定其在某一迷宫(不给出该迷宫)移动的序列,序列中只包含大写字母UDLR,问这条路径是否可能是最短的路径(您可以在迷宫任意位置放置一个障碍,有障碍的地方机器人不能到达),若是,输出 OK,否则输出 BUG

其中序列含义如下:

  • U 向上移动一格。
  • D 向下移动一格。
  • L 向左移动一格。
  • R 向右移动一格。

输入格式

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.

输入一个长度不超过100的特定字符串其中只包含大写字母UDLR

输出格式

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

如果输入的移动序列可能是最短的路径则输出OK,否则输出BUG

样例

样例输入1

LLUUUR

样例输出1

OK

样例输入2

RRUULLDD

样例输出2

BUG

样例解释

image

image

数据范围与提示

输入字符串长度不超过 100100