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位 我们可以直接预处理出能走的格子和不能走的格子,每次走的时 ...
随机推荐
- 标签中的name属性和ID属性的区别
标签中的name属性和ID属性的区别 2018年05月13日 10:17:44 tssit 阅读数:760 编程这么久,细想了一下,发现这个问题还不是很清楚,汗!看了几篇文章,整理了一下,分享下! ...
- java基础——Map集合
Map以键值对的形式存储数据,其中Map.entry,是Map的内部类,它用来描述Map中的键值对.Map是一个接口,HashMap是他的一个实现类 Map中有几个重要的方法: get(Object ...
- An Intuitive Explanation of Convolutional Neural Networks
https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ An Intuitive Explanation of Convolu ...
- 【线段树 树链剖分 差分 经典技巧】loj#3046. 「ZJOI2019」语言【未完】
还是来致敬一下那过往吧 题目分析 先丢代码 #include<bits/stdc++.h> ; ; ; struct node { int top,son,fa,tot; }a[maxn] ...
- 03等待多个线程返回WaitForMultipleObject
二. WaitForMultipleObject 等待单个线程返回 1. 函数原型 DWORD WINAPI WaitForMultipleObjects( _In_ DWORD nCount, _I ...
- build_mem_type_table
该函数设置mem_types结构体数组,结构体定义如下: struct mem_type { unsigned int prot_pte; //二级页表属性 unsigned int prot ...
- 并查集:CDOJ1594-老司机的奇幻漂流 (食物链)
老司机的奇幻漂流 UESTC - 1594 Problem Description 老司机在救出了女票之后,就和她在全世界旅游,有一天,他们来到了一个神奇的小岛上. 这个小岛上有三种动物,他们互相克制 ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- LINQ to SQL和Entity Framework对比与关联
LINQ to SQL和Entity Framework都是一种包含LINQ功能的对象关系映射技术.他们之间的本质区别在于EF对数据库架构和我们查询的类型实行了更好的解耦.使用EF,我们查询的对象不再 ...
- webdriver高级应用- 禁止Chrome浏览器的PDF和Flash插件
#encoding=utf-8 from selenium import webdriver # 导入Options类 from selenium.webdriver.chrome.options i ...