zoj4092.Robot Cleaner I(模拟水)

                                                Robot Cleaner I


Time Limit: 1 Second      Memory Limit: 65536 KB


Tired of tidying up his room, BaoBao decides to invent a robot cleaner to help him do the cleaning.

Let's consider BaoBao's room as a grid with  rows and  columns, where the cell in the -th row and the -th column is denoted as . Each cell belongs to one of the following three types:

  • Type 0: This cell is empty;
  • Type 1: This cell is a wall;
  • Type 2: This cell contains a piece of litter.

 

As we know, rooms are surrounded by walls. So it's guaranteed that for all , both  and  are walls; And for all , both  and  are walls.

After days of hard work, BaoBao successfully creates his very first robot cleaner. The robot is equipped with five sensors, four wheels, a robot arm, and a very simple controller.

The sensors can detect the type of the cell the robot is currently in, as well as the types of the four neighboring cells. That is to say, if the robot is in cell , it can know the types of five cells: , , ,  and .

The controller accepts a string  consisting of exactly 243 () characters as the program, where each character represents an instruction, and controls the robot according to the program and the values returned from the sensors. We now list the valid instructions below, and we assume that the robot is currently in cell .

  • U: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • D: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • L: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • R: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • P: If  contains a piece of litter, the robot will pick up the litter. Otherwise do nothing. Note that after picking up the litter,  becomes an empty cell;
  • I: Do nothing.

 

The controller works as follows. Note that we still assume that the robot is currently in cell , and we denote  as the type of cell .

  1. Calculate ;
  2. Read the -th character in  as the instruction and execute it. After that, go back to step 1.

 

Given the map of BaoBao's room, the program string and the starting position of the robot, please calculate the number of pieces of litter the robot can pick up after executing  instructions.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers ,  (, ), indicating the size of the room.

The second line contains three integers ,  and  (, , ), where  and  indicates that the robot starts from cell , and  indicates the number of instructions the robot executes.

The third line contains a string  (), indicating the program fed into the controller.

For the following  lines, the -th line contains a string  () consisting of '0', '1' and '2', where the -th character of  indicates the type of cell .

It's guaranteed that

  • For all , both  and  are walls; And for all , both  and  are walls;
  • Cell  is not a wall;
  • The sum of  of all test cases will not exceed .

 

Output

For each test case output one line containing one integer, indicating the number of pieces of litter the robot can pick up after executing  instructions.

Sample Input

2
5 5
2 4 6
RUPIRPIUDDLRUDRURLIIURDLPRDLDIRLIDPPRRRLLULPRPUPPDPRIUIUDLULIRIDIRPUPPIRRLRLULUPLRIIRLPRLLRLDLLPDRUUDLDPRRPLLPPUIUUPPLUIILLDRIDILDRRUPLPPLPDLDPDDUPIPPUIILIPLUPLURRPIIDDPPIUPRPRIRDRPPIUIRDUUUPPPDIIRPURIUIUIPLRILLDPPPURPPRRPDPRRLUDUDUDUPRLIUIRLR
11111
12021
10101
12021
11111
4 5
2 2 1000000000000000000
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
11111
10021
11211
11111

Sample Output

2
0

Author: WENG, Caizhi
Source: The 19th Zhejiang University Programming Contest Sponsored by TuSimple

 

一、原题地址

点我传送

 

二、大致题意

一个机器人打扫卫生,给出一张地图,

1表示围墙,0表示空地,2表示有一个垃圾。

现在有6种操作。

  • U: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • D: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • L: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • R: If  is not a wall, move the robot from  to . Otherwise do nothing;
  • P: If  contains a piece of litter, the robot will pick up the litter. Otherwise do nothing. Note that after picking up the litter,  becomes an empty cell;
  • I: Do nothing.

英文很好懂了。

询问K步操作以后能清扫多少垃圾。

 

三、大致思路

显然就是一个简单的模拟题,问题在于机器人将可能执行10^18次操作,每一步都跑完显然是不可能的。

那么如何中途就退出循环去掉那些无用的步骤呢。

用一个vis[ i ][ j ][ 0 ] 记录机器人在(i,j)这个点执行过的操作p,

用vis[ i ][ j ][ 1 ]记录机器人在(i,j)这个点已经清扫了多少垃圾。

那么如果机器人在第 x 次经过了(i,j)点之后,记录下了p 和ans ,下一次机器人再到达这个点,发现仍然执行的是p操作,并且ans仍然和上一次记录的一样,说明此时机器人出去逛了一圈,但是无功而返了。   此时推出循环。

 

四、代码

#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<iostream>
#include<deque>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;


int T;
int n,m;
int nx,ny,K,ans;
char s[500];
char mmp[2005][2005];
int vis[2005][2005][2];
void read()
{
    scanf("%d %d",&n,&m);
    scanf("%d %d %d",&nx,&ny,&K);
    scanf("%s",s+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",mmp[i]+1);
        for(int j=1;j<=m;j++)
        {
            vis[i][j][0]=vis[i][j][1]=-1;
        }
    }
}

int getp(int i,int j)
{
    return (mmp[i][j]-'0')*3*3*3*3+(mmp[i-1][j]-'0')*3*3*3+(mmp[i+1][j]-'0')*3*3+(mmp[i][j-1]-'0')*3+(mmp[i][j+1]-'0');
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        read();
        ans=0;
        while(K--)
        {
            int p=getp(nx,ny);
            if(vis[nx][ny][0]==p&&vis[nx][ny][1]==ans)break;
            vis[nx][ny][0]=p;
            vis[nx][ny][1]=ans;
            char op=s[p+1];
            if(op=='U')
            {
                if(mmp[nx-1][ny]!='1')nx--;
            }
            else if(op=='D')
            {
                if(mmp[nx+1][ny]!='1')nx++;
            }
            else if(op=='L')
            {
                if(mmp[nx][ny-1]!='1')ny--;
            }
            else if(op=='R')
            {
                if(mmp[nx][ny+1]!='1')ny++;
            }
            else if(op=='P')
            {
                if(mmp[nx][ny]=='2')
                {
                    ans++;mmp[nx][ny]='0';
                }
            }
            else if(op=='I')break;
        }
        printf("%d\n",ans);
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值