Skip to content

24677: 安全位置

http://cs101.openjudge.cn/practice/24677/

公元2200年,人类和外星人开始了一场宇宙大战,你作为百京大学的一名本科小盆友和外星人在一个四维空间展开了一场殊死搏斗。现在给出一串密码,希望你能从中解锁出所有的安全位置。具体来说,密码是一个字符串,你可以将其分为四个部分,每个部分依次代表四维空间中该维度的坐标。如果这四个坐标均在0到500之间(包含0和500)则是一个安全位置。注意坐标不能含有前导0,即001是不合法的坐标。

输入

输入只有一行,是一个字符串S,0<=len(S)<=30。

输出

输出共1行,是一个数字,代表从该密码中解锁出的安全位置的个数。

样例输入

010010

样例输出

2
# ['0.10.0.10', '0.100.1.0']
python
"""
GitHub Copilot Chat:
This solution works by recursively splitting the string into four parts and 
checking if each part is a valid coordinate. 
The safe_locations function takes the remaining string, the current parts, 
and the current depth as arguments. 
If the depth is 4, it checks if the string is empty and if all parts are 
valid coordinates. If so, it returns 1, otherwise it returns 0. 
If the depth is less than 4, it tries to split the string at every possible 
position and recursively calls itself with the new parts and increased depth. 
"""


def safe_locations(s, parts, depth=0):
    if depth == 4:
        if not s and all(0 <= int(part) <= 500 and 
                (part == '0' or not part.startswith('0')) for part in parts):
            return 1
        return 0
    return sum(safe_locations(s[i:], parts + [s[:i]], depth + 1) 
               for i in range(1, len(s) + 1))


s = input().strip()
print(safe_locations(s, []))