Results 1 to 4 of 4

Thread: types of stacks

  1. #1
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default types of stacks

    I am doing a n-queens problem using stacks. I'm trying to define a stack, but I don't know what type to use (e.g. int, char, or some object type.)

    Stack<unknown> personsGuess = new Stack<unknown>();

    To make a guess for the queens problem you pick a square on a grid such as row 1, column 3, or... row 6, column 2.

    The problem says, "Each time a choice is made, the choice is pushed onto a stack that already contains all the previously made choices The purpose of the stack is to make it easy to fix incorrect choices."

    the thing is I only know how to push on integer, float and double types. A first guess might look like "row 1, column 1" or row but how would this be stored onto the stack?
    Last edited by vendetta; 02-06-2010 at 12:46 AM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Java Code:
    public class Choice {
      public int rowNumber;
      public int columnNumber;
    }
    
    
    Stack<Choice> choices = new Stack<Choice>()

  3. #3
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    so, if my program has one class and one a driver/test file, do I have a third file with what you just wrote?

    or is what you wrote just written above my constructor?

    EDIT. looks like it works just above my constructor...Thanks
    Last edited by vendetta; 02-06-2010 at 01:05 AM.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Just a quick tip, we were just discussing this problem at a course at my uni. You don't acctually need the row and collumn numbers, just one or the other. Because, if you think about it, no 2 queens can be in the same row or in the same collumn, so you can break down the problem to a permutation of a list [1,2,...,n] and checking if the queens attack one another recursively. Because, the solution to the n queen problem is, that on a board size of n*n, each queen occupies one row (or one collumn).

Similar Threads

  1. Help with stacks
    By Srcee in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 11:23 AM
  2. 2 stacks, the second keeps mirroring the second??
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 10-11-2009, 07:31 AM
  3. Arithmetic Stacks
    By unc123w in forum New To Java
    Replies: 22
    Last Post: 10-21-2008, 08:24 PM
  4. Stacks
    By Zosden in forum Advanced Java
    Replies: 15
    Last Post: 05-05-2008, 08:16 AM
  5. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •