zoukankan      html  css  js  c++  java
  • CodeForces 97D. Robot in Basement

    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    Input

    The first line contains three integers nm 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.

    Output

    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).

    Examples
    input
    5 5 7
    #####
    #...#
    #...#
    #E..#
    #####
    UULLDDR
    output
    6
    input
    5 5 7
    #####
    #.#.#
    #...#
    #E..#
    #####
    UULLDDR
    output
    -1
    input
    5 3 2
    ###
    #.#
    #.#
    #E#
    ###
    DD
    output
    2

    先在每格空地上放一个机器人,然后模拟指令,看什么时候它们能一起走到出口。

    直接模拟显然复杂度太高,可以用位运算优化。

    bitset大法好

     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<bitset>
     8 using namespace std;
     9 bitset<50010>a,b,e,mp;
    10 int id[160][160];
    11 int n,m,k;
    12 char s[100100];
    13 void init(){
    14     int res=0;
    15     for(int i=1;i<=n;i++)
    16         for(int j=1;j<=m;j++)
    17             id[i][j]=res++;
    18     return;
    19 }
    20 int main(){
    21     scanf("%d%d%d",&n,&m,&k);
    22     int i,j;
    23     init();
    24     for(i=1;i<=n;i++){
    25         scanf("%s",s+1);
    26         for(j=1;j<=m;j++){
    27             if(s[j]=='#')mp[id[i][j]]=1;
    28                 else a[id[i][j]]=1;
    29             if(s[j]=='E')e[id[i][j]]=1;
    30         }
    31     }
    32     scanf("%s",s+1);
    33     b=a;
    34     for(i=1;i;i++){
    35         if(a==e){//全汇集到了出口 
    36             printf("%d
    ",i-1);break;
    37         }
    38         if(i>k){
    39             printf("-1
    ");break;
    40         }
    41         if(s[i]=='U') a=(((a>>m)&b) | ((mp<<m)&a) );
    42         if(s[i]=='D') a=(((a<<m)&b) | ((mp>>m)&a) );
    43         if(s[i]=='L') a=(((a>>1)&b) | ((mp<<1)&a) );
    44         if(s[i]=='R') a=(((a<<1)&b) | ((mp>>1)&a) );
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    2020.1.15考试总结
    P4558 [JSOI2018]机器人 结论&DP
    2020.1.11考试总结
    2020.1.9考试总结
    如何和出题人斗智斗勇?奇技淫巧汇总
    各种公式总结
    2020.1.5考试总结
    C基础学习笔记——01-C基础第10天(内存结构)
    C基础学习笔记——01-C基础第09天(指针下)
    C基础学习笔记——01-C基础第08天(指针上)
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6338000.html
Copyright © 2011-2022 走看看