Giter Club home page Giter Club logo

n-puzzle-problem-cpp-implementation-using-a-star-search's Introduction

N-puzzle problem is one of the well-known classical problems in artificial intelligence for modelling algorithm using heuristic.

Problem description is as follows :

  • Given a k x k grid with k2-1 (=n) blocks each labeled with 1 through n and a blank one.
    Our goal is to rearrange the blocks so that they are in order, using fewest number of moves possible.
    A move can be either a horizontal or vertical sliding of block.

This C++ implementation uses A* search to solve this problem using heuristic. The success of this approach largely depends on the choice of heuristic.

Here three heuristics have been considered.

  • Hamming Distance : The number of blocks in the wrong position (excluding the blank)

  • Manhattan distance: The sum of the Manhattan distances (sum of the vertical and horizontal distance) from the blocks to their goal positions.

  • Linear Conflict: Two tiles tj and tk are in a linear conflict if tj and tk are the same line, the goal positions of tj and tk are both in that line,tj is to the right of tk , and goal position of tj is to the left of the goal position of tk . Here line indicated both rows and columns. The linear conflict heuristic is calculated as Manhattan distance + 2*(Linear conflicts).

Hamming distance heuristic may require a lot of memory even over 5GB for solutions having significant number of steps. Better not to run hamming distance heuristic for 4x4 or larger boards.
You can change this in "main.cpp".

Procedure to run this Implementation:

Step 1:

Input format:(in "in.txt")  

Line 1 : n  
Next put initial k X k board configuration with numbers from 1 to n and a single 0 that denotes blank

An input:

3
1 5 0
7 6 4
2 3 8

Step 2:

Compile "main.cpp" and then run the executable    
or  
You can open the whole folder as a CLion Project.  

Step 3:

You will find the output in "out.txt".  

Some sample inputs have been provided in "in.txt"

This implementation requires minimum C++11

n-puzzle-problem-cpp-implementation-using-a-star-search's People

Contributors

subangkar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.