B. Obsession with Robots

Codeforces
IDCF8B
Time2000ms
Memory64MB
Difficulty
constructive algorithmsgraphsimplementation
English · Original
Chinese · Translation
Formal · Original
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 _L_, _R_, _U_ 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). [samples]
整个世界都沉迷于机器人,为了跟上科技进步,伟大的贝兰德程序员Draude决定建造自己的机器人。他为机器人倾注了大量心血,教会它从一个点到另一个点走最短路径,并记录下所有移动过程。但正如Draude的许多程序一样,这里也存在一个bug——机器人并不总是走最短路径。幸运的是,机器人正确地记录了它自己的移动轨迹。现在Draude想弄清楚他的机器人何时运行错误。如果Draude还记得测试机器人时所用场地的地图,他就能轻易判断机器人是否朝正确的方向移动。但场地地图已经丢失,再也找不到了,因此他请求你判断是否存在至少一张地图,使得机器人记录的路径是最短路径。 地图是一个无限的棋盘,每个方格要么为空,要么包含障碍物。已知机器人从不会试图撞向障碍物。根据机器人记录的移动轨迹,判断是否存在至少一张这样的地图:可以为机器人选择一个起始方格(起始方格必须为空),使得机器人从该方格出发时,其移动轨迹与记录完全一致(机器人仅沿空方格移动,不会撞到任何障碍物),且从起始方格到终点的路径是最短的。 在一次移动中,机器人可以移动到与其当前所在方格有公共边的方格(前提是该方格没有障碍物)。 输入文件的第一行包含机器人移动的记录。该记录是一个非空字符串,由大写拉丁字母 _L_、_R_、_U_ 和 _D_ 组成,分别代表向左、向右、向上和向下移动。字符串长度不超过 _100_。 在第一行输出唯一的单词 _OK_(如果存在上述描述的地图),或 _BUG_(如果这样的地图不存在)。 ## Input 输入文件的第一行包含机器人移动的记录。该记录是一个非空字符串,由大写拉丁字母 _L_、_R_、_U_ 和 _D_ 组成,分别代表向左、向右、向上和向下移动。字符串长度不超过 _100_。 ## Output 在第一行输出唯一的单词 _OK_(如果存在上述描述的地图),或 _BUG_(如果这样的地图不存在)。 [samples]
### Definitions 1. **Grid Space:** Let the world be the infinite integer lattice $\mathbb{Z}^2$. 2. **Move Set:** Let $\Delta = \{L, R, U, D\}$ be the set of possible move indicators, mapped to vector displacements in $\mathbb{Z}^2$ as follows: $$ \vec{L} = (-1, 0), \quad \vec{R} = (1, 0), \quad \vec{U} = (0, 1), \quad \vec{D} = (0, -1) $$ 3. **Input:** Let $S = (s_1, s_2, \dots, s_n)$ be a sequence of length $n$ where $s_i \in \Delta$ for all $i \in \{1, \dots, n\}$. 4. **Trajectory:** Let $P = (v_0, v_1, \dots, v_n)$ be the sequence of coordinates visited by the robot, defined recursively: $$ v_0 = (0, 0) $$ $$ v_i = v_{i-1} + \vec{s_i} \quad \text{for } 1 \le i \le n $$ 5. **Visited Set:** Let $V_P = \{v_0, v_1, \dots, v_n\} \subseteq \mathbb{Z}^2$ be the set of all distinct cells visited during the trajectory. 6. **Induced Distance:** For any subset of cells $M \subseteq \mathbb{Z}^2$, let $G_M$ be the graph where vertices are elements of $M$ and edges exist between any $u, v \in M$ if and only if $\|u - v\|_1 = 1$ (Manhattan distance is 1). Let $d_M(start, end)$ denote the shortest path distance (number of edges) between vertices $start$ and $end$ in the graph $G_M$. ### Objective Determine the truth value of the following existence statement: $$ \exists M \subseteq \mathbb{Z}^2 \quad \text{such that} \quad \begin{cases} V_P \subseteq M \\ d_M(v_0, v_n) = n \end{cases} $$ ### Output * If the statement is **True**, output: `OK` * If the statement is **False**, output: `BUG`
Samples
Input #1
LLUUUR
Output #1
OK
Input #2
RRUULLDD
Output #2
BUG
API Response (JSON)
{
  "problem": {
    "name": "B. Obsession with Robots",
    "description": {
      "content": "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 th",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 65536
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF8B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 th...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "整个世界都沉迷于机器人,为了跟上科技进步,伟大的贝兰德程序员Draude决定建造自己的机器人。他为机器人倾注了大量心血,教会它从一个点到另一个点走最短路径,并记录下所有移动过程。但正如Draude的许多程序一样,这里也存在一个bug——机器人并不总是走最短路径。幸运的是,机器人正确地记录了它自己的移动轨迹。现在Draude想弄清楚他的机器人何时运行错误。如果Draude还记得测试机器人时所用场地的...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "### Definitions\n\n1.  **Grid Space:** Let the world be the infinite integer lattice $\\mathbb{Z}^2$.\n2.  **Move Set:** Let $\\Delta = \\{L, R, U, D\\}$ be the set of possible move indicators, mapped to vec...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments