这是尝试使用深度优先搜索(递归版本)制作一个简单的 AI 解决方案来解决 peg solitaire 。< / p>
我的问题出在potentialMovesDFS
函数:
newNodes.append(Node(Node.non,problemBuf,None))
虽然problemBuf
发生了变化,但会传递旧值。
我认为它与指针有关,但我不知道如何解决这个问题。
有什么想法吗?
Tree = []
class Node():
non = 0
def __init__(self,iD,dt,ch):
self.iD = Node.non
self.data = dt
self.children = []
Tree.append(self)
Node.non = Node.non + 1
def winCheck(self):
counter = 0
for j in range(y):
counter += problem[j].count('1')
return counter
def addChildren(self,ch):
self.children.append(ch)
def getParent(self):
return self.parent
def printNode(self):
print('\n')
print("iD:",self.iD)
print("Data:",self.data)
print("Child:",self.children)
print('\n')
def printData(self):
for j in range(y):
for i in range(x):
print(problem[j][i],end='')
print(" ",end='')
print('\n')
#4 cases of movement
def potentialMovesDFS(self,problem):
newMoves = []
newNodes = []
problemBuf = problem[:]
for j in range(y):
for i in range(x):
if(problem[j][i]=='2'):
#print(problem[j][i+2]=='1',problem[j][i+1]=='1',i<=x-3)
#print("Space at:",j,i)
if(j>=2 and problem[j-2][i]=='1' and problem[j-1][i]=='1'):
#print("Move",j-2,i,"to",j,i,"and remove",j-1,i,'\n')
print('1')
buf = problemBuf[j][:i]+'1'+problemBuf[j][i+1:]
problemBuf[j] = buf
#aka problem[j][i] = '1'
buf = problemBuf[j-1][:i]+'2'+problemBuf[j-1][i+1:]
problemBuf[j-1] = buf
#aka problem[j-1][i] = '2'
buf = problemBuf[j-2][:i]+'2'+problemBuf[j-2][i+1:]
problemBuf[j-2] = buf
#aka problem[j-2][i] = '1'
newNodes.append(Node(Node.non,problemBuf,None))
newMoves.append(problemBuf)
problemBuf = problem[:]
if(i<=x-3 and problem[j][i+2]=='1' and problem[j][i+1]=='1'):
#print("Move",j,i+2,"to",j,i,"and remove",j,i+1,'\n')
print('2')
buf = problemBuf[j][:i]+'1'+problemBuf[j][i+1:]
problemBuf[j] = buf
buf = problemBuf[j][:i+1]+'2'+problemBuf[j][i+2:]
problemBuf[j] = buf
buf = problemBuf[j][:i+2]+'2'+problemBuf[j][i+3:]
problemBuf[j] = buf
newNodes.append(Node(Node.non,problemBuf,None))
newMoves.append(problemBuf)
problemBuf = problem[:]
if(j<=y-3 and problem[j+2][i]=='1' and problem[j+1][i]=='1'):
#print("Move",j+2,i,"to",j,i,"and remove",j+1,i,'\n')
print('3')
buf = problemBuf[j][:i]+'1'+problemBuf[j][i+1:]
problemBuf[j] = buf
buf = problemBuf[j+1][:i]+'2'+problemBuf[j+1][i+1:]
problemBuf[j+1] = buf
buf = problemBuf[j+2][:i]+'2'+problemBuf[j+2][i+1:]
problemBuf[j+2] = buf
newNodes.append(Node(Node.non,problemBuf,None))
newMoves.append(problemBuf[:])
problemBuf = problem[:]
if(i>=2 and problem[j][i-2]=='1' and problem[j][i-1]=='1'):
#print("Move",j,i-2,"to",j,i,"and remove",j,i-1,'\n')
print('4')
buf = problemBuf[j][:i]+'1'+problemBuf[j][i+1:]
problemBuf[j] = buf
buf = problemBuf[j][:i-1]+'2'+problemBuf[j][i:]
problemBuf[j] = buf
buf = problemBuf[j][:i-2]+'2'+problemBuf[j][i-1:]
problemBuf[j] = buf
newNodes.append(Node(Node.non,problemBuf,None))
newMoves.append(problemBuf)
problemBuf = problem[:]
return newNodes
#read file
inputFile = open("m.txt","r")
problem = inputFile.readlines()
x,y = problem[0].split(' ')
x = int(x)
y = int(y)
problem = problem[1:y+1]
for i in range(y):
problem[i] = problem[i].replace(' ','')
problem[i] = problem[i].replace('\n','')
'''
def dfs_recursive(graph, vertex, path=[]):
path += [vertex]
for neighbor in graph[vertex]:
if neighbor not in path:
path = dfs_recursive(graph, neighbor, path)
return path
adjacency_matrix = {1: [2, 3], 2: [4, 5],
3: [5], 4: [6], 5: [6],
6: [7], 7: []}
print(dfs_recursive(adjacency_matrix, 1))'''
def dfs_recursive(thisNode, path=[]):
path.append(thisNode)
#leipei h potential
for child in thisNode.potentialMovesDFS(thisNode.data):
if child not in path:
path = dfs_recursive(child, path)
return path
#set tree root
root = Node(Node.non,problem,None)
print(root.printData())
for i in dfs_recursive(root):
print(i.printData())