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基础——Map集合
Map以键值对的形式存储数据,其中Map.entry,是Map的内部类,它用来描述Map中的键值对.Map是一个接口,HashMap是他的一个实现类 Map中有几个重要的方法: get(Object ...
- word中在空白处加下划线不显示解决
终极解决:Ctrl + Shift + Space Alt + 选择,竖向选择.和VS,其他一些编辑器一样
- C#算术运算符
一.C#算术运算符 C#语言的算术运算符主要用于数学计算中. 二.示例 using System;using System.Collections.Generic;using System.Linq; ...
- 采用maven 对tomcat 进行自动部署
在工作过程中经常会遇到项目频繁发不到额过程,而且在这个过程中会一直进行一些简单但是繁琐的重复性工程 1.打war 包 2.停掉tomcat 3.copy war 包 4.启动tomcat 听说mave ...
- 使用xcode 8 调试ios10
这几天更新了ios10,发现真机不能调试,弹出几个错,表示没有证书.用ios9的真机能调试, 真他么坑,总结一下解决方法. 在BuildSetting 的Signing中Code Signing Id ...
- NSString 使用 copy、strong
// 首先定义2个属性 @property (nonatomic, strong) NSString *stStr; @property (nonatomic, copy) NSString *coS ...
- Win10家庭版找不到组策略gpedit.msc
首先在桌面上建立一个txt文本文件,将下面的代码复制到里面 @echo off pushd "%~dp0" dir /b C:\Windows\servicing\Packages ...
- java实现可安装的exe程序
java实现可安装的exe程序 通过编写Java代码,实现可安装的exe文件的一般思路: 1.在eclipse中创建java项目,然后编写Java代码,将编写好的Java项目导出一个.jar格式的ja ...
- Nordic Collegiate Programming Contest 2015 D. Disastrous Downtime
You're investigating what happened when one of your computer systems recently broke down. So far you ...
- oracle结构-内存结构与动态内存管理
内存结构与动态内存管理 内存是影响数据库性能的重要因素. oracle8i使用静态内存管理,即,SGA内是预先在参数中配置好的,数据库启动时就按这些配置来进行内在分配,oracle10g引入了动态内存 ...