Esempio n. 1
0
    print(name+", what do you want to do?\nACT:\n[U] Go Up/Forward\n[D] Go Down/Back\n[L] Go Left\n[R] Go Right\n[-] Wait\n\nOTHER:\n[S] Show Me The Way\n[E] Exhaust Nodes\n[Q] Quit\n")
    print("Current Position: "+str(n))
    i = raw_input("Choice: ")
    if (i.upper() == 'Q'):
        break
    elif (i.upper() == 'S'):
        print("You giving up? Ha! Weak...")
        t0 = time.time()
        Solver(n, k).solve(True)
        print "Time to solve:", time.time()-t0, "s"
        break
    elif (i.upper() == 'E'):
        Solver(n, k).exhaust_nodes()
        break
    else:
        n = M.move(i.upper(), n)

if M.check_goal(n):
    print("\nCurrent Position: "+str(n))
    print("************ CONGRATULATIONS! YOU GOT OUT ALIVE! ************")
else:
    print("\nGood night...")

"""
OBSERVATIONS:
- Interesting... So the columns repeat after 4 columns. Which means columns
  1, 5, 9, etc. have the same values. In general, if there i rows and j
  columns, then j, i+j, 2i+j, ... have the same column compositions.
- Some nodes are two-way; that is, they are inverses of each other. Most nodes
  are one-way as expected of One-Way Woods.
Esempio n. 2
0
class Graph:
    def __init__(self, k):
        # Pointers to root, and solution nodes; initially None
        self.root = None
        self.solution = None
        self.M = Master(k)

        # List of values already visited and discovered
        self.visited = []
        self.bfs_disc_val = []

        # Queue of nodes to visit in BFS
        self.bfs_queue = []

    def init_root(self, val):
        N = Node(val)
        self.root = N

    def bfs_traversal(self, N):
        # Check moves in the ff. order: U, R, D, L, Wait (-)
        directions = ['U', 'R', 'D', 'L', '-']
        for d in directions:
            n = self.M.move(d, N.val)
            if (n not in self.visited) and (n not in self.bfs_disc_val):
                if d == 'U':
                    N.up = Node(n, N.path+d)
                    N.up.prev = N.val
                    self.bfs_queue.append(N.up)
                    if self.M.check_goal(N.up.val):
                        N = N.up
                        break
                elif d == 'R':
                    N.right = Node(n, N.path+d)
                    N.right.prev = N.val
                    self.bfs_queue.append(N.right)
                    if self.M.check_goal(N.right.val):
                        N = N.right
                        break
                elif d == 'D':
                    N.down = Node(n, N.path+d)
                    N.down.prev = N.val
                    self.bfs_queue.append(N.down)
                    if self.M.check_goal(N.down.val):
                        N = N.down
                        break
                elif d == 'L':
                    N.left = Node(n, N.path+d)
                    N.left.prev = N.val
                    self.bfs_queue.append(N.left)
                    if self.M.check_goal(N.left.val):
                        N = N.left
                        break
                elif d == '-':
                    N.wait = Node(n, N.path+d)
                    N.wait.prev = N.val
                    self.bfs_queue.append(N.wait)
                    if self.M.check_goal(N.wait.val):
                        N = N.wait
                        break
                self.bfs_disc_val.append(n)
        self.visited.append(N.val)
        
        if self.M.check_goal(N.val):
            self.solution = N
            return True
        return False

    def bfs_driver(self):
        # Enqueue root as initial point
        self.bfs_queue.append(self.root)
        self.bfs_disc_val.append(self.root.val)
        
        while len(self.bfs_queue) > 0:
            if(self.bfs_traversal(self.bfs_queue.pop(0))):
                break
        return self.solution