Created
January 20, 2020 16:47
-
-
Save samagragupta/f5c1532e249fa5d250350bc60995a5c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node: | |
| parent = None | |
| def __init__(self,state): | |
| self.state = state | |
| def setParent(self,parent): | |
| self.parent = parent | |
| def BackTrack(p): | |
| count=0 | |
| while(p.parent!=None): | |
| print(p.state) | |
| p = p.parent | |
| count+=1 | |
| print(p.state) | |
| print(count) | |
| def checkInClose(z,close): | |
| for i in close: | |
| if i.state==z: | |
| return False | |
| return True | |
| def MoveGen(p,open1,close): | |
| li = p.state | |
| pos = li.index(0) | |
| for i in range(-2,3): | |
| k = pos + i | |
| z = li.copy() | |
| if k>=0 and k<=6 and k!=pos: | |
| z[pos],z[k] = z[k],z[pos] | |
| # print(z) | |
| if checkInClose(z,close): | |
| stump = Node(z) | |
| stump.setParent(p) | |
| open1.append(stump) | |
| init_state = [1,1,1,0,2,2,2] | |
| goal_state = [2,2,2,0,1,1,1] | |
| root = Node(init_state) | |
| open1 = [root] | |
| close = [] | |
| while len(open1)>0: | |
| p = open1[-1] | |
| close.append(open1[-1]) | |
| open1.pop(-1) | |
| if p.state == goal_state: | |
| BackTrack(p) | |
| break | |
| else: | |
| MoveGen(p,open1,close) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment