Algorithm


B. Obsession with Robots
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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.

Input

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 LRU and D, standing for movements left, right, up and down respectively. The length of the string does not exceed 100.

Output

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

Examples
input
Copy
LLUUUR
output
Copy
OK
input
Copy
RRUULLDD
output
Copy
BUG

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <vector>

int main(){

    const long N = 100;
    const long D = 2 * N + 7;
    std::vector<std::vector<long> > vis(D, std::vector<long>(D, 0));

    long x(D / 2), y(D / 2);
    vis[x][y] = true;
    bool possible(true);

    std::string s; std::cin >> s;
    for(long p = 0; p < s.size(); p++){

        if(s[p] == 'U'){++y;}
        else if(s[p] == 'D'){--y;}
        else if(s[p] == 'R'){++x;}
        else if(s[p] == 'L'){--x;}

        if(vis[x][y]){possible = false; break;}
        else if(vis[x][y + 1] + vis[x][y - 1] + vis[x + 1][y] + vis[x - 1][y] > 1){possible = false; break;}

        vis[x][y] = true;
    }

    puts(possible ? "OK" : "BUG");

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
LLUUUR

Output

x
+
cmd
OK
Advertisements

Demonstration


Codeforces Solution-B. Obsession with Robots-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+