topcoder SRM 680 T3 Friendly Robot(DP)

Problem Statement

You are programming a robot which lives on an infinite grid of empty cells. Each cell can be described by two integer coordinates (x, y). The robot always occupies a single cell of the grid. Initially, the robot is at coordinates (0, 0). A program has already been uploaded into the robot. The program is a sequence of instructions. Each instruction is one of the letters U, D, L, or R. These letters correspond to the commands ‘move up’, ‘move down’, ‘move left’, and ‘move right’ respectively. Once the robot is turned on, it will execute each instruction exactly once, in the given order. After executing all of the instructions, it will stop moving and it will turn itself off. You are given the robot’s current program in the string instructions. Whenever the robot returns to the cell (0, 0), it claps its hands. You find that funny, so you want to maximize the number of times the robot returns to the cell (0, 0). You are allowed to make at most changesAllowed modifications to the robot’s current program. Each modification consists of selecting a single character and changing it to a different character. (The new character still needs to be one of the letters U, D, L, or R. Note that you are not allowed to add or remove characters, you can only change the existing ones.) Please find and return the maximum number of times the robot can return to the cell (0, 0).
Definition

Class:
FriendlyRobot
Method:
findMaximumReturns
Parameters:
string, int
Returns:
int
Method signature:
int findMaximumReturns(string instructions, int changesAllowed)
(be sure your method is public)
Limits

Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256

Constraints

instructions will contain between 2 and 300 characters, inclusive.

Each character of instructions will be U, D, L, or R.

changesAllowed will be between 0 and the length of instructions, inclusive.
Examples
0)

“UULRRLLL”
1
Returns: 3
By changing the the first U to a D, you can make the robot return to its starting location 3 times. (Changing the second U to a D is also a valid solution.)
1)

“ULDR”
0
Returns: 1

2)

“ULDR”
2
Returns: 2

3)

“ULDRRLRUDUDLURLUDRUDL”
4
Returns: 8

4)

“LRLDRURDRDUDDDDRLLRUUDURURDRRDRULRDLLDDDDRLRRLLRRDDLRURLRULLLLLRRRDULRULULRLRDLLDDLLRDLUUDUURRULUDUDURULULLDRUDUUURRRURUULRLDLRRRDLLDLRDUULUURUDRURRLURLDLDDUUURRURRLRDLDDULLUDLUDULRDLDUURLUUUURRLRURRDLRRLLLRDRDUUUDRRRDLDRRUUDUDDUDDRLUDDULRURRDRUDLDLLLDLUDDRLURLDUDRUDDDDURLUUUDRLURDDDDLDDRDLUDDLDLURR”
47
Returns: 94

5)

“UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU”
300
Returns: 150

6)

“UD”
1
Returns: 1
Remember that you can change fewer than changesAllowed characters if you wish.

题意:

有一个机器人,从原点开始按照给定的操作移动,每次操作可以向上下左右中一个方向移动一格,现在可以改变k个操作,求机器人最多能经过原点几次。

思路:

1.第一次想到的是把左移看做-1,右移+1,下移-400,上移+400,因为总步数不超过400,可以保证一个值表示一段确定的移动路线。然后就将问题转化成求这个序列中修改k个值最多有多少个前缀和为0。然而后面我做不出来了

2.接下来想到DP,f[i][j] 表示前i步,改变了j次,回到了原点,最多有几次经过原点。注意回到了原点是很重要的条件,为了后面更新时不出错。也可以说i为奇数的根本不用考虑,因为无论如何修改都不会走回原点。最开始写DP的时候没有考虑到f[i][j]走回原点这个条件,导致错误。具体操作:先预处理出不修改,走了前i步到的坐标x[i]和y[i],转移方程为f [ i ] [ k ] = max ( f [ i ] [ k ] , f [ j ] [ k - ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) / 2 ) ] + 1 ) ( j < i && k <= changeAllowed && ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) %2 == 0 )

贴个代码,纪念一下我想了好久,调了半小时,不得不回寝室睡觉,又想了一晚上,早上起床才A掉的题目。然而jack表示这种题不是一下就A掉了吗

#include <bits/stdc++.h>
using namespace std;
const int N = 310;
int n, x[N], y[N];
int f[N][N];

class FriendlyRobot {
public:
    int findMaximumReturns( string instructions, int changesAllowed );
};
int FriendlyRobot::findMaximumReturns(string instructions, int changesAllowed) {
    n = instructions.size();
    x[0] = y[0] = 0;
    for (int i = 1; i <= n; i++){
        x[i] = x[i-1];
        y[i] = y[i-1];
        if (instructions[i-1] == 'U') x[i]++;
        else if (instructions[i-1] == 'D') x[i]--;
        else if (instructions[i-1] == 'L') y[i]--;
        else y[i]++;
    }
    int ans = 0;
    memset(f, -1, sizeof(f));
    f[0][0] = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < i; j++){
            int ope = abs(x[i]-x[j])+abs(y[i]-y[j]);
            if (ope&1) continue;
            else ope >>= 1;
            for (int k = ope; k <= changesAllowed; k++){
                if (f[j][k-ope] == -1) continue;
                f[i][k] = max(f[i][k], f[j][k-ope]+1);
                ans = max(ans, f[i][k]);
            }
        }
    return ans;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值