Robot in Basement//bitset 模拟

2176: Robot in Basement
时间限制: 2 Sec  内存限制: 256 MB
提交: 13  解决: 8
[提交][状态][讨论版]
题目描述
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The Professor has lost his home robot yet again. After some thinking Professor understood that he had left the robot in the basement.


The basement in Professor's house is represented by a rectangle n×m, split into 1×1 squares. Some squares are walls which are impassable; other squares are passable. You can get from any passable square to any other passable square moving through edge-adjacent passable squares. One passable square is the exit from the basement. The robot is placed exactly in one passable square. Also the robot may be placed in the exit square.


Professor is scared of going to the dark basement looking for the robot at night. However, he has a basement plan and the robot's remote control. Using the remote, Professor can send signals to the robot to shift one square left, right, up or down. When the robot receives a signal, it moves in the required direction if the robot's neighboring square in the given direction is passable. Otherwise, the robot stays idle.


Professor wrote a sequence of k commands on a piece of paper. He thinks that the sequence can lead the robot out of the basement, wherever it's initial position might be. Professor programmed another robot to press the required buttons on the remote according to the notes on the piece of paper. Professor was just about to run the program and go to bed, when he had an epiphany.


Executing each command takes some energy and Professor doesn't want to get huge electricity bill at the end of the month. That's why he wants to find in the sequence he has written out the minimal possible prefix that would guarantee to lead the robot out to the exit after the prefix is fulfilled. And that's the problem Professor challenges you with at this late hour.


Input
The first line contains three integers n, m and k (3≤n,m≤150, 1≤k≤105). Next n lines contain m characters each − that is the Professor's basement's description: "#" stands for a wall, "." stands for a passable square and "E" stands for the exit from the basement (this square also is passable). It is possible to get from each passable square to the exit, all squares located by the n×m rectangle's perimeter are the walls. Exactly one square is the exit from the basement. The last line contains k characters, the description of the sequence of commands that Professor has written out on a piece of paper. "L", "R", "U", "D" stand for commands left, right, up and down correspondingly.


Output
Print in the output file the length of the smallest possible prefix that will lead the robot to the exit square. In other words, wherever the robot had been positioned initially, it should be positioned in the exit square after all the commands from the prefix are fulfilled (during doing commands the robot can come and leave the exit square, but only the last position of the robot is interesting for us). If Professor is mistaken and no prefix (including the whole sequence) can bring the robot to the exit, print "-1" (without the quotes). If there is the only passable square and it is the exit, print "0" (without the quotes).


Examples
Input
5 5 7
#####
#...#
#...#
#E..#
#####
UULLDDR


Output
6
Input
5 5 7
#####
#.#.#
#...#
#E..#
#####
UULLDDR
Output
-1
Input
5 3 2
###
#.#
#.#
#E#
###
DD
Output

2


题目意思:输入多组数据,第一行为n,m,k,之后输入n行字符串,每行字符串有m个字符,

‘#’代表墙,‘.’代表通路,‘E’代表出口;

再输入一组操作字符串,有k个操作字符。

题目要求:从通路的任何位置开始,找到一条最短的前缀路径,输出前缀路径的长度。


解题思路:用bitset模板模拟一下。

先介绍一下bitset

1、bitset是一个类似于vector的模板,头文件用#include

bitset  b;//n代表长度,
//且长度一定要提前确定。

2、bitset的相关函数

Robot in Basement//bitset 模拟_第1张图片

最后补充一下‘&’和‘|’是位运算,在二进制中表示‘与’和‘或’;

'<<'表示右移,‘>>’表示左移;

#include
using namespace std;
int n,m,k;
const int INF=100010;
char cc[220][220];
char cz[INF];
int bit_set() //模拟过程
{
	bitset  t,tp,w,e; //t表示通路,w表示墙,e表示出口,tp用来寻找。
	for(int i=0;i>m)&t)|(tp&(w<>1)&t)|(tp&(w<<1));
		else if(cz[i]=='D') tp=((tp<>m));
		else if(cz[i]=='R') tp=((tp<<1)&t)|(tp&(w>>1));
	}
	if(tp==e) return k; //最短前缀即为本身
	return -1; //找不到
}
int main(void)
{
	while(~scanf("%d%d%d",&n,&m,&k))  //输入多组数据
	{
		printf("%d\n",bit_set());
	}
	return 0;
}

以上内容参考:https://blog.csdn.net/jnxxhzz/article/details/80957430 的博文。

你可能感兴趣的:(zcmu)