ref: https://blog.csdn.net/m0_37966618/article/details/80374581
1"""2ID: ***3LANG: PYTHON34TASK: wormhole5"""6fin = open ('wormhole.in', 'r')7fout = open ('wormhole.out', 'w')8N = int(fin.readline())9total_List = list()10def checkPairings(listGenrt,crd):11 for i in range(len(crd)):#assign starting point12 sPt = crd[i]13 lG = list(listGenrt)14 org = list(sPt)15 nXt = []54 collapsed lines
16 num = 017 while(True):#checking solutions of this starting point18 for tpl in listGenrt:19 if sPt in tpl:20 sPt = tpl[0 if tpl.index(sPt) == 1 else 1]21 if tpl in lG:22 lG.remove(tpl)23
24 for x in crd:25 if x[1] == sPt[1] and x != sPt and x[0] > sPt[0]:26 if nXt==[] or (nXt!=[] and x[0] < nXt[0]):27 nXt = x28
29 if(nXt==[]):#can't find wh with the same Y value30 break31 sPt = list(nXt)32 nXt = []33 if sPt == org and len(lG)!=len(listGenrt):34 num+=135 break36
37 if num!=0:38 total_List.append(listGenrt)39 break40
41def createPairings(remaining):42 if len(remaining)==0:43 return44
45 for x in range(1,len(remaining)):46 if(x>1):47 P2 = list(pairings)48 #print("---{}".format(P2))49 checkPairings(P2,crd)50
51 del pairings[(len(P2)-int(len(remaining)/2)):len(P2)]52
53 pairings.append((remaining[0], remaining[x]))54 p = list(remaining)55 p.pop(0)56 p.pop(x-1)57 createPairings(p)58 if(x==len(remaining)-1 and len(pairings)==len(remaining)/2):59 checkPairings(pairings,crd)60
61crd = list()62pairings = list()63for x in range(N):64 c = list(map(int,fin.readline().split()))65 crd.append(c)66#print(crd)67createPairings(crd)68fout.write("{}\n".format(str(len(total_List))))69fout.close()