hi I am working on this python code and wanted to know which searching algorithm its using to find goal state. I want to know if the following code is using BFS or any other searching algorithm this code is solving classic farmer, wolf, cabbage, goat problem
initial = ['L', 'L', 'L', 'L']
goal = ['R', 'R', 'R', 'R']
stack = []
stack.append(initial)
def nextState(state):
if state == goal: return False
farmer = state[3]
for i, s in enumerate(state):
if s == farmer:
tryState = makeState(state, i)
if testState(tryState) and isUnique(tryState):
stack.append(tryState)
return True
return False
def makeState(s, i):
t = []
for x in s: t.append(x)
t[3] = 'R' if t[3] == 'L' else 'L'
if t[3] == 'L':
if not testState(t):
t[i] = 'R' if t[i] == 'L' else 'L'
else:
t[i] = 'R' if t[i] == 'L' else 'L'
return t
def testState(s):
if s[0] == s[1] and s[3] != s[0]: return False
if s[1] == s[2] and s[3] != s[1]: return False
return True
def isUnique(s):
if s in stack: return False
return True
while nextState(stack[-1]): pass
for i, x in enumerate(stack):
print (i)
print (x)