CodeForces 97D. Robot in Basement
4 seconds
256 megabytes
standard input
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.
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大法好
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<bitset>
using namespace std;
bitset<>a,b,e,mp;
int id[][];
int n,m,k;
char s[];
void init(){
int res=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
id[i][j]=res++;
return;
}
int main(){
scanf("%d%d%d",&n,&m,&k);
int i,j;
init();
for(i=;i<=n;i++){
scanf("%s",s+);
for(j=;j<=m;j++){
if(s[j]=='#')mp[id[i][j]]=;
else a[id[i][j]]=;
if(s[j]=='E')e[id[i][j]]=;
}
}
scanf("%s",s+);
b=a;
for(i=;i;i++){
if(a==e){//全汇集到了出口
printf("%d\n",i-);break;
}
if(i>k){
printf("-1\n");break;
}
if(s[i]=='U') a=(((a>>m)&b) | ((mp<<m)&a) );
if(s[i]=='D') a=(((a<<m)&b) | ((mp>>m)&a) );
if(s[i]=='L') a=(((a>>)&b) | ((mp<<)&a) );
if(s[i]=='R') a=(((a<<)&b) | ((mp>>)&a) );
}
return ;
}
CodeForces 97D. Robot in Basement的更多相关文章
- Codeforces.97D.Robot in Basement(bitset 模拟)
题目链接 (ozr attack) 考虑怎么暴力,就是先在所有非障碍格子上全放上机器人,然后枚举每一步,枚举每个机器人移动这一步,直到所有机器人都在出口位置.复杂度是\(O(nmk)\)的. 怎么优化 ...
- Codeforces 626A Robot Sequence(模拟)
A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces 626A Robot Sequence
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces - 645D Robot Rapping Results Report(拓扑排序)
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some a ...
- Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...
- CodeForces - 922D Robot Vacuum Cleaner (贪心)
Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...
- CodeForces 645D Robot Rapping Results Report
二分,拓扑排序. 二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$0$的节点,那么不可行. #pragma comment(linker, "/STACK:102400 ...
- Codeforces 346D Robot Control(01BFS)
题意 有一个 \(N\) 个点, \(M\) 条边的有向图, 初始有一个机器人在 \(1\) 号点. 每个时刻, 这个机器人会随机选择一条从该点出发地边并通过.当机器人到达点 \(N\) 时, 它就会 ...
- cf97D. Robot in Basement(模拟 bitset)
题意 题目链接 Sol 接下来我的实现方式和论文里不太一样 然后用bitset优化,上下走分别对应着右移/左移m位,左右走对应着右移/左移1位 我们可以直接预处理出能走的格子和不能走的格子,每次走的时 ...
随机推荐
- java工作环境配置jdk,idea
下载 jdk 1.8 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 配置环境 ...
- 如何下载并安装 robomongo 到Ubuntu 系统
官网下载软件,https://robomongo.org/download wget https://download.robomongo.org/1.2.1/linux/robo3t-1.2.1-l ...
- Voyager的路由
修改默认的后台登录路由 打开web.php,把prefix值改为你想设置的值,如back: Route::group(['prefix' => 'back'], function () { Vo ...
- json转换为字典
str---dict ata_dict=json.loads(data)
- 19.Yii2.0框架模型删除记录
目录 //删除记录 //http://yii.com/?r=home/del public function actionDel() { //查出要删除的记录行 // 方法一:(查一行,删一行) // ...
- JSON Web Token(JWT)的详解
1.传统身份验证和JWT的身份验证 传统身份验证: HTTP 是一种没有状态的协议,也就是它并不知道是谁是访问应用.这里我们把用户看成是客户端,客户端使用用户名还有密码通过了身份验证,不过下回这个客户 ...
- QT入门学习笔记1:为什么要选QT及QT软件下载
为什么选择QT? Qt突出的优势: ◆ Qt 是基于 C++ 的一种语言扩展(Extention) C/C++ 目前还是一种很多人都在学习的语言. Qt的好处就在于Qt本身可以被称作是一种 C++ 的 ...
- SAS描述统计量
MEANS过程 MEAN过程默认输出的统计量有:观测总数.均值.标准差.最大值和最小值.如果要计算其他统计量或其中的某一些统计量,则可在PROC语句中指定统计量的关键字. BY语句规定了分组变量,要求 ...
- 一个通用的Makefile框架
先做一个简单的记录,后续有时间再慢慢完善补充细节. 先上一个整体图片: 其中,最重要的文件就是:program_template.mk. 下面是program_template.mk最重要的内容: $ ...
- 《百词斩·象形9000》第一册(下) 符号Symbol 1
001-version n.版本:译文 This is the first version of the software #这是软件开发的第一个版本: 002-element n.成分:要素:元素: ...