CF8B Obsession with Robots
题目描述
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.
输入输出格式
输入格式:
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.
输出格式:
In the first line output the only word OK (if the above described map exists), or BUG (if such a map does not exist).
输入输出样例
输入样例#1: 复制
LLUUUR
输出样例#1: 复制
OK
输入样例#2: 复制
RRUULLDD
输出样例#2: 复制
BUG
讲一下思路:如果认定一条路线为最短路,那么路径中任意一点不会有第二条路到达,也就是说假如x,y是这个路径上的一点,那么围绕x,y的四个点中,只有上次走过来的点被走过,其他的的三个点不能被走,这也是BFS最短路的核心思想,可以在纸上模拟一下;
这个题源自于别的OJ网,不知道为什么一直编译失败,还有exit(0)是退出整个程序的意思,不是仅仅只退出递归(被这个卡了好久)。数组一定要开两倍,并从map[100][100]开始模拟,也就是地图的中心那个点,因为原题可以往左走。
#include<bits/stdc++.h>
using namespace std;
struct point{int x;int y;};
bool Map[205][205],flag=true;
string road;
bool juge(int x,int y)
{
int sum=0;
if(Map[x-1][y]) sum++;
if(Map[x+1][y]) sum++;
if(Map[x][y-1]) sum++;
if(Map[x][y+1]) sum++;
if(sum>1) return false;
else return true;
}
void move(int x,int y,char t)
{
if(t==road.length()) return ;
Map[x][y]=true;
if(juge(x,y)==false)
{
flag=false;
return ;
}
if(road[t]=='U') move(x-1,y,t+1);
else if(road[t]=='D') move(x+1,y,t+1);
else if(road[t]=='R') move(x,y+1,t+1);
else move(x,y-1,t+1);
}
int main()
{
cin>>road;
move(102,102,0);
if(flag) cout<<"OK"<<endl;
else cout<<"BUG"<<endl;
return 0;
}