【题目】
2176: Robot in Basement
Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 14 Solved: 9
[Submit][Status][Web Board]
Description
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.
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.
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).
5 5 7
#####
#...#
#...#
#E..#
#####
UULLDDR
6
5 5 7
#####
#.#.#
#...#
#E..#
#####
UULLDDR
-1
5 3 2
###
#.#
#.#
#E#
###
DD
2
【题解】
学到了一个新容器!!bitset!!!
基本操作
b.any() | b中是否存在置为1的二进制位? |
b.none() | b中不存在置为1的二进制位吗? |
b.count() | b中置为1的二进制位的个数 |
b.size() | b中二进制位的个数 |
b[pos] | 访问b中在pos处的二进制位 |
b.test(pos) | b中在pos处的二进制位是否为1? |
b.set() | 把b中所有二进制位都置为1 |
b.set(pos) | 把b中在pos处的二进制位置为1 |
b.reset() | 把b中所有二进制位都置为0 |
b.reset(pos) | 把b中在pos处的二进制位置为0 |
b.flip() | 把b中所有二进制位逐位取反 |
虽然我也不是很明白这道题为啥这样左移右移取或取与的....哎会用就行。
示例:
5 5 7
#####
# . . . #
# . . . #
#E . . #
#####
UULLDDR
t 00000 01110 01110 01110 00000
tp 00000 01110 01110 01110 00000
w 11111 10001 10001 10001 11111
e 00000 00000 00000 01000 00000
U
00000 01110 01110 01110 00000
01110 01110 01110 00000
00000 01110 01110 00000 00000
00000 01110 01110 01110 00000
00000 11111 10001 10001 10001 11111
00000 01110 00000 00000 00000
tp 00000 01110 01110 00000 00000
以此类推......
【代码】
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,m,k;
char b[maxn];
char a[160][160];
bitset <maxn> t,tp,w,e;
int f()
{
t.reset();
w.reset();
e.reset();
int i,j;
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
for(j=0;j<m;j++)
{
(a[i][j]=='#'?w:t).set(i*m+j);
(a[i][j]=='E'?e.set(i*m+j):0);
}
}
tp=t;
scanf("%s",b);
for(i=0;i<k;i++)
{
if(tp==e) return i;
if(b[i]=='U') tp=((tp>>m)&t|(w<<m)&tp);
else if(b[i]=='L') tp=((tp>>1)&t|(w<<1)&tp);
else if(b[i]=='D') tp=((tp<<m)&t|(w>>m&tp));
else tp=((tp<<1)&t|(w>>1)&tp);
}
if(tp==e) return k;
return -1;
}
main()
{
while(~scanf("%d%d%d",&n,&m,&k))
printf("%d\n",f());
}