B. Obsession with Robots

题目连接:

http://www.codeforces.com/contest/8/problem/B

Description

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).

Sample Input

LLUUUR

Sample Output

OK

Hint

题意

有一个迷宫,他不知道某些位置是障碍还是空地,但是会给你一个移动的指令

然后问你这个移动的指令是否走的是最短路

题解:

就只假设走过的地方是空地好了,最后再跑一个bfs

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
int M[maxn][maxn];
int T[maxn][maxn];
int vis[maxn][maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
string s;
int main()
{
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
M[i][j]=1;
cin>>s;
int nowx=maxn/2,nowy=maxn/2;
M[nowx][nowy]=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='L')nowy++;
if(s[i]=='U')nowx++;
if(s[i]=='D')nowx--;
if(s[i]=='R')nowy--;
M[nowx][nowy]=0;
}
int enx=nowx,eny=nowy;
nowx=maxn/2,nowy=maxn/2;
queue<pair<int,int> >Q;
Q.push(make_pair(nowx,nowy));
vis[nowx][nowy]=1;
while(!Q.empty())
{
pair<int,int>now = Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
pair<int,int>next=now;
next.first+=dx[i];
next.second+=dy[i];
if(vis[next.first][next.second])continue;
if(M[next.first][next.second])continue;
T[next.first][next.second]=T[now.first][now.second]+1;
vis[next.first][next.second]=1;
Q.push(next);
}
}
if(T[enx][eny]<s.size())cout<<"BUG"<<endl;
else cout<<"OK"<<endl;
}

Codeforces Beta Round #8 B. Obsession with Robots 暴力的更多相关文章

  1. Codeforces Beta Round #37 C. Old Berland Language 暴力 dfs

    C. Old Berland Language 题目连接: http://www.codeforces.com/contest/37/problem/C Description Berland sci ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  6. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

随机推荐

  1. 残差网络(Residual Network)

    一.背景 1)梯度消失问题 我们发现很深的网络层,由于参数初始化一般更靠近0,这样在训练的过程中更新浅层网络的参数时,很容易随着网络的深入而导致梯度消失,浅层的参数无法更新. 可以看到,假设现在需要更 ...

  2. Linux下帮助命令

    帮助命令(各种命令区别)   最常用的帮助命令   help --help help cd 查看内置命令的使用 info man   help cd 查看内置命令的使用   获得帮助的途径:   ma ...

  3. gunicorn之日志详细配置

    gunicorn的日志配置 gunicorn的日志配置相关的常用参数有4个,分别是accesslog,access_log_format,errorlog,loglevel. accesslog:用户 ...

  4. u-boot引导内核过程

    目标板:2440 u-boot引导内核启动时,传入内核的参数为bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0 一.nand re ...

  5. Groovy 与 DSL

    一:DSL 概念 指的是用于一个特定领域的语言(功能领域.业务领域).在这个给出的概念中有 3个重点: 只用于一个特定领域,而非所有通用领域,比如 Java / C++就是用于通用领域,而不可被称为 ...

  6. bitbucket SSH 生成

    在bitbucket设置ssh的方法: 1.运行ssh-keygen. 2.一路enter,直接到结束. 3 seeting 中Bitbucket选择.ssh/id_rsa.pub文件

  7. thinkphp5.1使用phpstudy隐藏index.php

    apache的重写规则如下: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine on R ...

  8. Tarojs+redux支付宝小程序开发攻略

    技术选型 对于习惯react语法的开发者来讲,RN是实现native的必备工具. 我们甚至可以屏蔽官方稳定而强大的配置层,直接上手开发. 而后,同为表层React语法的Rax.Taro这样的开源多端开 ...

  9. 关闭webstorm自动保存,并显示文件未保存标识

    1.取消自动保存 2.显示编辑状态设置:

  10. 七 oracle 表查询二

    1.使用逻辑操作符号问题:查询工资高于500或者是岗位为manager的雇员,同时还要满足他们的姓名首字母为大写的J?select * from emp where (sal > 500 or ...