Two Robots

https://www.hackerrank.com/challenges/two-robots/problem

思路:DP。用dp[i][j][k]表示执行到第i步,一个在j位置,一个在k位置,复杂度O(NNK),TLE

其实有一个robot一定停在当前query的结束位置,只需要枚举另外一个robot(可以是2个robot交替着来)的位置就好了,复杂度O(NK)

def twoRobots(m, queries):
    n=len(queries)
    dp=[[[float('inf') for _ in range(m+1)] for _ in range(m+1)] for _ in range(n+1)]
    dp[0]=[[0 for _ in range(m+1)] for _ in range(m+1)]
    for i in range(1,n+1):
        s,t=queries[i-1]
        for j in range(1,m+1):
            for k in range(1,m+1):
                dp[i][t][k]=min(dp[i][t][k], dp[i-1][j][k]+abs(j-s)+abs(s-t))
                dp[i][j][t]=min(dp[i][j][t], dp[i-1][j][k]+abs(k-s)+abs(s-t))
    return min(t for t in min(dp[n]))

print(twoRobots(10, [[2,4],[5,4],[9,8]]))
def twoRobots(m, queries):
    n=len(queries)
    dp=[[float('inf') for _ in range(m+1)] for _ in range(n)]
    dp[0]=[abs(queries[0][1]-queries[0][0]) for _ in range(m+1)]
    for i in range(n-1):
        _,t0=queries[i]
        s,t=queries[i+1]
        for j in range(1,m+1):
            dp[i+1][j]=min(dp[i+1][j], dp[i][j]+abs(t0-s)+abs(s-t))
            dp[i+1][t0]=min(dp[i+1][t0], dp[i][j]+abs(j-s)+abs(s-t))
    return min(dp[n-1])

print(twoRobots(10, [[2,4],[5,4],[9,8]]))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following: Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them. Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map. Input The first line contains an integer T (<= 11) which is the number of test cases. For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '', or 'o' which represent Wall, Grass, and Empty, respectively. Output For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值