Ich dachte, da eine solche Frage nirgendwo auf Google zu finden ist, war es eine Antwort wert. Es ist sicherlich eine interessante Frage, aber versuchen Sie, beim nächsten Mal ein bisschen Code zu liefern.
Dies ist meine Lösung (Vergib meine Python etwas verrostet sein)
def notDiagonal(path):
point1, point2 = path
a1, a2 = point1
b1, b2 = point2
if(a1 == b1):
return True
if(a2 == b2):
return True
else:
return False
N, M = 4, 2
matPoints, matPairs, bounds, edges = [], [], [], [(0,0),(N-1,0),(0,M-1),(N-1,M-1)]
def oneEdge(path):
point1, point2 = path
if (point1 not in edges and point2 not in edges) or (point1 in edges and point2 in edges):
return False
return True
for i in range(N):
if (i,0) not in bounds:
bounds.append((i,0))
if (i,M-1) not in bounds:
bounds.append((i,M-1))
for j in range(M):
matPoints.append((i, j))
for j in range(M):
if (0,j) not in bounds:
bounds.append((0,j))
if (N-1,j) not in bounds:
bounds.append((N-1,j))
print("number of points is: ", len(matPoints))
for i in range(len(matPoints)-1):
for j in range(i+1, len(matPoints)):
matPairs.append( (matPoints[i], matPoints[j] ) )
matPairCopy = list(matPairs)
print("number of lines before removal: ", len(matPairs))
for i in range(len(matPairs)):
a = (matPairs[i][0][0] + matPairs[i][1][0])/2.0
b = (matPairs[i][0][1] + matPairs[i][1][1])/2.0
if(int(a) == a and int(b) == b):
# Center point is (int(a), int(b))
# Delete the partitioned lines if they exist (they may have been deleted before)
if( ((matPairs[i][0][0], matPairs[i][0][1]), (int(a), int(b))) in matPairCopy):
matPairCopy.remove( ((matPairs[i][0][0], matPairs[i][0][1]), (int(a), int(b))) )
if( ((int(a), int(b)) , (matPairs[i][1][0], matPairs[i][1][1]) ) in matPairCopy ):
matPairCopy.remove( ((int(a), int(b)) , (matPairs[i][1][0], matPairs[i][1][1]) ))
for k in matPairs:
if(k[0] not in bounds or k[1] not in bounds):
if(k in matPairCopy):
matPairCopy.remove(k)
elif(notDiagonal(k) and (oneEdge(k)) and k in matPairCopy):
matPairCopy.remove(k)
print("number of lines after removing partitions: ", len(matPairCopy))
edited: Feste kleine Ausgabe
N = 2, M = 2: Output = 6
N = 2, M = 3: Ausgang = 11
N = 2, M = 4: Output = 18
N = 3, M = 3: Output = 20
N = 3, M = 4: Output = 31
Wenn Sie nur nach dem Algorithmus suchen, und nicht eine Möglichkeit, ihn in einer bestimmten Sprache zu programmieren, scheint dies die Art von Problem zu sein, die http://scicomp.stackexchange.com/ eigentlich gefallen würde. – NoseKnowsAll
Mögliches Duplikat von https://stackoverflow.com/questions/31774824/how-do-i-find-the-number-of-distinctive-uncurving-straight-lines-in-a-matrix-in [1 ]: https://stackoverflow.com/questions/31774824/how-do-i-find-the-number-of-distinctive-uncurving-straight-lines-in-a-matrix-in – David