-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdd_PlantTree.py
61 lines (51 loc) · 1.37 KB
/
pdd_PlantTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding:utf-8 -*-
N = int(input())
line = list(map(int, input().split()))
class_index = {}
M = 0
# 统计类别数目
for i, value in enumerate(line):
class_index[i+1] = value
M += value
# print(class_index)
# 初始状态
init_state = [1]
class_index[1] -= 1
# 临界值
class_index[N+1] = -1
while len(init_state) > 0:
# print(init_state)
# print(class_index)
# 存在合理方案
if len(init_state) == M:
break
# 从最小值开始添加
init_state.append(1)
class_index[1] -= 1
while True:
# 如果长度为1
if len(init_state) == 1:
if init_state[0] == N + 1:
init_state.pop()
break
# 满足条件
if class_index[init_state[-1]] >= 0 and init_state[-1] != init_state[-2]:
break
# 不满足条件时
# 没超过界限,更新
if init_state[-1] != N + 1:
class_index[init_state[-1]] += 1
init_state[-1] += 1
class_index[init_state[-1]] -= 1
# 超过界限,回溯
else:
class_index[init_state[-1]] += 1
init_state.pop()
class_index[init_state[-1]] += 1
init_state[-1] += 1
class_index[init_state[-1]] -= 1
# print(len(init_state))
if len(init_state) == M:
print(" ".join(map(str, init_state)))
else:
print("-")