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位 我们可以直接预处理出能走的格子和不能走的格子,每次走的时 ...
随机推荐
- 谈谈TCP的四次挥手
“挥手”是为了终止连接,TCP四次挥手的流程图如下: (在socket编程中,可以由客户端或服务端进行close操作来进行) 下面的图是由客户端主动关闭连接 MSL是什么?最长报文段寿命 ------ ...
- 反射中 invoke方法 getMethod方法 getClass()方法
package com.swift; import java.util.*; import java.lang.reflect.*; public class ReflectDemo { public ...
- js函数节流和函数防抖
概念解释 函数节流: 频繁触发,但只在特定的时间内才执行一次代码 函数防抖: 频繁触发,但只在特定的时间内没有触发执行条件才执行一次代码 函数节流 函数节流应用的实际场景,多数在监听页面元素滚动事件的 ...
- 基础的Mapgis三维二次开发-插件式
最近在做一个杭州石油的项目开发一个小系统. 1.命令必须是 ICommand 的派生类 using System; using System.Collections.Generic; using Sy ...
- NOIP模拟赛 czy的后宫
[题目描述] czy要妥善安排他的后宫,他想在机房摆一群妹子,一共有n个位置排成一排,每个位置可以摆妹子也可以不摆妹子.有些类型妹子如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看了.假定每种 ...
- CentOS7下systemd
配置文件: /usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/ /run/systemd/system:系统执行过程中所产生的服务脚本, ...
- sphinx 快速使用
建立配置文件 例可以参照之前的模板新建一个配置文件 sphinx/etc目录 #MySQL数据源配置,详情请查看:http://www.coreseek.cn/products-install/mys ...
- json_encode() 避免转换中文
json_encode() 避免转换中文 我们都知道,json_encode()可以将数据转换为json格式,而且只针对utf8编码的数据有效,而且在转换中文的时候,将中文转换成不可读的”\u***” ...
- DAOMYSQLI工具类
<?php //DAOMySQLI.class.php //完成对mysql数据库操作,单例模式 //开发类 //1. 定类名 //2. 定成员属性 //3. 定成员方法[查询,dml操作] f ...
- #pragma与_Pragma(转载)
C90为预处理指令家族带来一位新成员:#pragma.一般情况下,大家很少见到它. #pragma的作用是为特定的编译器提供特定的编译指示,这些指示是具体针对某一种(或某一些)编译器的, ...