360 散步

想了半小时, 最后没办法了, 暴力搜索,竟然过了, wdnmd


'''
@Author: rayenwang
@Date: 2019-08-31 17:45:00
@Description: 
'''


class Solution:
    def __init__(self, length):
        self.length = length
        self.end = set()

    def dfs(self, step, index, position):
        if position > self.length or position < 1:
            return
        if index == len(step):
            self.end.add(position)
            return
        self.dfs(step, index + 1, position + step[index])
        self.dfs(step, index + 1, position - step[index])


N, M = [int(n) for n in input().split()]
step = []
for _ in range(M):
    step.append(int(input()))
result = set()
for i in range(1, N + 1):
    s = Solution(N)
    s.dfs(step, 0, i)
    result = result | s.end
print(len(result))


#360公司##笔试题目#
全部评论
可以问一下题目是什么嘛
点赞 回复 分享
发布于 2019-08-31 19:34
两道题+起来不到三十行
点赞 回复 分享
发布于 2019-08-31 18:04

相关推荐

03-19 10:07
已编辑
广东药科大学 golang
Yki_:你倒是进一个面啊
点赞 评论 收藏
分享
评论
1
6
分享

创作者周榜

更多
牛客网
牛客企业服务