8B - Obsession with Robots

来源:互联网 发布:护眼有什么软件 编辑:程序博客网 时间:2024/06/07 13:10

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
LLUUUR
output
OK
input
RRUULLDD
output
BUG


题目给出一个机器人在某个迷宫中走的路线,问这个路线是否可能是起点到终点的最短路径


如果某路径是迷宫的最短路径

1,经过的位置最多只出现一次 (bfs求最短路的标记的原因)

2,刚走到某个位置时,这个位置只能由刚经过的位置到达(如果这个位置旁边的某位置早已经到达过,那么如果之前从那个位置走到这个位置,最终的路径肯定会更短)

然后分情况讨论即可


#include<cstdio>#include<cstring>#include<map>using namespace std;int gra[105*2][105*2];struct node{int a,b;}x[4]={{0,1},{0,-1},{1,0},{-1,0}};map<char,int> vis;bool judge(int a,int b)//判断是否有重复 {int cnt=0;for(int i=0;i<4;++i){int tx=a+x[i].a,ty=b+x[i].b;if(gra[tx][ty])//旁边走过的路只能有一个 {++cnt;if(cnt>1){return 1; } }}return 0;}bool slove(char s[]){int sx=105,sy=105,len=strlen(s);gra[sx][sy]=1;for(int i=0;i<len;++i){int tp=vis[s[i]];sx+=x[tp].a;sy+=x[tp].b;if(gra[sx][sy]||judge(sx,sy)){return 0;}gra[sx][sy]=1;}return 1;}int main(){vis['L']=0;vis['R']=1;vis['U']=2;vis['D']=3;char s[105]={0};scanf("%s",s);printf("%s\n",slove(s)?"OK":"BUG");return 0;}


0 0