-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbolTriangle.py
More file actions
executable file
·45 lines (39 loc) · 1.02 KB
/
symbolTriangle.py
File metadata and controls
executable file
·45 lines (39 loc) · 1.02 KB
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
#!/usr/bin/python
#-*- coding:utf-8 -*-
__metaclass__ = type
class Triangle:
def __init__(self):
self.n = None #第一行的符号个数
self.half = None #n*(n+1)/4
self.count = None #当前'+'号个数
self.p = None #符号三角形矩阵
self.sum = None #已经找到的符号三角形数
def Backtrack(self, t):
if self.count > self.half or t*(t-1)/2-self.count > self.half: return
if t > self.n:
self.sum += 1
else:
for i in range(2):
self.p[1][t] = i
self.count += i
for j in range(2, t+1):
self.p[j][t-j+1] = self.p[j-1][t-j+1]^self.p[j-1][t-j+2]
self.count += self.p[j][t-j+1]
self.Backtrack(t+1)
for j in range(2, t+1):
self.count -= self.p[j][t-j+1]
self.count -= i
def Compute(n):
x = Triangle()
x.n = n
x.count = 0
x.sum = 0
x.half = n*(n+1)/2
if x.half%2 == 1:
return 0
x.half = x.half/2
x.p = [[0 for i in range(n+1)] for j in range(n+1)]
x.Backtrack(1)
return x.sum
if __name__ == "__main__":
print "Total:%s" % Compute(8)