Codeforces 606B Testing Robots 【模拟】

来源:互联网 发布:运维自动化软件 编辑:程序博客网 时间:2024/05/22 06:33

B. Testing Robots
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.

After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.

Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.

The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.

Input

The first line of the input contains four integers xyx0y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right.

The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.

Output

Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.

Sample test(s)
input
3 4 2 2UURDRDRL
output
1 1 0 1 1 1 1 0 6
input
2 2 2 2ULD
output
1 1 1 1
Note

In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: .


题意:给定一个n*m的地图(下标从1开始)。在位置(x, y)有一个机器人,现在给该机器人一些指令字符串str,L表示移动到左边的位置,R表移动到右边的位置,U表示移动到下面的位置,D表示移动到上面的位置。已知移动位置超过这个n*m的地图,机器人会在原地停留。机器人体内有一个code会使该机器人爆炸,引爆的条件有两个——机器人移动到有酒的位置或者执行完所有的指令。在n*m个位置上,我们每次选择一个位置(不能重复)并在该位置放上酒,把这作为一次测试。现在问你有多少组测试会使得机器人在执行完k个指令后爆炸(0<=k<=|str|)。假设机器人爆炸后会重置为初始状态。


思路:模拟,统计访问过的位置数cnt。

对于中间移动的位置若已经访问过,则输出0,反之输出1;

对于末尾,若该位置已经访问过,输出n*m-cnt,反之输出n*m-cnt+1。因为酒可以放在访问不到的位置,等到机器人执行完所有|str|个指令就会爆炸。



AC代码:

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (100000+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;char str[MAXN];bool vis[501][501];int n, m;bool judge(int x, int y){    return x >=1 && x <= n && y >= 1 && y <= m;}int main(){    int x, y;    Ri(n); Ri(m); Ri(x); Ri(y);    Rs(str);    int len = strlen(str);    CLR(vis, false); vis[x][y] = true;    int cnt = 1;    printf("1");    for(int i = 0; i < len; i++)    {        int xx, yy;        switch(str[i])        {            case 'U': xx = x-1; yy = y; break;            case 'D': xx = x+1; yy = y; break;            case 'L': xx = x; yy = y - 1; break;            case 'R': xx = x; yy = y + 1; break;        }        if(!judge(xx, yy))            xx = x, yy = y;        printf(" ");        if(vis[xx][yy])        {            if(i != len-1)                printf("0");            else                printf("%d", n*m-cnt);        }        else        {            cnt++;            vis[xx][yy] = true;            if(i != len-1)                printf("1");            else                printf("%d\n", n*m-cnt+1);        }        //printf(" %d %d\n", xx, yy);        x = xx; y = yy;    }    printf("\n");    return 0;}


0 0