Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 661019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:13:19+00:00 2026-05-13T23:13:19+00:00

I am trying to build a library for solving different constrained problems . I

  • 0

I am trying to build a library for solving different constrained problems .
I tried first with 4-queens problem and I can’t figure out how I can represent the node .
I mean I can do it without any tree classes (by dimensional arrays) but I wanna represent it as tree-structure problem .

the depth of a tree is always <=4

here is my code :

class Node {
  Node []next ;
  int value;
  int depth;
  String name;
  Node(){
      next=null;
      value=0;
      depth=0;
      name=null;
  }
  Node(int value,int depth,String name){
      this.value=value;
      //this.next=child;
      this.depth=depth;
      this.name=name;
  }

class Tree{ Node root; Stack stack; String[] vars={"Q1","Q2","Q3","Q4"}; int[] domain={1,2,3,4}; int count=0; Tree(){ root=new Node(); stack=new Stack();

}

void start(){
stack.push(root);
count++;
search(stack.pop(),0);
}

boolean consistent(Node current){
boolean flag=true;
int n=current.getDepth();
//need more
return flag;
}

private void search(Node current,int num) {

if(num==3&&consistent(current)){
System.out.println("solution !");
num=0;
}
else{
if(consistent(current)){
Node child[]=new Node[4];
for(int i=0;i<4;i++)
child[i]=new Node(domain[i],current.getDepth()+1,vars[num]);

current.setNext(child);

for(int i=3;i>=0;i--)
stack.push(child[i]);

search(stack.pop(),num+1);

}
search(stack.pop(),num);
}
}<code>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:13:20+00:00Added an answer on May 13, 2026 at 11:13 pm

    The state of the problem at any point in time is the position of each of the queens in each column on the board. As @poly said, there’s no way two queens can be in the same column. @poly did a great job explaining the parameters of the problem.

    If you’re taking a backtracking approach then what you want to do is choose a location for the first queen and continue onward to see if it works. Next you choose the location of the 2nd queen, then the 3rd and finally the 4th. If the 4th doesn’t work, you’ll backtrack to the third, if the third doesn’t work, you’ll back to the 2nd, etc.

    I’ll just talk about the 4 queen case on a 4×4 board. The way I would see it is that the root of the tree is where you’ve made zero choices. The first level below the root would be four children…one child for each possible location of the first queen in the first column (1,2,3 & 4). At height two in the tree, you choose the location of the 2nd queen in the second column and so on down the tree.

    Here’s a partially completed tree:

                              ""
                                |
                  -----------------------------------------------
                  1                  2            3             4
                  |                  |            |             |        
         ---------------------
         1,1   1,2   1,3   1,4
                |
    ------------------------------------
    
    1,1,1     1,1,2     1,1,3      1,1,4
    
                                     |
            -------------------------------------------------
            1,1,4,1      1,1,4,2       1,1,4,3        1,1,4,4
    

    So all the leaves of the tree are tuples of the form (A,B,C,D) where A is the position of the first queen, B is the position of the 2nd, C is the position of the 3rd and D is the position of the fourth.

    So I would say the “value” of each Node is the set of choices you’ve made so far. I don’t think I’d see it as an int. You could just keep it as a string if you wanted to or something like an ArrayList might make your recursion a bit easier. Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 400k
  • Answers 400k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I've understood the concern correctly: you simply divide the… May 15, 2026 at 4:18 am
  • Editorial Team
    Editorial Team added an answer You can use such command to check if Java is… May 15, 2026 at 4:18 am
  • Editorial Team
    Editorial Team added an answer str.replaceAll("\\s*\n\\s*\n\\s*\n\\s*", "\n\n") This will replace any whitespace-substring that contains more… May 15, 2026 at 4:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.