【思特奇杯·云上蓝桥-算法集训营】第2周

1.带分数

from itertools import permutations
num = int(input())
ans = 0
for n in ragne(1,num+1):
    if '0' in str(n) or len(set(list(n)))) != len(str(n)):
        continue
    diff = num -n
    s = '123456789'
    for i in str(n):
        s = s.replace(i,'')
    for length in range(1,len(s)//2+1):
        for i in permutations(s,length):
            fenzi = str(int(''.join(i))*diff)
            if len(fenzi) == len(s) - length:
                if set(list(fenzi)) == set(list(s)) - set(list(''.join(i))):
                    ans += 1
print(ans)

2.李白打酒

n = 0
def dfs(a,b,c):
    if a == 0 and b == 0 and c == 1:
        global n
        n +=1
        return
    if a > 0:
        dfs(a-1,b,c*2)
    if b > 0:
        dfs(a,b-1,c-1)
dfs(5,9,2)
print(n)

3.第39台阶

lt = [[-1]*39 for _ in range(39)]
def d(n,s):
    if n > 39:
        return 0
    elif n == 39:
        return 1 if s % 2 == 0 else 0
    if lt[n][s] != -1:
        return lt[n][s]
    else:
        ans = d(n+1, s+1) + d(n+2, s+1)
        lt[n][s] = ans
        return ans
print(d(0,0))

4.穿越雷区

n = int(input())
m = [input().split(' ') for _ in range(n)]
visit = [[False]*n for _ in range(n)]
step = [(0, -1), (0, 1), (-1, 0), (1, 0)]
queque = [(0, 0, 0)]
while queque:
    y, x, t = queque.pop(0)
    if m[y][x] == 'B':
        print(t)
        break
    for dy ,dx in step:
        ny = y + dy
        nx = x + dx
        if -1 < nx < n and -1 < ny < n:
            if not visit[ny][nx] and m[y][x] != m[ny][nx]:
                queque.append((ny,nx,t+1))
                visit[y][x] = True
if not queque:
    print(-1)

5.迷宫

m = '''01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000'''
 
 
 
m = m.split('\n')
visit = [[False]*50 for _ in range(30)]
visit[0][0] = True
step = [(1, 0, 'D'), (0, -1, 'L'), (0, 1, 'R'), (-1, 0, 'U')]
queque = [(0, 0, -1, 0)]
path = []
while queque:
    y, x, _, _ = cur = queque.pop(0)
    path.append(cur)
    if cur[:2] == (29, 49):
        temp = []
        i = len(path) - 1
        while i > 0:
            temp.append(path[i][3])
            i = path[i][2]
        temp.reverse()
        print(''.join(temp))
        break
    for dy, dx, dir in step:
        ny, nx = y+dy, x+dx
        if -1 < nx < 50 and -1 < ny < 30 and m[ny][nx] == '0' and not visit[ny][nx]:
            queque.append((ny, nx, len(path)-1, dir))
            visit[ny][nx] = True

6.跳马

a, b, c, d = map(int, input().split(' '))
step = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
visit = [[False]*8 for _ in range(8)]
queue = [(a, b, 0)] 
while queue:
    y, x, t = queue.pop(0)
    if y == c and x == d:
        print(t)
        break
    for dy, dx in step:
        ny = y + dy
        nx = x + dx
        if -1 < ny < 8 and -1 < nx < 8 and not visit[ny][nx]:
            queue.append((ny, nx, t+1))
            visit[ny][nx] = True
if not queue:
    print(-1)

7.路径之谜

n = int(input())
nums = [list(map(int, input().split(' '))) for _ in range(2)]
visit = [[False]*n for _ in range(n)]
visit[0][0] = True
ans = [0]
record = [[0]*n for _ in range(2)]
record[0][0] = record[1][0] = 1
step = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def dfs(y, x):
    if y == n-1 and x == n-1 and record == nums:
        print(' '.join(map(str, ans)))
        return
    for dy, dx in step:
        ny = y + dy
        nx = x + dx
        if -1 < ny < n and -1 < nx < n and not visit[ny][nx]:
            ans.append(ny*n + nx)
            visit[ny][nx] = True
            record[0][nx] += 1
            record[1][ny] += 1
            dfs(ny, nx)
            ans.pop()
            visit[ny][nx] = False
            record[0][nx] -= 1
            record[1][ny] -= 1
dfs(0, 0)

8.未名湖边的烦恼

m, n = map(int, input().split(' '))
record = [[-1]*(n+1) for _ in range(m+1)]
def dfs(a, b): 
    if a == b == 0:
        return 1
    if record[a][b] != -1:
        ans = record[a][b]
    else:
        ans = 0
        if a > 0:
            ans += dfs(a-1, b)
        if 0 < b <= a:
            ans += dfs(a, b-1)
        record[a][b] = ans
    return ans
print(dfs(m, n))

9.大臣的旅会

n = int(input())
lis = [list(map(int, input().split(' '))) for _ in range(n-1)]
m = {i: [] for i in range(1, n+1)}
for i in lis:
    m[i[0]].append(i[1:])
    m[i[1]].append((i[0], i[2]))
visit = [False] * (n+1)
node = max_length = 0
def dfs(x, length):
    global max_length
    if length > max_length:
        global node
        max_length, node = length, x
    for nx, l in m[x]:
        if not visit[nx]:
            visit[nx] = True
            dfs(nx, length+l)
            visit[nx] = False
dfs(1, 0)
visit = [False] * (n+1)
visit[node] = True
dfs(node, 0)
print(max_length * 11 + max_length*(max_length-1)//2)

10.皇后问题

def check(record, i, j):
    for k in range(i): 
        if j == record[k] or abs(record[k] - j) == abs(i - k):
            return False
    return True
def dfs(record, i, color):
    if i == n:
        if color == 'black':
            dfs([0]*n, 0, 'white')
        else:
            global ans
            ans += 1
        return
    for j in range(n):
        if m[i][j] == '1':
            if check(record, i, j):
                m[i][j] = '2'
                record[i] = j
                dfs(record, i+1, color)
                m[i][j] = '1'
n = int(input())
m = [input().split(' ') for _ in range(n)]
ans = 0
dfs([0]*n, 0, 'black')
print(ans)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值