Giter VIP home page Giter VIP logo

n-puzzle-solver's Introduction

N-Puzzle solver

This repository explores the N-Puzzle solver using the following algorithms (in order of increasing performance):

  1. Breadth-First Search (BFS.py)
  2. A* Search using Manhattan Distance (manhattanDistance.py)
  3. A* Search using Manhattan Distance & Linear conflict (linearConflict.py)

Running the code

python <filename> <inputfile> <outputfile>

Input format

The input file should be a 2D array representing the puzzle. Look at the test input for an example.

Output format

The scripts writes the series of steps to solve the N-puzzle into the output file. If no solution exists, it writes 'UNSOLVABLE'.

Other minor performance optimisations

Solvability Check

We can compute whether the grid is solvable in one step, by checking the position of the blank tile and the number of inversions in the grid. This improves performance significantly, as it would not have to generate all possible nodes before declaring that the puzzle is not solvable. Read more here.

Computing Manhattan Distance

Instead of computing Manhattan Distance for each grid, we could update the manhattan distance using the parent's manhattan distance, compute the change caused by a particular move.

State generation

Another minor optimisation done was to prevent generation of a new grid each time. By doing a simple swap to check if a particular state has been visited before, we saved on a couple of deepcopy operations, which were quite expensive.

n-puzzle-solver's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.