Click here to Skip to main content
16,020,432 members
Home / Discussions / Java
   

Java

 
AnswerRe: Problems Implementing Functions into Main.java Pin
Richard MacCutchan22-Sep-09 23:05
mveRichard MacCutchan22-Sep-09 23:05 
GeneralRe: Problems Implementing Functions into Main.java Pin
wkid8723-Sep-09 3:50
wkid8723-Sep-09 3:50 
GeneralRe: Problems Implementing Functions into Main.java Pin
Richard MacCutchan23-Sep-09 5:43
mveRichard MacCutchan23-Sep-09 5:43 
GeneralRe: Problems Implementing Functions into Main.java Pin
David Skelly23-Sep-09 6:24
David Skelly23-Sep-09 6:24 
GeneralRe: Problems Implementing Functions into Main.java [modified] Pin
wkid8723-Sep-09 16:59
wkid8723-Sep-09 16:59 
GeneralRe: Problems Implementing Functions into Main.java Pin
David Skelly24-Sep-09 3:35
David Skelly24-Sep-09 3:35 
AnswerRe: Problems Implementing Functions into Main.java Pin
427748023-Sep-09 18:22
427748023-Sep-09 18:22 
QuestionN Queens Problem help Pin
shiggedyshwa21-Sep-09 19:20
shiggedyshwa21-Sep-09 19:20 
I have read through numerous forums and sites, but i couldn't find exactly what i was looking for.
I am trying to just write a Java program for a single solution to the "N-Queens Problem" also known as the 8 queens problem. i know what i want to do i just dont know how to put it into code.

import java.io.*;
import java.util.*;

public class NQueensProblemSingleSolution {

  private int n;
  private int[] solutionVector;

  public NQueensProblemSingleSolution(int n) {

      if (n % 6 == 2) {
          for (m=1; m++; m=n/2)          
              // i want to place a queen at (m, 2m) and (n/2 + ,2m - 1)
      }
      if (n % 6 == 0) {
          for (m=1; m++; m=n/2)
              // i want to place a queen at (m, 1 + (2(m-1) + n/2 - 1) % n) and 
              // (n+1-m, n - (2(m-1) + n/2 - 1) % n)
      }
      if (n % 2 != 0) {
          for (m=1; m++; m=n/2)
              // i want to use (1) or (2), whichever one is appropriate, on n-1 and extend with a queen
              // at (n,n)
      }                        
  }
    @Override
  public String toString() {
        return "This is the solution: "+solutionVector; 
  //this toString is not right; i'll worry about this once i figure out the first part
    }


  public static void main(String[] args) {

    if (args.length != 1) {
      System.out.println("usage: java NQueensProblemSingleSolution <outputfile>");
      System.exit(0);
    }
    try {
      PrintWriter outFile = new PrintWriter(new FileWriter(args[0]));
      for (int i = 4; i <= 16; i++) {
        NQueensProblemSingleSolution result = new NQueensProblemSingleSolution(i);
        outFile.println(i + ": " + result);
      }     
      outFile.close();
    }
    catch(FileNotFoundException fnfe) {
      System.out.println("Open error: " + fnfe);
    }
    catch(IOException ioe) {
      System.out.println("I/O error: " + ioe);
    }
  }
}


okay some notes i forgot to mention before:
n queens are being arranged on an n x n chessboard.
so, if the chess board is 8 by 8 then there should be 8 queens.
i have tried to make a two dimensional array setting it with rows and columns but i havent been able to get it right because it wont work i might be going the wrong direction.
for example i tried this:

public NQueensProblemSingleSolution(int n) {
      int rows;
      int columns;
      Array[][] one = new Array[][];
      if (n % 6 == 2) {
          for (m=1; m++; m=n/2)
              board[n][n]= ;
}

i knew this wouldnt work and i just threw it together quickly to try and get somewhere but i declared board as a String[][] in the top.
i also forgot to mention the main section with args[] isnt something i need to change, i just included it so that it would be more clear what i am trying to use the code for.
please tell me if i forgot to mention anything else that would help make it more clear to show what i am looking for
i think we are supposed to just add mroe to the public NQueensProblemSingleSolution(int n) and i feel like we have to do more than just that i dont know
any help or hints at all would be greatly appreciated
sorry for including so much code

another note: the if and for loop sections are for the algorithm itself, depending on the 3 conditions we were given that 1. n is even but not of the form 6k + 2 (if n%6 == 2). 2. n is even but not of the form 6k (n%6==0) and 3. n is odd (n%2 != 0)

(i came up with the n% parts so if they are wrong tell me i think they are right though :z)
AnswerRe: N Queens Problem help Pin
427748021-Sep-09 20:27
427748021-Sep-09 20:27 
GeneralRe: N Queens Problem help Pin
shiggedyshwa21-Sep-09 21:15
shiggedyshwa21-Sep-09 21:15 
QuestionRead a file and find text Pin
Mbu00720-Sep-09 22:28
Mbu00720-Sep-09 22:28 
AnswerRe: Read a file and find text Pin
427748021-Sep-09 4:31
427748021-Sep-09 4:31 
QuestionHow do i get all values from a JTextfields that is located on another JInternalFrame? Pin
miceisland20-Sep-09 20:36
miceisland20-Sep-09 20:36 
AnswerRe: How do i get all values from a JTextfields that is located on another JInternalFrame? Pin
427748021-Sep-09 4:37
427748021-Sep-09 4:37 
QuestionHaving trouble with recursion (kind of long, but please read) Pin
KingLane20-Sep-09 12:48
KingLane20-Sep-09 12:48 
AnswerI got the code to work Pin
KingLane20-Sep-09 14:06
KingLane20-Sep-09 14:06 
QuestionJava projects Pin
prietycool19-Sep-09 0:29
prietycool19-Sep-09 0:29 
AnswerRe: Java projects Pin
Richard MacCutchan20-Sep-09 9:29
mveRichard MacCutchan20-Sep-09 9:29 
QuestionJava web Project using Springs Pin
prietycool19-Sep-09 0:27
prietycool19-Sep-09 0:27 
AnswerRe: Java web Project using Springs Pin
427748021-Sep-09 5:22
427748021-Sep-09 5:22 
QuestionMerge Sort problem Pin
snssewell18-Sep-09 18:02
snssewell18-Sep-09 18:02 
AnswerRe: Merge Sort problem Pin
427748018-Sep-09 18:13
427748018-Sep-09 18:13 
GeneralRe: Merge Sort problem Pin
snssewell18-Sep-09 18:25
snssewell18-Sep-09 18:25 
GeneralResolved Pin
snssewell18-Sep-09 18:29
snssewell18-Sep-09 18:29 
GeneralRe: Merge Sort problem Pin
427748018-Sep-09 18:30
427748018-Sep-09 18:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.