Codeforces Beta Round #8 B. Obsession with Robots 暴力

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;
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5381220.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞