Thread: Puzzle Input And Array c++ using a-star algorithm

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Puzzle Input And Array c++ using a-star algorithm

    Hey,

    I've developed a 8puzzle solver using a fixed width and height

    Below is my input condition and the pre-set goal state which i use for testing.
    Code:
    for (i = 0; i < 9; i++)        cin >> initstate[i/3][i%3];
        
        for (i = 0; i < 9 ; i++){
            if (i != 8){
                endstate[i/3][i%3] = i+1;
            }else {
                endstate[i/3][i%3] = 0;
            }
    Assume initstate and endstate have been defined as two dimensional arrays with dimensions[3][3].

    I've used a-star search to find the shortest path f(n) = h(n) + g(n)
    A couple of questions
    1. How do i go about getting the input from a text file ?
    Yes i am familiar with fstream and getline but
    for example if the text file is such:
    4
    4
    1@(3,1) 6@(3,3) 0@(1,2) etc upto 16 values
    1@(1,1) 2@(1,2) 3@(1,3) etc upto 16 values

    In this case the first line denotes its Height, second line its Width
    third line its initial state and fourth line its goal state.

    Also if the input is put into a hashtable(if someone suggests)
    do the parameters given such as (3,1) (3,3) which are the position
    values of each of the pre-fix'd numbers "1" @(3,1)
    if Not then what sort of string strip/trim is required and how do
    you specify delimiters with @(can be any number, can be anynumber)

    2. Equally large problem im facing is how do i use the input Height, Width info from the text file to define/initialise my node & priority queue ? Currently they are all pre-set to function for a 3x3. I want to attempt a 4x4 & 5x3. Basically the size of the board, goal array need to be set at runtime.

    3. I've tried substituting certain fixed values such as 3, with 4
    and 9 with 16 in for loops and the respective i/dimension, i%dimension cases and the possible number of states 9factorial/8bits replaced by 16!factorial/15 bits respectively where it's required in the program and the base table (multiples of 9 or 16)
    Where am i going wrong ? what functions for a 3x3 should do the same for 4x4 with appropriately replaced values yes ? I was wondering if parity needs to be used if so then how ? I know the basic parity function.

    I've tried various test cases for the 3x3 and it performs perfectly.
    I've also seen a code online which uses template metamorphing but i cant quite get the hang of it (it works for a set dimension value, 4x4)

    Below is some sample code which gives an idea of my problem
    Code:
    // My header file ->
    class node {private:
        // functions omitted
    public:
        node(valNum board[3][3]); //valnum can be double or
        valNum nodeBoard[3][3];   //unsigned short/long int
        // other functions omitted
    };
    
    class Pqueue {
    private:
        // code omitted
        valNum startState[3][3]; //3x3 i need dynamic 5x3/4x4
        char stateChecked[45360]; //9factorial/8bits
    public:
        Pqueue(valNum begin[3][3], valNum end[3][3]);
        ~Pqueue();
        // code omitted
    };
    
    void Pqueue::doOpen(node *posn)
    {
        // code omitted
    //marks position as visited 
        stateChecked[pos/8] = stateChecked[pos/8] | check;
           
    }
    If you require any further code to understand my question better, let me know.
    Last edited by isAnonymous; 04-17-2012 at 02:24 PM. Reason: error in variable used

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in swapping puzzle contain of the array...
    By tianwu in forum C Programming
    Replies: 4
    Last Post: 11-28-2011, 09:08 AM
  2. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 01:53 PM
  3. A basic Word Puzzle Algorithm
    By m0ntana in forum C Programming
    Replies: 5
    Last Post: 03-29-2007, 12:05 PM
  4. star wars vs star trek
    By psychopath in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 06-14-2005, 10:28 PM
  5. Star Wars or Star Trek
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 09-28-2001, 08:33 AM

Tags for this Thread